Free, open-source file converter. Convert 263 formats offline by drag & drop — images, PDF, Word, Excel, PowerPoint, HWP/HWPX, video, audio, e-books and archives. No upload, no Office needed.
C# audio-converteravaloniabatch-conversionconvertercsharpdocument-converterdocx-to-pdfdotnetdrag-and-dropfile-conversionfile-converterheic-converterhwphwpximage-converterlinuxmacospdf-convertervideo-converterwindows
Later launches wait for a Filee that is still starting
KnifeLemon76a6958f730e
diff --git a/CHANGELOG.md b/CHANGELOG.mdindex 9a9a957b..03fd1717 100644--- a/CHANGELOG.md+++ b/CHANGELOG.md@@ -39,6 +39,12 @@ zh: 自动转换文件夹会在输出中保留文件所在的子文件夹`Inbox\2025\scans` now go to `converted\2025\scans` instead of all into `converted` (where files of the same namebecame `a (2).pdf`), the way originals are already moved. `filee watch` does the same.+### Fixed++- **Starting Filee again while it is still starting.** Files opened from the right-click menu or Send To, or the+ installer's request to close Filee for an update, were lost when Filee took more than a few seconds to start (a+ slow or busy computer). They now wait until Filee is ready.+## 1.10.3<!-- updatediff --git a/src/Filee.App/Program.cs b/src/Filee.App/Program.csindex b09b51d7..0d83b53b 100644--- a/src/Filee.App/Program.cs+++ b/src/Filee.App/Program.cs@@ -23,6 +23,9 @@ internal static class Program/// <summary>How long the installer waits for a running Filee to save and exit.</summary>private static readonly TimeSpan QuitTimeout = TimeSpan.FromSeconds(20);+ /// <summary>How long a later launch waits for a Filee that is still starting to take its arguments.</summary>+ private static readonly TimeSpan ForwardTimeout = TimeSpan.FromSeconds(30);+[STAThread]public static int Main(string[] args){@@ -32,7 +35,7 @@ internal static class Programreturn RunInstallerCommand(Instance);if (!Instance.IsFirst){- Instance.ForwardToFirst(args);+ Instance.ForwardToFirst(args, ForwardTimeout);Instance.Dispose();return 0;}@@ -68,8 +71,10 @@ internal static class Programvar deadline = DateTime.UtcNow + QuitTimeout;if (!instance.IsFirst){- instance.ForwardToFirst(["--quit"]);- if (!instance.WaitForFirstToExit(QuitTimeout))+ // A Filee that is still starting takes the request once it listens; then it has the rest to exit.+ instance.ForwardToFirst(["--quit"], QuitTimeout);+ var left = deadline - DateTime.UtcNow;+ if (!instance.WaitForFirstToExit(left > TimeSpan.FromSeconds(5) ? left : TimeSpan.FromSeconds(5)))return 1;}// The mutex is released just before the process ends: wait for the end, so Filee.exe is no longer in use.diff --git a/src/Filee.App/Services/SingleInstance.cs b/src/Filee.App/Services/SingleInstance.csindex c3e45f2e..7c890056 100644--- a/src/Filee.App/Services/SingleInstance.cs+++ b/src/Filee.App/Services/SingleInstance.cs@@ -10,14 +10,16 @@ namespace Filee.App.Services;public sealed class SingleInstance : IDisposable{// string.GetHashCode() is randomized per process, so a stable hash is required for both processes to agree.- private static readonly string Name = "Filee-" + Convert.ToHexString(+ private static readonly string DefaultName = "Filee-" + Convert.ToHexString(System.Security.Cryptography.SHA256.HashData(System.Text.Encoding.UTF8.GetBytes(Environment.UserName)))[..12];private readonly Mutex _mutex;+ private readonly string _name;private readonly CancellationTokenSource _cts = new();- private SingleInstance(Mutex mutex, bool isFirst)+ private SingleInstance(Mutex mutex, string name, bool isFirst){_mutex = mutex;+ _name = name;IsFirst = isFirst;}@@ -26,14 +28,17 @@ public sealed class SingleInstance : IDisposable/// <summary>Raised on a background thread with the arguments of a later launch.</summary>public event Action<string[]>? ArgumentsReceived;- public static SingleInstance Acquire()+ public static SingleInstance Acquire() => Acquire(DefaultName);++ /// <summary>The instance under another name (tests, which must not reach a Filee that is running).</summary>+ internal static SingleInstance Acquire(string name){bool createdNew;var mutex = OperatingSystem.IsWindows()- ? new Mutex(initiallyOwned: true, @"Local\" + Name, out createdNew)- : new Mutex(initiallyOwned: true, Name,+ ? new Mutex(initiallyOwned: true, @"Local\" + name, out createdNew)+ : new Mutex(initiallyOwned: true, name,new NamedWaitHandleOptions { CurrentUserOnly = true, CurrentSessionOnly = false }, out createdNew);- return new SingleInstance(mutex, createdNew);+ return new SingleInstance(mutex, name, createdNew);}/// <summary>Starts listening for later launches (first instance only).</summary>@@ -47,7 +52,7 @@ public sealed class SingleInstance : IDisposable{try{- await using var server = new NamedPipeServerStream(Name, PipeDirection.In, 1,+ await using var server = new NamedPipeServerStream(_name, PipeDirection.In, 1,PipeTransmissionMode.Byte, PipeOptions.Asynchronous | PipeOptions.CurrentUserOnly);await server.WaitForConnectionAsync(_cts.Token);var args = await JsonSerializer.DeserializeAsync(server, AppJsonContext.Default.StringArray, _cts.Token);@@ -66,18 +71,31 @@ public sealed class SingleInstance : IDisposable});}- /// <summary>Sends this process's arguments to the first instance.</summary>- public void ForwardToFirst(string[] args)+ /// <summary>+ /// Sends this process's arguments to the first instance. It only listens once its services are up, which can take+ /// over ten seconds on a slow or busy computer: a second launch (or the installer's --quit) right after the first+ /// lost its arguments after one 3-second try. Tries again until <paramref name="wait"/> has passed; false when the+ /// first instance never answered (it is shutting down, or hung).+ /// </summary>+ public bool ForwardToFirst(string[] args, TimeSpan wait){- try- {- using var client = new NamedPipeClientStream(".", Name, PipeDirection.Out, PipeOptions.CurrentUserOnly);- client.Connect(3000);- JsonSerializer.Serialize(client, args, AppJsonContext.Default.StringArray);- }- catch (Exception ex) when (ex is IOException or TimeoutException or UnauthorizedAccessException)+ var deadline = DateTime.UtcNow + wait;+ while (true){- // The first instance is starting or shutting down; nothing sensible to do.+ var left = deadline - DateTime.UtcNow;+ try+ {+ using var client = new NamedPipeClientStream(".", _name, PipeDirection.Out, PipeOptions.CurrentUserOnly);+ client.Connect((int)Math.Clamp(left.TotalMilliseconds, 100, 1000));+ JsonSerializer.Serialize(client, args, AppJsonContext.Default.StringArray);+ return true;+ }+ catch (Exception ex) when (ex is IOException or TimeoutException or UnauthorizedAccessException)+ {+ if (DateTime.UtcNow >= deadline)+ return false;+ Thread.Sleep(100); // the pipe is busy with another launch, or not there yet+ }}}diff --git a/tests/Filee.App.Tests/SingleInstanceTests.cs b/tests/Filee.App.Tests/SingleInstanceTests.csnew file mode 100644index 00000000..dec9a087--- /dev/null+++ b/tests/Filee.App.Tests/SingleInstanceTests.cs@@ -0,0 +1,47 @@+// A later launch hands its arguments to the running Filee, also while that one is still starting.++using Filee.App.Services;++namespace Filee.App.Tests;++public class SingleInstanceTests+{+ [Fact]+ public async Task Arguments_reach_a_first_instance_that_starts_listening_late()+ {+ // The installer's --quit (or a second launch) came 8 s after Filee started, on a computer where Filee took 12 s+ // to start listening: one 3-second try gave up and the request was lost.+ var name = "Filee-test-" + Guid.NewGuid().ToString("N")[..8];+ using var first = SingleInstance.Acquire(name);+ Assert.True(first.IsFirst);+ var received = new TaskCompletionSource<string[]>();+ first.ArgumentsReceived += args => received.TrySetResult(args);++ var forward = Task.Run(() =>+ {+ using var second = SingleInstance.Acquire(name);+ Assert.False(second.IsFirst);+ return second.ForwardToFirst(["--quit"], TimeSpan.FromSeconds(15));+ }, TestContext.Current.CancellationToken);+ await Task.Delay(TimeSpan.FromSeconds(4), TestContext.Current.CancellationToken); // longer than the old single try+ first.StartListening();++ Assert.True(await forward.WaitAsync(TimeSpan.FromSeconds(15), TestContext.Current.CancellationToken));+ Assert.Equal(["--quit"], await received.Task.WaitAsync(TimeSpan.FromSeconds(5), TestContext.Current.CancellationToken));+ }++ [Fact]+ public async Task Forwarding_gives_up_when_nobody_listens()+ {+ var name = "Filee-test-" + Guid.NewGuid().ToString("N")[..8];+ using var first = SingleInstance.Acquire(name);++ var delivered = await Task.Run(() =>+ {+ using var instance = SingleInstance.Acquire(name);+ return instance.ForwardToFirst(["--quit"], TimeSpan.FromSeconds(1));+ }, TestContext.Current.CancellationToken);++ Assert.False(delivered);+ }+}