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.

--quit no longer waits for a process that has already ended

KnifeLemonba051bc27318
diff --git a/.github/workflows/portable.yml b/.github/workflows/portable.yml
index ea2589a2..fe0fc0f2 100644
--- a/.github/workflows/portable.yml
+++ b/.github/workflows/portable.yml
@@ -150,7 +150,18 @@ jobs:
if process.poll() is not None:
raise RuntimeError('The desktop app exited after a second start.')
quit_code = 'import os, sys; os.setsid(); os.execv(sys.argv[1], [sys.argv[1], "--quit"])'
- subprocess.run([sys.executable, '-c', quit_code, app], check=True, timeout=30)
+ # Reap Filee as soon as it ends while --quit waits for it, as launchd or a shell would: a process
+ # that has ended but was not reaped by its parent (this script) still counts as running.
+ quitter = subprocess.Popen([sys.executable, '-c', quit_code, app])
+ quit_deadline = time.monotonic() + 30
+ while quitter.poll() is None:
+ process.poll()
+ if time.monotonic() > quit_deadline:
+ quitter.kill()
+ raise RuntimeError('filee --quit did not finish.')
+ time.sleep(0.05)
+ if quitter.returncode != 0:
+ raise RuntimeError(f'filee --quit exited with {quitter.returncode}.')
if process.wait(timeout=15) != 0:
raise RuntimeError('The desktop app did not exit cleanly.')
except BaseException:
diff --git a/src/Filee.App/Program.cs b/src/Filee.App/Program.cs
index 0d83b53b..45d9e23c 100644
--- a/src/Filee.App/Program.cs
+++ b/src/Filee.App/Program.cs
@@ -90,28 +90,66 @@ internal static class Program
{
if (Environment.ProcessPath is not { } exe)
return true;
- foreach (var process in Process.GetProcessesByName(Path.GetFileNameWithoutExtension(exe)))
+ if (!OperatingSystem.IsWindows())
+ {
+ // Process.WaitForExit counts a process that has ended but not been reaped by its parent (a zombie) as still
+ // running, so it would wait for the deadline. Such a process no longer has an executable path: look again
+ // until no other process runs this exe.
+ while (true)
+ {
+ var others = OtherProcessesOfThisExe(exe);
+ var running = others.Count > 0;
+ others.ForEach(p => p.Dispose());
+ if (!running)
+ return true;
+ if (DateTime.UtcNow >= deadline)
+ return false;
+ Thread.Sleep(100);
+ }
+ }
+ foreach (var process in OtherProcessesOfThisExe(exe))
{
using (process)
{
try
{
- if (process.Id == Environment.ProcessId
- || !string.Equals(process.MainModule?.FileName, exe, Filee.Core.Platform.FileSystemPaths.Comparison))
- continue;
var left = deadline - DateTime.UtcNow;
if (left <= TimeSpan.Zero || !process.WaitForExit(left))
return false;
}
catch (Exception ex) when (ex is System.ComponentModel.Win32Exception or InvalidOperationException)
{
- // Exited meanwhile, or another user's process we may not inspect.
+ // Exited meanwhile.
}
}
}
return true;
}
+ /// <summary>Other running processes started from <paramref name="exe"/> (the caller disposes them).</summary>
+ private static List<Process> OtherProcessesOfThisExe(string exe)
+ {
+ var found = new List<Process>();
+ foreach (var process in Process.GetProcessesByName(Path.GetFileNameWithoutExtension(exe)))
+ {
+ try
+ {
+ if (process.Id != Environment.ProcessId
+ && string.Equals(process.MainModule?.FileName, exe, Filee.Core.Platform.FileSystemPaths.Comparison))
+ {
+ found.Add(process);
+ continue;
+ }
+ }
+ catch (Exception ex) when (ex is System.ComponentModel.Win32Exception or InvalidOperationException)
+ {
+ // Exited meanwhile, or another user's process we may not inspect.
+ }
+ process.Dispose();
+ }
+ return found;
+ }
+
/// <summary>Also used by the XAML previewer.</summary>
public static AppBuilder BuildAvaloniaApp() =>
AppBuilder.Configure<App>()
diff --git a/src/Filee.App/Services/Triggers/TriggerService.cs b/src/Filee.App/Services/Triggers/TriggerService.cs
index 01049918..0f3ea557 100644
--- a/src/Filee.App/Services/Triggers/TriggerService.cs
+++ b/src/Filee.App/Services/Triggers/TriggerService.cs
@@ -89,7 +89,8 @@ public sealed class TriggerService : IDisposable
Error = null;
try
{
- _hook = new EventLoopGlobalHook();
+ // Background threads: a hook that fails to stop can never keep Filee running after it quits.
+ _hook = new EventLoopGlobalHook(useBackgroundThreadForEventLoop: true);
_hook.HookEnabled += (_, _) => { _log.LogInformation("Global input hook started"); NotifyStatus(); };
_hook.HookDisabled += (_, _) => { _log.LogInformation("Global input hook stopped"); NotifyStatus(); };
_hook.MousePressed += (_, e) =>
@@ -102,7 +103,7 @@ public sealed class TriggerService : IDisposable
_hook.MouseDragged += (_, e) => OnMove(e);
_hook.MouseMoved += (_, e) => OnMove(e);
_hook.KeyPressed += (_, e) => _detector.KeyDown(KeyName(e.Data.KeyCode), Map(e.RawEvent.Mask));
- _ = _hook.RunAsync().ContinueWith(t =>
+ _ = _hook.RunAsync(useBackgroundThread: true).ContinueWith(t =>
{
// e.g. macOS without Accessibility permission. Drop zone and context menu keep working.
if (t.Exception is not null)
@@ -204,7 +205,15 @@ public sealed class TriggerService : IDisposable
{
_permissionWatch?.Dispose();
_permissionWatch = null;
- _hook?.Dispose();
+ try
+ {
+ _hook?.Dispose();
+ }
+ catch (HookException ex)
+ {
+ // Quitting goes on: the hook runs on background threads, which end with the process.
+ _log.LogWarning(ex, "Could not stop the global input hook");
+ }
_hook = null;
}
}