{"record":{"id":"7aad8ed34593e1d0","repo":"subhra74/xdm","slug":"xdm-instance-already-running-mutex-exists-global-xdm-active","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":"info","filePath":"app/XDM/XDM.Core/BrowserMonitoring/NativeMessagingHostHandler.cs","lineNumber":31,"sourceCode":"#endif\r\nusing TraceLog;\r\n\r\nnamespace XDM.Core.BrowserMonitoring\r\n{\r\n    public class NativeMessagingHostHandler : IDisposable\r\n    {\r\n        private int MaxPipeInstance = 254;\r\n        private static readonly string PipeName = \"XDM_Ipc_Browser_Monitoring_Pipe\";\r\n        private List<NativeMessagingHostChannel> connectedChannels = new();\r\n        private static Mutex globalMutex;\r\n        private Thread listenerThread;\r\n\r\n        public static void EnsureSingleInstance()\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                    var args = Environment.GetCommandLineArgs().Skip(1);\r\n                    if (args.Count() > 0)\r\n                    {\r\n                        Log.Debug(ex, \"Sending args to running instance\");\r\n                        SendArgsToRunningInstance(args);\r\n                        Environment.Exit(0);\r\n                    }\r\n                    throw;\r\n                }\r\n            }\r\n            globalMutex = new Mutex(true, @\"Global\\XDM_Active_Instance\");\r\n        }\r","sourceCodeStart":13,"sourceCodeEnd":49,"githubUrl":"https://github.com/subhra74/xdm/blob/1ca5a25aae007826c859c81bea494e7c102e1242/app/XDM/XDM.Core/BrowserMonitoring/NativeMessagingHostHandler.cs#L13-L49","documentation":"XDM enforces a single active instance across the machine using a named system-wide mutex 'Global\\XDM_Active_Instance'. NativeMessagingHostHandler.EnsureSingleInstance opens this mutex; if it exists, another XDM instance is already running and it throws InstanceAlreadyRunningException. The exception is immediately caught and logged at Debug level, so it functions as an instance-detection signal rather than a fatal error.","triggerScenarios":"Calling NativeMessagingHostHandler.EnsureSingleInstance (e.g. from the native messaging host startup path) while another XDM process already holds the 'Global\\XDM_Active_Instance' mutex.","commonSituations":"Launching the browser native messaging host while the main XDM app is running; a crashed XDM process that failed to release the mutex (rare, mutexes die with the process); running XDM under multiple Windows sessions where the Global mutex is shared.","solutions":["Close the existing XDM instance (check system tray and Task Manager for XDM processes) before starting the native messaging host or a second copy","If no visible instance exists, kill orphaned XDM processes in Task Manager; the Global mutex is released when the owning process exits","If you must run the host independently, ensure your code catches InstanceAlreadyRunningException from EnsureSingleInstance and treats it as 'instance detected', not a failure","Reboot as a last resort if a zombie process holds the mutex and cannot be identified"],"exampleFix":"// before\nEnsureSingleInstance(); // throws if XDM already running\n// after\ntry\n{\n    EnsureSingleInstance();\n}\ncatch (InstanceAlreadyRunningException)\n{\n    Log.Info(\"XDM main instance already running; exiting host gracefully\");\n    return;\n}","handlingStrategy":"try-catch","validationCode":"// Probe for an existing instance before performing work\nMutex existing;\nvar probe = Mutex.TryOpenExisting(@\"Global\\XDM_Active_Instance\", out existing);\nif (probe) { /* another instance is running */ }","typeGuard":null,"tryCatchPattern":"try\n{\n    NativeMessagingHostHandler.EnsureSingleInstance();\n}\ncatch (InstanceAlreadyRunningException ex)\n{\n    Log.Info(\"XDM already running: {0}\", ex.Message);\n    return; // exit gracefully\n}","preventionTips":["Always call EnsureSingleInstance inside try/catch and treat InstanceAlreadyRunningException as a normal detection signal","Check for running XDM processes (tray icon / Task Manager) before launching the native messaging host","Never manually delete or spoof the Global mutex; rely on process exit to release it"],"tags":["single-instance","mutex","process-lifecycle","windows"],"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"}