Skip to content

armgit / Filee

Public Imported from GitHub
0

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.

Stop the input hook before disposing it, so quitting doesn't stall the mouse

KnifeLemon6d2786fddb40
diff --git a/src/Filee.App/Services/Triggers/TriggerService.cs b/src/Filee.App/Services/Triggers/TriggerService.cs
index 7032470f..5e25f2a9 100644
--- a/src/Filee.App/Services/Triggers/TriggerService.cs
+++ b/src/Filee.App/Services/Triggers/TriggerService.cs
@@ -330,6 +330,28 @@ public sealed class TriggerService : IDisposable
_permissionWatch?.Dispose();
_permissionWatch = null;
try
+ {
+ // Stop the native hook and let its Run thread end before disposing (#358): SharpHook's Dispose closes the
+ // event queue first and stops the hook after, so a mouse move in between throws on the hook thread, and
+ // Windows then waits for that stuck low-level hook on every mouse move until the Join below gives up.
+ if (_hook is { IsDisposed: false, IsRunning: true })
+ _hook.Stop();
+ }
+ catch (HookException ex)
+ {
+ _log.LogWarning(ex, "Could not stop the global input hook");
+ Error = ex.Message;
+ NotifyStatus();
+ return; // Keep ownership: starting a second native hook would corrupt the first one.
+ }
+ if (_hookThread is { IsAlive: true } stopping && !stopping.Join(TimeSpan.FromSeconds(5)))
+ {
+ Error = "The previous global input hook did not finish stopping. Retry after it exits.";
+ _log.LogWarning("Global input hook shutdown timed out; keeping the previous hook to prevent overlap");
+ NotifyStatus();
+ return;
+ }
+ try
{
_hook?.Dispose();
}
diff --git a/tests/Filee.App.Tests/TriggerServiceLifecycleTests.cs b/tests/Filee.App.Tests/TriggerServiceLifecycleTests.cs
index 67f618c4..2fbed49b 100644
--- a/tests/Filee.App.Tests/TriggerServiceLifecycleTests.cs
+++ b/tests/Filee.App.Tests/TriggerServiceLifecycleTests.cs
@@ -45,6 +45,31 @@ public sealed class TriggerServiceLifecycleTests
Assert.True(provider.HasCallback);
}
+ [AvaloniaFact]
+ public async Task Input_arriving_while_the_hook_stops_is_not_refused()
+ {
+ // #358: quitting from the tray, the mouse keeps moving while the native hook stops. SharpHook's Dispose closes
+ // its event queue before stopping the hook, so such a move threw on the hook thread and stalled the cursor.
+ var provider = new DelayedProvider();
+ using var service = Create(provider, () => { });
+ service.Start();
+ Assert.True(SpinWait.SpinUntil(() => provider.Runs == 1, 2000));
+ var cancellation = TestContext.Current.CancellationToken;
+ var stopping = Task.Factory.StartNew(service.Dispose, cancellation, TaskCreationOptions.LongRunning, TaskScheduler.Default);
+ try
+ {
+ Assert.True(provider.StopRequested.Wait(2000, cancellation));
+ provider.PostMouse(EventType.MouseMoved, 10, 20, EventMask.None);
+ }
+ finally
+ {
+ provider.AllowExit.Set();
+ await stopping.WaitAsync(TimeSpan.FromSeconds(2), cancellation);
+ }
+ Assert.Null(service.Error);
+ Assert.False(service.IsRunning);
+ }
+
[AvaloniaFact]
public void Repeated_retries_keep_delivering_input()
{