AbandonedMutexException doesn't make sense.

D

dvestal

I have an app that grabs a named mutex as its first action. This is
used to ensure it's the only instance of the app running.

Sometimes the attempt to grab the mutex results in an
AbandonedMutexException, even though at that point there's only a
single thread, and the mutex hasn't even been grabbed yet. How is
this possible, and how do I recover from it?

Super-simplified code below:

using System;
using System.Diagnostics;
using System.Threading;

namespace TSSG.DeviceTesting
{
public class TestEngine
{
[STAThread]
static public void Main(string[] commandLineArgs)
{
TestEngine te = new TestEngine();
te.RunApplication(commandLineArgs);
}

public void RunApplication(string[] commandLineArgs)
{
Mutex mutex = null;
bool gotMutex = false;

try
{
// Ensure that this is the only
// instance of the app running.
mutex = new Mutex(
false,
"Opsys Engine Exclusivity");

// Sometimes this throws an AbandonedMutexException,
// despite being the first thing the app does.
// It seems to follow the operator killing the
// previous instance of the application using
// task manager. Is this possible? What's the
// best way to recover?
gotMutex = mutex.WaitOne(0, false);

if(!gotMutex)
{
MessageBox.Show(
"This application is already running.");
return;
}

// After this, several threads are created.
// Drivers are loaded, hardware is interacted
// with, etc. Crashes can occur anywhere.
// BUT...can this lead to an AbandonedMutex
// Exception for new invocations of the app?
DoLotsOfActualWork();
}
catch(Exception e)
{
}
finally
{
if(gotMutex && null != mutex)
{
mutex.ReleaseMutex();
}
}
}
}
}
 
A

Arne Vajhøj

I have an app that grabs a named mutex as its first action. This is
used to ensure it's the only instance of the app running.

Sometimes the attempt to grab the mutex results in an
AbandonedMutexException, even though at that point there's only a
single thread, and the mutex hasn't even been grabbed yet. How is
this possible, and how do I recover from it?

Super-simplified code below:
Mutex mutex = null;
bool gotMutex = false;

try
{
// Ensure that this is the only
// instance of the app running.
mutex = new Mutex(
false,
"Opsys Engine Exclusivity");

// Sometimes this throws an AbandonedMutexException,
// despite being the first thing the app does.
// It seems to follow the operator killing the
// previous instance of the application using
// task manager. Is this possible? What's the
// best way to recover?

http://msdn.microsoft.com/en-us/library/system.threading.mutex.aspx says:

<quote>
Mutexes are of two types: local mutexes, which are unnamed, and named
system mutexes. A local mutex exists only within your process.
....
Named system mutexes are visible throughout the operating system, and
can be used to synchronize the activities of processes. You can create a
Mutex object that represents a named system mutex by using a constructor
that accepts a name.
</quote>

<quote>
If a thread terminates while owning a mutex, the mutex is said to be
abandoned. The state of the mutex is set to signaled, and the next
waiting thread gets ownership.
....
In the case of a system-wide mutex, an abandoned mutex might indicate
that an application has been terminated abruptly (for example, by using
Windows Task Manager).
</quote>

Arne
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top