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?
What you’re observing is the correct behavior. From the documentation:
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.