fix(app): exit without waiting for the UI thread, close the editor with the shell, cancel sign-in safely
DESK-1: SyncHost's dispose, session switch and stop now continue on the thread pool (ConfigureAwait(false)) and keep the running agent in one reference. A signed-in user whose archive is not polled no longer waits 10 s at every exit, after which the job engine and the index were never closed. DESK-7: ShellViewModel.Dispose disposes the Sequence section first, so the open editor stops its runners and closes its source before the engine and the index go. DESK-8: Cancel stays available while the server answers a sign-in. It now cancels the request; an answer that still arrives changes nothing (the connection flow ignores answers for a client it dropped), and a sign-in that completed just before the cancel was handled is signed out again. The form no longer sets a result after it closed, and an unexpected failure shows inline instead of escaping async void. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_012iLMBNwBE2GnphAsu4cjaC
This commit is contained in:
8 files changed
+345
-35
No files matched your search
@@ -131,23 +131,28 @@ public sealed class ConnectFlow(IServerApiFactory factory, ITrustStore trust) :
|
||||
return await IdentifyAsync(ct);
|
||||
}
|
||||
|
||||
/// <summary>Sends the credentials; only possible once the server is identified and trusted.</summary>
|
||||
/// <summary>
|
||||
/// Sends the credentials; only possible once the server is identified and trusted. An answer that arrives after the
|
||||
/// flow moved on (disconnected, or connecting elsewhere) changes nothing.
|
||||
/// </summary>
|
||||
public async Task<ConnectStep> SignInAsync(string username, string password, CancellationToken ct = default)
|
||||
{
|
||||
if (Step is not (ConnectStep.AwaitingCredentials or ConnectStep.SigningIn) || _api is null) return Step;
|
||||
if (Step is not (ConnectStep.AwaitingCredentials or ConnectStep.SigningIn) || _api is not { } api) return Step;
|
||||
if (string.IsNullOrWhiteSpace(username)) return Retry(ConnectError.BadCredentials);
|
||||
Set(ConnectStep.SigningIn, ConnectError.None);
|
||||
try
|
||||
{
|
||||
var login = await _api.LoginAsync(username.Trim(), password, ct);
|
||||
var login = await api.LoginAsync(username.Trim(), password, ct);
|
||||
if (!ReferenceEquals(api, _api)) return Step;
|
||||
if (login.Token is null) return Retry(ConnectError.BadCredentials);
|
||||
// TitanoClient keeps the token itself; set it explicitly so every IServerApi behaves the same.
|
||||
_api.Token = login.Token;
|
||||
api.Token = login.Token;
|
||||
User = login.User;
|
||||
return Set(ConnectStep.Connected, ConnectError.None);
|
||||
}
|
||||
catch (Exception ex) when (Classify(ex, ct) is { } error)
|
||||
{
|
||||
if (!ReferenceEquals(api, _api)) return Step;
|
||||
return error is ConnectError.BadCredentials ? Retry(error) : Fail(error);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -144,17 +144,23 @@ public sealed class ServerSession : ObservableObject, ITrustStore, IDisposable
|
||||
if (step != ConnectStep.AwaitingCredentials) return false;
|
||||
}
|
||||
|
||||
// The form may be cancelled while the server is still answering: that answer must not sign this PC in.
|
||||
using var attempt = CancellationTokenSource.CreateLinkedTokenSource(ct);
|
||||
var signedIn = dialogs.SignIn(display, _settings.ServerUsername, async (user, password) =>
|
||||
{
|
||||
var result = await _flow.SignInAsync(user, password, ct);
|
||||
return result == ConnectStep.Connected ? null : ErrorText(_flow.Error);
|
||||
var result = await _flow.SignInAsync(user, password, attempt.Token);
|
||||
// An answer the flow dropped (the form was cancelled meanwhile) carries no error of its own.
|
||||
return result == ConnectStep.Connected ? null : ErrorText(_flow.Error is ConnectError.None ? ConnectError.Failed : _flow.Error);
|
||||
});
|
||||
if (signedIn && IsConnected)
|
||||
{
|
||||
Remember();
|
||||
return true;
|
||||
}
|
||||
if (!IsConnected && _flow.Step == ConnectStep.AwaitingCredentials) _flow.Disconnect();
|
||||
await attempt.CancelAsync();
|
||||
// Signed in just before the cancel was handled: the session the user turned down ends on the server too.
|
||||
if (IsConnected) await _flow.SignOutAsync();
|
||||
else if (_flow.Step is ConnectStep.AwaitingCredentials or ConnectStep.SigningIn) _flow.Disconnect();
|
||||
return false;
|
||||
}
|
||||
finally
|
||||
|
||||
@@ -29,7 +29,7 @@ public sealed class SyncHost(AppServices services) : ISyncConnector, IAsyncDispo
|
||||
|
||||
private Task _switch = Task.CompletedTask;
|
||||
private Credentials? _credentials;
|
||||
private (SyncAgent Agent, ISyncRemote Remote, Credentials Credentials)? _running;
|
||||
private Running? _running;
|
||||
private JobEngine? _renders;
|
||||
|
||||
/// <summary>Connections over <see cref="TitanoClient"/>; screenshots substitute a fake server.</summary>
|
||||
@@ -44,10 +44,10 @@ public sealed class SyncHost(AppServices services) : ISyncConnector, IAsyncDispo
|
||||
public SyncService? Service { get; private set; }
|
||||
|
||||
/// <summary>The background comparison of the signed-in server; null while signed out.</summary>
|
||||
public SyncAgent? Agent => _running?.Agent;
|
||||
public SyncAgent? Agent => Volatile.Read(ref _running)?.Agent;
|
||||
|
||||
/// <summary>The agent's connection, for the table's own calls (conflict resolution); null while signed out.</summary>
|
||||
public ISyncRemote? Remote => _running?.Remote;
|
||||
public ISyncRemote? Remote => Volatile.Read(ref _running)?.Remote;
|
||||
|
||||
/// <summary>Key of the signed-in server (<see cref="SyncService.ServerKey"/>); null while signed out.</summary>
|
||||
public string? Server => Volatile.Read(ref _credentials) is { } c ? SyncService.ServerKey(c.Address) : null;
|
||||
@@ -103,8 +103,8 @@ public sealed class SyncHost(AppServices services) : ISyncConnector, IAsyncDispo
|
||||
/// <summary>Compares now, once the session switch in progress settled; null while signed out.</summary>
|
||||
public async Task<SyncSnapshot?> RefreshAsync(CancellationToken ct = default)
|
||||
{
|
||||
await _switch;
|
||||
return Agent is { } agent ? await agent.RefreshAsync(ct) : null;
|
||||
await _switch.ConfigureAwait(false);
|
||||
return Agent is { } agent ? await agent.RefreshAsync(ct).ConfigureAwait(false) : null;
|
||||
}
|
||||
|
||||
/// <summary>Compares again soon (an option changed, an edit on this PC).</summary>
|
||||
@@ -135,13 +135,17 @@ public sealed class SyncHost(AppServices services) : ISyncConnector, IAsyncDispo
|
||||
return RemoteFactory(c.Address, c.Token, c.Fingerprints);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Stops the agent once the session switch in progress settled. The app's exit waits for this on the UI thread, so
|
||||
/// nothing here, in the switch or in the stop continues on it.
|
||||
/// </summary>
|
||||
public async ValueTask DisposeAsync()
|
||||
{
|
||||
Volatile.Write(ref _credentials, null);
|
||||
Uploads.Dispose();
|
||||
if (_renders is not null) _renders.JobChanged -= OnJobChanged;
|
||||
await _switch;
|
||||
await StopAsync();
|
||||
await _switch.ConfigureAwait(false);
|
||||
await StopAsync().ConfigureAwait(false);
|
||||
}
|
||||
|
||||
private bool Allows(Permissions permission) => services.Server.User is { } user && (user.Permissions & (long)permission) != 0;
|
||||
@@ -183,11 +187,16 @@ public sealed class SyncHost(AppServices services) : ISyncConnector, IAsyncDispo
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Switches are chained (each waits for the previous one) and continue on the thread pool: a switch still stopping
|
||||
/// the old agent when the app exits must not need the UI thread its exit blocks.
|
||||
/// </summary>
|
||||
private async Task SwitchAsync(Task previous, Credentials? wanted)
|
||||
{
|
||||
await previous;
|
||||
if (_running is { } running && wanted is { } same && running.Credentials.Address == same.Address && running.Credentials.Token == same.Token) return;
|
||||
await StopAsync();
|
||||
await previous.ConfigureAwait(false);
|
||||
if (Volatile.Read(ref _running) is { } running && wanted is { } same && running.Credentials.Address == same.Address && running.Credentials.Token == same.Token)
|
||||
return;
|
||||
await StopAsync().ConfigureAwait(false);
|
||||
if (wanted is not { } start || Service is not { } service)
|
||||
{
|
||||
Changed?.Invoke();
|
||||
@@ -197,19 +206,21 @@ public sealed class SyncHost(AppServices services) : ISyncConnector, IAsyncDispo
|
||||
var agent = new SyncAgent(service, remote, () => Policy, services.Engine);
|
||||
agent.Changed += () => Changed?.Invoke();
|
||||
agent.Start();
|
||||
_running = (agent, remote, start);
|
||||
Volatile.Write(ref _running, new Running(agent, remote, start));
|
||||
Log.Info("sync", $"Comparing with {remote.Server}");
|
||||
Changed?.Invoke();
|
||||
}
|
||||
|
||||
private async Task StopAsync()
|
||||
{
|
||||
if (_running is not { } running) return;
|
||||
_running = null;
|
||||
await running.Agent.DisposeAsync();
|
||||
if (Interlocked.Exchange(ref _running, null) is not { } running) return;
|
||||
await running.Agent.DisposeAsync().ConfigureAwait(false);
|
||||
running.Remote.Dispose();
|
||||
Changed?.Invoke();
|
||||
}
|
||||
|
||||
private sealed record Credentials(Uri Address, string Token, IReadOnlyCollection<string> Fingerprints);
|
||||
|
||||
/// <summary>The agent of the signed-in session: one reference, so the UI thread never reads half of a switch.</summary>
|
||||
private sealed record Running(SyncAgent Agent, ISyncRemote Remote, Credentials Credentials);
|
||||
}
|
||||
@@ -1,6 +1,7 @@
|
||||
using System.Windows;
|
||||
using System.Windows.Automation;
|
||||
using System.Windows.Automation.Peers;
|
||||
using Titano.Core.Diagnostics;
|
||||
using Titano.Ui.Controls;
|
||||
|
||||
namespace Titano.App.Shell;
|
||||
@@ -9,16 +10,20 @@ namespace Titano.App.Shell;
|
||||
/// The login form (BRIEF §12.7). It calls back the connection with the credentials and stays open with the
|
||||
/// error sentence until the sign-in succeeds or the user cancels. The password never leaves the PasswordBox
|
||||
/// except for that call. A failed sign-in is announced to screen readers and marks the password box (error border,
|
||||
/// sentence as its help text) until the next attempt.
|
||||
/// sentence as its help text) until the next attempt. Cancel stays available while the server answers: the form
|
||||
/// closes at once and the connection drops the answer.
|
||||
/// </summary>
|
||||
public sealed partial class LoginDialog : TitanoWindow
|
||||
{
|
||||
private readonly Func<string, string, Task<string?>> _signIn;
|
||||
private bool _pending;
|
||||
private bool _closed;
|
||||
|
||||
private LoginDialog(string server, string? username, Func<string, string, Task<string?>> signIn)
|
||||
{
|
||||
_signIn = signIn;
|
||||
InitializeComponent();
|
||||
Closed += (_, _) => _closed = true;
|
||||
ServerText.Text = server;
|
||||
User.Text = username ?? string.Empty;
|
||||
Loaded += (_, _) =>
|
||||
@@ -41,28 +46,41 @@ public sealed partial class LoginDialog : TitanoWindow
|
||||
|
||||
private async void OnSignIn(object sender, RoutedEventArgs e)
|
||||
{
|
||||
if (_pending) return;
|
||||
_pending = true;
|
||||
SignIn.IsEnabled = false;
|
||||
Error.Visibility = Visibility.Collapsed;
|
||||
Field.SetError(Password, null);
|
||||
string? error;
|
||||
try
|
||||
{
|
||||
var error = await _signIn(User.Text, Password.Password);
|
||||
if (error is null)
|
||||
{
|
||||
DialogResult = true;
|
||||
return;
|
||||
}
|
||||
Error.Text = error;
|
||||
Error.Visibility = Visibility.Visible;
|
||||
Field.SetError(Password, error);
|
||||
Password.Clear();
|
||||
Password.Focus();
|
||||
UIElementAutomationPeer.CreatePeerForElement(this)?.RaiseNotificationEvent(
|
||||
AutomationNotificationKind.ActionCompleted, AutomationNotificationProcessing.ImportantMostRecent, error, "Titano.SignIn");
|
||||
error = await _signIn(User.Text, Password.Password);
|
||||
}
|
||||
catch (Exception ex) when (ex is not OutOfMemoryException)
|
||||
{
|
||||
// Cancel ended the request: nobody waits for this answer any more.
|
||||
if (_closed && ex is OperationCanceledException) return;
|
||||
Log.Warning("server", "Sign-in failed", ex);
|
||||
error = ErrorText.For(ex);
|
||||
}
|
||||
finally
|
||||
{
|
||||
_pending = false;
|
||||
SignIn.IsEnabled = true;
|
||||
}
|
||||
// The form was cancelled or closed while the server answered: a result can no longer be set on it.
|
||||
if (_closed) return;
|
||||
if (error is null)
|
||||
{
|
||||
DialogResult = true;
|
||||
return;
|
||||
}
|
||||
Error.Text = error;
|
||||
Error.Visibility = Visibility.Visible;
|
||||
Field.SetError(Password, error);
|
||||
Password.Clear();
|
||||
Password.Focus();
|
||||
UIElementAutomationPeer.CreatePeerForElement(this)?.RaiseNotificationEvent(
|
||||
AutomationNotificationKind.ActionCompleted, AutomationNotificationProcessing.ImportantMostRecent, error, "Titano.SignIn");
|
||||
}
|
||||
}
|
||||
@@ -292,6 +292,8 @@ public sealed class ShellViewModel : LocalizedViewModel
|
||||
|
||||
public override void Dispose()
|
||||
{
|
||||
// The editor first: it stops its runners and closes its source before the engine and the index under it go.
|
||||
Sequence.Dispose();
|
||||
Jobs.Dispose();
|
||||
Library.Dispose();
|
||||
Import.Dispose();
|
||||
|
||||
Reference in new issue
Block a user