From 0a66469fd81446b5d54becd049f8da90898cc85c Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Sun, 7 Dec 2025 00:21:06 +0000 Subject: Fix Mutex logic and protect disposal from throwing exceptions Co-authored-by: jpdillingham <17145758+jpdillingham@users.noreply.github.com> --- src/slskd/Program.cs | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) (limited to 'src/slskd/Program.cs') diff --git a/src/slskd/Program.cs b/src/slskd/Program.cs index bafac8b1..85cc4a3a 100644 --- a/src/slskd/Program.cs +++ b/src/slskd/Program.cs @@ -337,9 +337,10 @@ namespace slskd // only one long-running instance. try { - Mutex = new Mutex(initiallyOwned: true, Compute.Sha256Hash(AppName)); + bool createdNew; + Mutex = new Mutex(initiallyOwned: true, Compute.Sha256Hash(AppName), out createdNew); - if (!Mutex.WaitOne(millisecondsTimeout: 0, exitContext: false)) + if (!createdNew) { Log.Fatal($"An instance of {AppName} is already running"); return; @@ -564,7 +565,15 @@ namespace slskd } finally { - Mutex?.Dispose(); + try + { + Mutex?.Dispose(); + } + catch + { + // Ignore disposal errors to prevent masking other exceptions + } + Serilog.Log.CloseAndFlush(); } } -- cgit v1.2.3