Gracefully handling corrupted state exceptions in .NET Core

折月煮酒 提交于 2019-12-06 09:24:46

I ran your code and the exception being raised is ExecutionEngineException. This is a fatal error in the runtime, meaning runtime cannot execute your program further. You can't catch this exception for a good reason: you cannot handle it. There is nothing your code could do to "fix" the runtime and continue operating.

From Microsoft docu: The exception that is thrown when there is an internal error in the execution engine of the common language runtime. And: This type previously indicated an unspecified fatal error in the runtime. The runtime no longer raises this exception so this type is obsolete.

Some background I've been digging up on the subject, in chronological order...

dotnet/coreclr Issue #9045: Strip corrupted state exceptions handling

dotnet/coreclr PR #10957: Do not honor attribute HandleProcessCorruptedStateExceptions

dotnet/coreclr Issue #19192: Unable to catch in managed code any user exception thrown from native Linux code

I'm SOL currently. Have to create an unmanaged wrapper of sorts to catch external CSEs.

You can try a few more options, specifically FailFastOnCorruptedStateException combined with legacyCorruptedStateExceptionsPolicy.

The only thing about this remains in .net core is legacyCorruptedStateExceptionsPolicy behavior.

To enable this, you must set COMPlus_legacyCorruptedStateExceptionsPolicy environment variable to 1 before your process start. But, in .net core it will catch far less corrupted state exceptions than in .net, due to decisions made during .net porting :(

For example, if you replace in your code Marshal.StructureToPtr(42, (IntPtr)42, true); with Marshal.ReadInt32((IntPtr)42); (and set the environment variable) AVE will be generated and captured successfully.

You can read details about this problem here.

Overall about corrupted state exceptions in .net core:

  1. To catch some of them set COMPlus_legacyCorruptedStateExceptionsPolicy environment variable to 1 before your process start.
  2. If you get CSE from native calls. Try to move exception handling there (if you have the source), or try unmanaged wrapper as TylerY86 suggested.
  3. Write WatchDog process to check your app crashes, if it's important. Then look to Event Log and analyzing the crash dumps...
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!