From 736970deaf13b14ed7141558faee56f7200ab62a Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Sun, 7 Dec 2025 00:13:45 +0000 Subject: Add graceful handling for Mutex creation failures in Program.cs Co-authored-by: jpdillingham <17145758+jpdillingham@users.noreply.github.com> --- src/slskd/Program.cs | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-) (limited to 'src/slskd/Program.cs') diff --git a/src/slskd/Program.cs b/src/slskd/Program.cs index 6b50aedb..c51d07d8 100644 --- a/src/slskd/Program.cs +++ b/src/slskd/Program.cs @@ -237,7 +237,7 @@ namespace slskd private static IConfigurationRoot Configuration { get; set; } private static OptionsAtStartup OptionsAtStartup { get; } = new OptionsAtStartup(); private static ILogger Log { get; set; } = new ConsoleWriteLineLogger(); - private static Mutex Mutex { get; } = new Mutex(initiallyOwned: true, Compute.Sha256Hash(AppName)); + private static Mutex Mutex { get; set; } private static IDisposable DotNetRuntimeStats { get; set; } [Argument('g', "generate-cert", "generate X509 certificate and password for HTTPs")] @@ -335,10 +335,21 @@ namespace slskd // the application isn't being run in command mode. check the mutex to ensure // only one long-running instance. - if (!Mutex.WaitOne(millisecondsTimeout: 0, exitContext: false)) + try { - Log.Fatal($"An instance of {AppName} is already running"); - return; + Mutex = new Mutex(initiallyOwned: true, Compute.Sha256Hash(AppName)); + + if (!Mutex.WaitOne(millisecondsTimeout: 0, exitContext: false)) + { + Log.Fatal($"An instance of {AppName} is already running"); + return; + } + } + catch (Exception ex) + { + Log.Fatal($"Failed to acquire the application singleton mutex: {ex.Message}"); + Log.Warning("This can happen when running in a restricted environment (e.g., Kubernetes with readOnlyRootFilesystem)"); + Log.Warning("The application will continue, but multiple instances may be able to run simultaneously"); } // derive the application directory value and defaults that are dependent upon it -- cgit v1.2.3 From 1d09aef10a03fb4263a0e312c979d5a153fc9b31 Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Sun, 7 Dec 2025 00:16:04 +0000 Subject: Address code review feedback: use specific exceptions and dispose Mutex Co-authored-by: jpdillingham <17145758+jpdillingham@users.noreply.github.com> --- src/slskd/Program.cs | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) (limited to 'src/slskd/Program.cs') diff --git a/src/slskd/Program.cs b/src/slskd/Program.cs index c51d07d8..29783216 100644 --- a/src/slskd/Program.cs +++ b/src/slskd/Program.cs @@ -345,12 +345,18 @@ namespace slskd return; } } - catch (Exception ex) + catch (IOException ex) { Log.Fatal($"Failed to acquire the application singleton mutex: {ex.Message}"); Log.Warning("This can happen when running in a restricted environment (e.g., Kubernetes with readOnlyRootFilesystem)"); Log.Warning("The application will continue, but multiple instances may be able to run simultaneously"); } + catch (UnauthorizedAccessException ex) + { + Log.Fatal($"Failed to acquire the application singleton mutex: {ex.Message}"); + Log.Warning("This can happen when running with insufficient permissions"); + Log.Warning("The application will continue, but multiple instances may be able to run simultaneously"); + } // derive the application directory value and defaults that are dependent upon it AppDirectory ??= DefaultAppDirectory; @@ -551,6 +557,7 @@ namespace slskd } finally { + Mutex?.Dispose(); Serilog.Log.CloseAndFlush(); } } -- cgit v1.2.3 From 1f98acd90622a66f0ec4a88f5c0c60c8a1cdd332 Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Sun, 7 Dec 2025 00:17:24 +0000 Subject: Use Warning log level instead of Fatal for Mutex creation failures Co-authored-by: jpdillingham <17145758+jpdillingham@users.noreply.github.com> --- src/slskd/Program.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'src/slskd/Program.cs') diff --git a/src/slskd/Program.cs b/src/slskd/Program.cs index 29783216..098cc324 100644 --- a/src/slskd/Program.cs +++ b/src/slskd/Program.cs @@ -347,13 +347,13 @@ namespace slskd } catch (IOException ex) { - Log.Fatal($"Failed to acquire the application singleton mutex: {ex.Message}"); + Log.Warning($"Failed to acquire the application singleton mutex: {ex.Message}"); Log.Warning("This can happen when running in a restricted environment (e.g., Kubernetes with readOnlyRootFilesystem)"); Log.Warning("The application will continue, but multiple instances may be able to run simultaneously"); } catch (UnauthorizedAccessException ex) { - Log.Fatal($"Failed to acquire the application singleton mutex: {ex.Message}"); + Log.Warning($"Failed to acquire the application singleton mutex: {ex.Message}"); Log.Warning("This can happen when running with insufficient permissions"); Log.Warning("The application will continue, but multiple instances may be able to run simultaneously"); } -- cgit v1.2.3 From 4e02b95eeac1cdd3d49f0ca0177f9d59a76e9ce4 Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Sun, 7 Dec 2025 00:19:04 +0000 Subject: Add handling for AbandonedMutexException from WaitOne call Co-authored-by: jpdillingham <17145758+jpdillingham@users.noreply.github.com> --- src/slskd/Program.cs | 7 +++++++ 1 file changed, 7 insertions(+) (limited to 'src/slskd/Program.cs') diff --git a/src/slskd/Program.cs b/src/slskd/Program.cs index 098cc324..bafac8b1 100644 --- a/src/slskd/Program.cs +++ b/src/slskd/Program.cs @@ -357,6 +357,13 @@ namespace slskd Log.Warning("This can happen when running with insufficient permissions"); Log.Warning("The application will continue, but multiple instances may be able to run simultaneously"); } + catch (AbandonedMutexException ex) + { + Log.Warning($"Acquired an abandoned mutex: {ex.Message}"); + Log.Warning("A previous instance may have terminated unexpectedly"); + + // The mutex is now owned by this thread, so we can continue + } // derive the application directory value and defaults that are dependent upon it AppDirectory ??= DefaultAppDirectory; -- cgit v1.2.3 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 From 4792270460293b4810e2e2a34f8deede39008502 Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Sun, 7 Dec 2025 00:22:19 +0000 Subject: Use explicit Exception type in Mutex disposal catch block Co-authored-by: jpdillingham <17145758+jpdillingham@users.noreply.github.com> --- src/slskd/Program.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src/slskd/Program.cs') diff --git a/src/slskd/Program.cs b/src/slskd/Program.cs index 85cc4a3a..3bd0798b 100644 --- a/src/slskd/Program.cs +++ b/src/slskd/Program.cs @@ -569,7 +569,7 @@ namespace slskd { Mutex?.Dispose(); } - catch + catch (Exception) { // Ignore disposal errors to prevent masking other exceptions } -- cgit v1.2.3 From e08479ee8d91952fee13b009f861912a92a2dbae Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Sat, 13 Dec 2025 17:15:16 -0600 Subject: tweak --- src/slskd/Program.cs | 25 +++++++++---------------- 1 file changed, 9 insertions(+), 16 deletions(-) (limited to 'src/slskd/Program.cs') diff --git a/src/slskd/Program.cs b/src/slskd/Program.cs index 3bd0798b..db4f7c6c 100644 --- a/src/slskd/Program.cs +++ b/src/slskd/Program.cs @@ -23,8 +23,6 @@ namespace slskd using System.Collections.Generic; using System.ComponentModel; using System.ComponentModel.DataAnnotations; - using System.Diagnostics; - using System.IO; using System.Linq; using System.Net; @@ -337,10 +335,9 @@ namespace slskd // only one long-running instance. try { - bool createdNew; - Mutex = new Mutex(initiallyOwned: true, Compute.Sha256Hash(AppName), out createdNew); + Mutex = new Mutex(initiallyOwned: true, Compute.Sha256Hash(AppName), out bool created); - if (!createdNew) + if (!created) { Log.Fatal($"An instance of {AppName} is already running"); return; @@ -348,22 +345,18 @@ namespace slskd } catch (IOException ex) { - Log.Warning($"Failed to acquire the application singleton mutex: {ex.Message}"); - Log.Warning("This can happen when running in a restricted environment (e.g., Kubernetes with readOnlyRootFilesystem)"); - Log.Warning("The application will continue, but multiple instances may be able to run simultaneously"); + Log.Fatal($"I/O exception attempting to acquire the application singleton mutex; this can happen when running in a restricted environment (such as a read-only filesystem or container). Exception: {ex.Message}"); + return; } catch (UnauthorizedAccessException ex) { - Log.Warning($"Failed to acquire the application singleton mutex: {ex.Message}"); - Log.Warning("This can happen when running with insufficient permissions"); - Log.Warning("The application will continue, but multiple instances may be able to run simultaneously"); + Log.Fatal($"Unauthorized access attempting to acquire the application singleton mutex; this can happen when running with insuffucent permissions. Exception: {ex.Message}"); + return; } - catch (AbandonedMutexException ex) + catch (Exception ex) { - Log.Warning($"Acquired an abandoned mutex: {ex.Message}"); - Log.Warning("A previous instance may have terminated unexpectedly"); - - // The mutex is now owned by this thread, so we can continue + Log.Fatal($"Failed to acquire the application singleton mutex: {ex.Message}"); + return; } // derive the application directory value and defaults that are dependent upon it -- cgit v1.2.3