From 59605168b2b44bb1b15233d1f661c27e6177c242 Mon Sep 17 00:00:00 2001 From: Claude Date: Mon, 12 Oct 2026 12:12:48 +0000 Subject: [PATCH] test(app): competing launches wait on a thread of their own The single-instance test ran the second launch with Task.Run and waited on it. A task waited on before it starts may run inline on the waiting thread, which owns the instance mutex, and a mutex lets its owner in again at once, so the "nobody exits in time" check could see the launch get its turn. The launches now run on dedicated threads. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_012iLMBNwBE2GnphAsu4cjaC --- .../Editor/ProjectFilesTests.cs | 24 +++++++++++++++++-- 1 file changed, 22 insertions(+), 2 deletions(-) diff --git a/tests/Titano.App.Tests/Editor/ProjectFilesTests.cs b/tests/Titano.App.Tests/Editor/ProjectFilesTests.cs index 098c017f..526967dc 100644 --- a/tests/Titano.App.Tests/Editor/ProjectFilesTests.cs +++ b/tests/Titano.App.Tests/Editor/ProjectFilesTests.cs @@ -162,7 +162,7 @@ public sealed class ProjectFilesTests(TestContext context) Assert.Null(SingleInstance.TryAcquire(data)); // The next launch waits (the mutex belongs to a thread, so it waits on its own) and gets its turn once the first exits. - var next = Task.Run(() => + var next = OnOwnThread(() => { using var instance = SingleInstance.WaitForTurn(data, TimeSpan.FromSeconds(20)); return instance is not null; @@ -175,7 +175,7 @@ public sealed class ProjectFilesTests(TestContext context) var holder = Assert.NotNull(SingleInstance.TryAcquire(data)); try { - Assert.False(Task.Run(() => SingleInstance.WaitForTurn(data, TimeSpan.FromMilliseconds(200)) is { } got && Dispose(got)).Result); + Assert.False(OnOwnThread(() => SingleInstance.WaitForTurn(data, TimeSpan.FromMilliseconds(200)) is { } got && Dispose(got)).Result); } finally { @@ -187,6 +187,26 @@ public sealed class ProjectFilesTests(TestContext context) instance.Dispose(); return true; } + + // A thread of its own, never this one: a mutex this thread owns would let it in at once, and a task waited on + // before it started may run inline on the waiting thread. + static Task OnOwnThread(Func launch) + { + var done = new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously); + new Thread(() => + { + try + { + done.SetResult(launch()); + } + catch (Exception ex) + { + done.SetException(ex); + } + }) + { IsBackground = true }.Start(); + return done.Task; + } } /// Dialogs that pick one folder and cancel everything else.