fix(app): exit stops the node and engine off the UI thread; node follows trust and hardware choices

AppServices disposal and archive polling no longer continue on the UI
thread that App.OnExit blocks, so exit stops everything instead of timing
out. The render node never starts in capture mode, restarts when a trusted
certificate or the hardware preference changes, and starts again after the
server refused it. Local renders apply Settings > Rendering hardware use.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_012iLMBNwBE2GnphAsu4cjaC
This commit is contained in:
Claude committed 2026-10-02 23:16:23 +00:00
1 parent f7a1c158f4
commit e8dca63605
4 files changed
+186 -32

No files matched your search

@@ -0,0 +1,53 @@
using System.IO;
using Titano.App.Infrastructure;
using Titano.App.Tests.Uploader;
using Titano.Core.Model;
using Titano.Data;
using Titano.Data.Archiving;
using Titano.Testing;
namespace Titano.App.Tests.Infrastructure;
public sealed class AppServicesTests
{
/// <summary>Dialogs that answer like a user who cancels everything.</summary>
private sealed class NoDialogs : IAppDialogs
{
public string? PickFolder(string title, string? initialFolder = null) => null;
public string? PickJsonFile(string title) => null;
public bool Confirm(ConfirmRequest request) => false;
public bool SignIn(string server, string? username, Func<string, string, Task<string?>> signIn) => false;
}
[Test]
[WindowsOnly]
public void ExitStopsPollingAndTheEngineWhileTheUiThreadWaits()
{
var environment = AppEnvironment.ForCapture();
try
{
SingleThreadedContext.Run(async () =>
{
var root = Path.Combine(environment.DataDirectory, "archive");
await using (var setup = await TitanoDatabase.OpenAsync(DatabaseOptions.Sqlite(Path.Combine(environment.DataDirectory, "setup.db"))))
await new ArchiveService(setup).CreateAsync(root, "Exit");
var services = new AppServices(environment, new AppSettings { Language = "en", LastArchivePath = root }, () => { }, new NoDialogs());
await services.OpenAsync(environment.DatabasePath);
// The archive was reopened and its first rescan queued: polling now waits for the next one.
var until = DateTime.UtcNow + TimeSpan.FromSeconds(30);
while ((await services.Database!.Jobs.ListAsync(null, JobKind.Scan)).Items.Count == 0)
{
Assert.True(DateTime.UtcNow < until, "no rescan queued");
await Task.Delay(50);
}
// App.OnExit blocks the UI thread until the services stop: nothing they await may continue on it.
Assert.True(services.DisposeAsync().AsTask().Wait(TimeSpan.FromSeconds(10)), "services stopped");
});
}
finally
{
environment.DeleteCaptureData();
}
}
}
@@ -6,7 +6,7 @@ namespace Titano.App.Tests.Server;
public sealed class RenderNodeHostTests
{
private static readonly RenderNodeSession Nas = new(new Uri("https://nas.local:8443/"), "token-a");
private static readonly RenderNodeSession Nas = new(new Uri("https://nas.local:8443/"), "token-a", ["AA:BB", "CC:DD"], Hardware: true);
/// <summary>Records the nodes it starts; fails the first <see cref="Failures"/> starts.</summary>
private sealed class Nodes
@@ -14,7 +14,7 @@ public sealed class RenderNodeHostTests
public List<(RenderNodeSession Session, Node Node)> Started { get; } = [];
public int Failures { get; set; }
public IAsyncDisposable Start(RenderNodeSession session)
public IRenderNode Start(RenderNodeSession session)
{
if (Failures-- > 0) throw new IOException("cache folder unavailable");
var node = new Node();
@@ -23,13 +23,21 @@ public sealed class RenderNodeHostTests
}
}
private sealed class Node : IAsyncDisposable
private sealed class Node : IRenderNode
{
private readonly TaskCompletionSource _completion = new();
public bool Stopped { get; private set; }
public Task Completion => _completion.Task;
/// <summary>The server refused the node (signed out, no render-node right): it ends by itself.</summary>
public void Refuse() => _completion.TrySetResult();
public ValueTask DisposeAsync()
{
Stopped = true;
_completion.TrySetResult();
return ValueTask.CompletedTask;
}
}
@@ -46,6 +54,11 @@ public sealed class RenderNodeHostTests
Assert.Equal(Nas, host.Session);
Assert.Count(1, nodes.Started);
// An equal session (fingerprints in another order) keeps the node.
host.Update(Nas with { TrustedFingerprints = ["CC:DD", "AA:BB"] });
await host.Switching;
Assert.Count(1, nodes.Started);
// A new token (signed in again) is another session: the old node stops before the new one starts.
var renewed = Nas with { Token = "token-b" };
host.Update(renewed);
@@ -53,10 +66,53 @@ public sealed class RenderNodeHostTests
Assert.True(nodes.Started[0].Node.Stopped, "old node stopped");
Assert.Equal((renewed, false), (nodes.Started[1].Session, nodes.Started[1].Node.Stopped));
// A forgotten certificate or another hardware choice also needs a new node.
host.Update(renewed with { TrustedFingerprints = [] });
await host.Switching;
host.Update(renewed with { TrustedFingerprints = [], Hardware = false });
await host.Switching;
Assert.Count(4, nodes.Started);
Assert.True(nodes.Started[2].Node.Stopped, "replaced again");
host.Update(null);
await host.Switching;
Assert.Null(host.Session);
Assert.True(nodes.Started[1].Node.Stopped, "stopped when no longer wanted");
Assert.True(nodes.Started[3].Node.Stopped, "stopped when no longer wanted");
}
[Test]
public async Task ANodeThatStoppedItselfIsStartedAgainByTheNextUpdate()
{
var nodes = new Nodes();
await using var host = new RenderNodeHost(nodes.Start);
host.Update(Nas);
await host.Switching;
nodes.Started[0].Node.Refuse();
host.Update(Nas);
await host.Switching;
Assert.Count(2, nodes.Started);
Assert.Equal((Nas, false), (host.Session, nodes.Started[1].Node.Stopped));
}
[Test]
public void TheNodeIsWantedOnlyWhenOfferedAndSignedIn()
{
var address = new Uri("https://nas.local:8443/");
Assert.Equal(Nas, RenderNodeSession.For(offered: true, connected: true, address, "token-a", ["AA:BB", "CC:DD"], hardware: true));
Assert.Null(RenderNodeSession.For(offered: false, connected: true, address, "token-a", [], hardware: true));
Assert.Null(RenderNodeSession.For(offered: true, connected: false, address, "token-a", [], hardware: true));
Assert.Null(RenderNodeSession.For(offered: true, connected: true, address, null, [], hardware: true)); // signed out
Assert.Null(RenderNodeSession.For(offered: true, connected: true, null, "token-a", [], hardware: true));
}
[Test]
public void HardwarePreferenceDecidesPerPreset()
{
Assert.True(AppServices.AllowsHardware(HardwarePreference.PreferHardware, "4k-hevc"));
Assert.False(AppServices.AllowsHardware(HardwarePreference.SoftwareOnly, "720p-preview"));
Assert.True(AppServices.AllowsHardware(HardwarePreference.HardwareForPreviews, "720p-preview"));
Assert.False(AppServices.AllowsHardware(HardwarePreference.HardwareForPreviews, "4k-hevc10-archive"));
}
[Test]