{"record":{"id":"4936badb5c0a3941","repo":"subhra74/xdm","slug":"xdm-instance-already-running-mutex-exists-global-xdm-active-4936ba","errorCode":null,"errorMessage":"XDM instance already running, Mutex exists 'Global\\XDM_Active_Instance'","messagePattern":"XDM instance already running, Mutex exists 'Global\\\\XDM_Active_Instance'","errorType":"exception","errorClass":"InstanceAlreadyRunningException","httpStatus":null,"severity":"warning","filePath":"app/XDM/XDM.Core/SingleInstance.cs","lineNumber":21,"sourceCode":"using System.Collections.Generic;\r\nusing System.Linq;\r\nusing System.Net;\r\nusing System.Text;\r\nusing System.Threading;\r\nusing TraceLog;\r\nusing XDM.Core.BrowserMonitoring;\r\n\r\nnamespace XDM.Core\r\n{\r\n    public static class SingleInstance\r\n    {\r\n        public static Mutex GlobalMutex;\r\n        public static void Ensure()\r\n        {\r\n            try\r\n            {\r\n                using var mutex = Mutex.OpenExisting(@\"Global\\XDM_Active_Instance\");\r\n                throw new InstanceAlreadyRunningException(@\"XDM instance already running, Mutex exists 'Global\\XDM_Active_Instance'\");\r\n            }\r\n            catch (Exception ex)\r\n            {\r\n                Log.Debug(ex, \"Exception in NativeMessagingHostHandler ctor\");\r\n                if (ex is InstanceAlreadyRunningException)\r\n                {\r\n                    SendArgsToRunningInstance();\r\n                    Environment.Exit(0);\r\n                }\r\n            }\r\n            GlobalMutex = new Mutex(true, @\"Global\\XDM_Active_Instance\");\r\n        }\r\n\r\n        private static void SendArgsToRunningInstance()\r\n        {\r\n            try\r\n            {\r\n                Log.Debug(\"Sending to running instance...\");\r","sourceCodeStart":3,"sourceCodeEnd":39,"githubUrl":"https://github.com/subhra74/xdm/blob/1ca5a25aae007826c859c81bea494e7c102e1242/app/XDM/XDM.Core/SingleInstance.cs#L3-L39","documentation":"SingleInstance.Ensure checks for a system-wide named mutex Global\\XDM_Active_Instance. If the mutex already exists, another instance of the app is running and it throws InstanceAlreadyRunningException to enforce a single-instance policy (important for the native-messaging host). This is an environment/lifecycle condition, not a code defect.","triggerScenarios":"Starting the app (Ensure called at startup) while another instance holds Global\\XDM_Active_Instance; a crashed previous instance left the mutex behind (rare, since mutexes are released on process death, but possible with abandoned handles on Windows).","commonSituations":"User double-launches the app or has it in the tray/autostart; app was force-killed and a stale handle remains held by a child process; running the app in multiple Windows user sessions where 'Global' mutex collides; testing two builds on one machine.","solutions":["Check the taskbar/tray and running processes for an existing instance and close it before relaunching.","Kill leftover XDM-related processes (check Task Manager) that may still hold the mutex.","Catch InstanceAlreadyRunningException at startup and exit gracefully with a message instead of crashing.","If a stale handle is suspected after a crash, reboot or identify the holding process; consider scoping the mutex per-session (Local\\) if multi-user collision is the issue."],"exampleFix":"// before\nSingleInstance.Ensure();\n\n// after\ntry { SingleInstance.Ensure(); }\ncatch (InstanceAlreadyRunningException)\n{\n    Console.WriteLine(\"App is already running.\");\n    return; // exit gracefully\n}","handlingStrategy":"try-catch","validationCode":"Mutex m = null;\ntry { m = Mutex.OpenExisting(@\"Global\\XDM_Active_Instance\"); }\ncatch (WaitHandleCannotBeOpenedException) { /* not running */ }","typeGuard":null,"tryCatchPattern":"try { SingleInstance.Ensure(); }\ncatch (InstanceAlreadyRunningException)\n{\n    MessageBox.Show(\"Application is already running.\");\n    Environment.Exit(0);\n}","preventionTips":["Always call Ensure inside a try-catch at startup and exit gracefully","Check tray/background processes before relaunching","Consider per-session mutex names if multi-user scenarios are expected"],"tags":["single-instance","mutex","process-lifecycle","startup"],"backgroundTag":"address-already-in-use","analyzedSha":"1ca5a25aae007826c859c81bea494e7c102e1242","analyzedAt":"2026-09-13T20:37:40.968Z","contentChangedAt":"2026-09-13T20:37:40.968Z","schemaVersion":2},"datasetVersion":"2026-09-16T04:17:20.429Z"}