Search code examples
c#multithreading.net-3.5deadlockmanualresetevent

What could cause EventWaitHandle.Set() to block the current thread?


I am invoking the Set method on an instance of a ManualResetEvent, and it is occasionally deadlocking. I can't find anything in the documentation to indicate that this is a blocking method. What could cause MRE.Set to block?

Stack Trace:

[Managed to Native Transition]
mscorlib.dll!System.Threading.EventWaitHandle.Set() + 0xe bytes
MyCode.StopAll(bool force) Line 179 + 0xd bytes
MyCode.CalcCheckThread() Line 250 + 0xb bytes
mscorlib.dll!System.Threading.ThreadHelper.ThreadStart_Context(object state) + 0x66 bytes
mscorlib.dll!System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext executionContext, System.Threading.ContextCallback callback, object state) + 0x6f bytes
mscorlib.dll!System.Threading.ThreadHelper.ThreadStart() + 0x44 bytes



private static void StopAll(bool force)
{
    if( !force )
        LogHelper.SendAllCloseState(logger);

    _forcablyExit = force;
    _running = false;
    _stopWait.Set();  // This line appears to be blocking
}

Solution

  • We've tracked down the source of this problem with the help of our friends at Microsoft Developer Support.

    EventWaitHandle.Set() enters a critical block section, and can be blocked if native code enters a critical block and never releases it. This is happening with a very old 3rd-party library that we are using, and can't be easily replaced/updated.