AbandonedMutexException for system mutex not caugh

2019-07-20 19:26发布

问题:

I have created the following test program:

static void Main(string[] args)
{
    using (var mutex = new Mutex(false, "foobar"))
    {
        Console.WriteLine("Created mutex");
        try
        {
            try
            {
                if (!mutex.WaitOne(TimeSpan.FromSeconds(5), false))
                {
                    Console.WriteLine("Unable to acquire mutex");
                    Environment.Exit(0);
                }
            }
            catch (AbandonedMutexException)
            {
                Console.WriteLine("Mutex was abandoned");
            }

            Console.WriteLine("Acquired mutex - sleeping 10 seconds");
            Thread.Sleep(10000);
        }
        finally
        {
            mutex.ReleaseMutex();
            Console.WriteLine("Released mutex");
        }
    }

The idea is that I run the program, and while the thread is sleeping for 10 seconds, I kill the process via task manager. Next time I run the process, I'm expecting that the AbandonedMutexException would be caught on the WaitOne() call. But I'm not seeing the output "Mutex was abandoned".

The MSDN documentation mentions the following:

When a thread abandons a mutex, the exception is thrown in the next thread that acquires the mutex.

However, it looks like the OS is releasing the mutex when my process is killed (rather than another thread within the same application).

Is there a way for me to be able to detect a mutex abandoned in this manner?

回答1:

What you're observing is the correct behavior. From the documentation:

The named mutex is a system object whose lifetime is bounded by the lifetimes of the Mutex objects that represent it. The named mutex is created when the first process creates its Mutex object; in this example, the named mutex is owned by the first process that runs the program. The named mutex is destroyed when all the Mutex objects that represent it have been released.

If you run your program in two separate processes simultaneously (perhaps increasing the timeouts) and kill the first process, you can observe your expected behavior. When the first process is killed, it abandons the Mutex, which allows the second process to obtain the mutex, at which point it immediately throws an AbandonedMutexException.

If you want to determine whether your process exited gracefully, you need a different mechanism. You could accomplish this a number of ways, but one of the simplest would be to have the process create a file in a known location at startup time and delete that file after a graceful cleanup. Then you can check for that file, and if you find the file (and no running instance of your program), you know the last shutdown was not graceful or was not completed successfully.



回答2:

AbandonedMutexException is useful when there are multiple processes using a mutex. If one of the processes is killed, another process can detect this condition.

However, if the mutex is used by just one process, and that process is terminated, then the mutex is removed from the system (since there are no more references to it).



标签: c# mutex