fix(app): project files handed over while Titano starts or closes are no longer lost

The running Titano listens for later launches as soon as it holds its data folder's mutex and keeps what arrives
until the window can take it, so a double-clicked .titano during a slow start is opened instead of dropped after
3 s. When the main window closes it stops listening (the mutex stays until the services have stopped), so a later
launch gets no answer and, instead of exiting, waits up to 30 s for the closing instance to exit and starts in its
place with the file. A mutex created by a Titano running with administrator rights no longer crashes a normal
launch: it counts as another instance and the launch exits quietly.

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-12 11:41:57 +00:00
1 parent 067e8ca062
commit bd38cf2e36
3 files changed
+224 -17

No files matched your search

@@ -123,15 +123,25 @@ public sealed class ProjectFilesTests(TestContext context)
var first = Assert.NotNull(SingleInstance.TryAcquire(data));
try
{
var received = new TaskCompletionSource<IReadOnlyList<string>>(TaskCreationOptions.RunContinuationsAsynchronously);
first.Listen(args => received.TrySetResult(args));
first.Listen();
Assert.Null(SingleInstance.TryAcquire(data), "one Titano per data folder");
using (var other = SingleInstance.TryAcquire(context.CreateTempDirectory("other-copy")))
Assert.NotNull(other, "a portable copy with its own data folder runs on its own");
// Launches while the first one is still opening its window are kept until the window takes them. The pipe
// takes one launch at a time, so once the second hand-over is through, the first one is already waiting.
Assert.True(SingleInstance.HandOver(data, [@"D:\Projects\Early.titano"]));
Assert.True(SingleInstance.HandOver(data, []));
using var received = new System.Collections.Concurrent.BlockingCollection<IReadOnlyList<string>>();
first.Receive(received.Add);
Assert.True(received.TryTake(out var early, TimeSpan.FromSeconds(10)), "the hand-over that came first is delivered");
Assert.SequenceEqual([@"D:\Projects\Early.titano"], early!);
Assert.True(received.TryTake(out var show, TimeSpan.FromSeconds(10)), "then the plain launch");
Assert.Empty(show!);
Assert.True(SingleInstance.HandOver(data, [@"D:\Projects\Roof.titano"]));
Assert.True(received.Task.Wait(TimeSpan.FromSeconds(10)), "the running instance received the hand-over");
Assert.SequenceEqual([@"D:\Projects\Roof.titano"], received.Task.Result);
Assert.True(received.TryTake(out var later, TimeSpan.FromSeconds(10)), "the running instance received the hand-over");
Assert.SequenceEqual([@"D:\Projects\Roof.titano"], later!);
}
finally
{
@@ -139,6 +149,46 @@ public sealed class ProjectFilesTests(TestContext context)
}
}
[Test]
public void ALaunchWhileTitanoClosesWaitsForItAndStartsInItsPlace()
{
var data = context.CreateTempDirectory("closing");
var first = Assert.NotNull(SingleInstance.TryAcquire(data));
first.Listen();
first.Receive(_ => { });
// Closing: it no longer answers, but it still holds the data folder until its services have stopped.
first.StopListening();
Assert.False(SingleInstance.HandOver(data, [@"D:\Projects\Roof.titano"]), "a closing Titano takes no file it could not open");
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(() =>
{
using var instance = SingleInstance.WaitForTurn(data, TimeSpan.FromSeconds(20));
return instance is not null;
});
Assert.False(next.Wait(TimeSpan.FromMilliseconds(300)), "it waits while the first one still runs");
first.Dispose();
Assert.True(next.Wait(TimeSpan.FromSeconds(20)) && next.Result, "it starts once the first one has exited");
// Nobody exits in time: the launch gives up.
var holder = Assert.NotNull(SingleInstance.TryAcquire(data));
try
{
Assert.False(Task.Run(() => SingleInstance.WaitForTurn(data, TimeSpan.FromMilliseconds(200)) is { } got && Dispose(got)).Result);
}
finally
{
holder.Dispose();
}
static bool Dispose(SingleInstance instance)
{
instance.Dispose();
return true;
}
}
/// <summary>Dialogs that pick one folder and cancel everything else.</summary>
private sealed class FolderDialogs(string folder) : IAppDialogs
{