runtime icon indicating copy to clipboard operation
runtime copied to clipboard

SEHException after instantiating invalid ManualResetEvent

Open corruptmem opened this issue 7 years ago • 3 comments

Not sure if this counts as a bug, but it was unexpected behaviour for me.

Test case:

foreach (var _ in Enumerable.Range(1, 10000))
{            
    new ManualResetEvent(false) { Handle = new IntPtr(1234) };
    GC.Collect();
}

Run under the debugger (Windows, .NET Core 2.2) it will produce a SEHException from the GC finalizer thread:

System.Runtime.InteropServices.SEHException
  HResult=0x80004005
  Message=External component has thrown an exception.
  Stack Trace:
     Microsoft.Win32.Win32Native.CloseHandle(IntPtr handle) at
     System.Runtime.InteropServices.SafeHandle.InternalFinalize() at
     System.Runtime.InteropServices.SafeHandle.Finalize()

Although this is clearly an application code issue (in my case, it came from incorrectly serializing then subsequently deserializing a ManualResetEvent with JsonConvert), because the exception occurs on the finalizer thread some time after the incorrect code is called, it's very difficult to figure out where the code issue came from.

corruptmem avatar Mar 01 '19 13:03 corruptmem

Hi,

I would like to +1 this issue and add that 'very difficult to figure out where the issue came from' is an understatement. Since about 2 weeks I get exactly this exception which basically makes it impossible to debug. Up until now I've been unsuccessful in finding any clues to what might cause this problem. My project has a modest 180.000 lines of code and I really have no idea what to look for. Debugging the live situation with VS2019 doesn't bring me any closer to understanding what happens nor does analyzing a dump file of the situation with windbg.

'Clearly being an application code issue' suggests that my lack of knowledge is the problem, which is probably true, but I dare to argue that the situation I currently find myself in is at least partially caused by design/security choices in the CLR, given that the code runs without problems when no debugger is attached.

Is there a way to approach this problem and find the problematic code?

cheers, RIchard

rkuiper1 avatar Jun 08 '21 15:06 rkuiper1

Due to lack of recent activity, this issue has been marked as a candidate for backlog cleanup. It will be closed if no further activity occurs within 14 more days. Any new comment (by anyone, not necessarily the author) will undo this process.

This process is part of our issue cleanup automation.

This issue will now be closed since it had been marked no-recent-activity but received no further activity in the past 14 days. It is still possible to reopen or comment on the issue, but please note that the issue will be locked if it remains inactive for another 30 days.