subhra74/xdm · warning · InstanceAlreadyRunningException

XDM instance already running, Mutex exists…

Error message

XDM instance already running, Mutex exists 'Global\XDM_Active_Instance'

What it means

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.

Solutions

  1. Check the taskbar/tray and running processes for an existing instance and close it before relaunching.
  2. Kill leftover XDM-related processes (check Task Manager) that may still hold the mutex.
  3. Catch InstanceAlreadyRunningException at startup and exit gracefully with a message instead of crashing.
  4. 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.

Example fix

// before
SingleInstance.Ensure();

// after
try { SingleInstance.Ensure(); }
catch (InstanceAlreadyRunningException)
{
    Console.WriteLine("App is already running.");
    return; // exit gracefully
}
Defensive patterns

Strategy: try-catch

Validate before calling

Mutex m = null;
try { m = Mutex.OpenExisting(@"Global\XDM_Active_Instance"); }
catch (WaitHandleCannotBeOpenedException) { /* not running */ }

Try / catch

try { SingleInstance.Ensure(); }
catch (InstanceAlreadyRunningException)
{
    MessageBox.Show("Application is already running.");
    Environment.Exit(0);
}

Prevention

When it happens

Trigger: 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).

Common situations: 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.

Related errors


AI-assisted analysis of subhra74/xdm@1ca5a25aae (2026-09-13). Data as JSON: /api/errors/4936badb5c0a3941. Report an issue: GitHub.

Appendix: source

Thrown at app/XDM/XDM.Core/SingleInstance.cs:21

using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading;
using TraceLog;
using XDM.Core.BrowserMonitoring;

namespace XDM.Core
{
    public static class SingleInstance
    {
        public static Mutex GlobalMutex;
        public static void Ensure()
        {
            try
            {
                using var mutex = Mutex.OpenExisting(@"Global\XDM_Active_Instance");
                throw new InstanceAlreadyRunningException(@"XDM instance already running, Mutex exists 'Global\XDM_Active_Instance'");
            }
            catch (Exception ex)
            {
                Log.Debug(ex, "Exception in NativeMessagingHostHandler ctor");
                if (ex is InstanceAlreadyRunningException)
                {
                    SendArgsToRunningInstance();
                    Environment.Exit(0);
                }
            }
            GlobalMutex = new Mutex(true, @"Global\XDM_Active_Instance");
        }

        private static void SendArgsToRunningInstance()
        {
            try
            {
                Log.Debug("Sending to running instance...");

View on GitHub (pinned to 1ca5a25aae)