aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorcopilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com>2025-12-07 00:13:45 +0000
committercopilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com>2025-12-07 00:13:45 +0000
commit736970deaf13b14ed7141558faee56f7200ab62a (patch)
tree72776ada61fb7b41a38a06b852033d82802b3ed5 /src
parent13359328171b06ae2af9a47e25164b38f43cda54 (diff)
downloadslskd-736970deaf13b14ed7141558faee56f7200ab62a.tar.gz
Add graceful handling for Mutex creation failures in Program.cs
Co-authored-by: jpdillingham <17145758+jpdillingham@users.noreply.github.com>
Diffstat (limited to 'src')
-rw-r--r--src/slskd/Program.cs19
1 files changed, 15 insertions, 4 deletions
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