Repository navigation
Description
Description
Throwing a C++ exception from inside a catch block in a UnmanagedCallersOnly method is causing the CLR to re-execute the catch block, which easily crashes the runtime with a stack overflow and doesn't match how managed exceptions work, nor how C++ exceptions work if the method is not UnmanagedCallersOnly (using Marshal.GetFunctionPointerForDelegate), nor if we just throw a C++ exception in a catch block normally (with no native frame in between)
Reproduction Steps
- Managed method Foo calls native method Apple, passes function ptr to
[UnmanagedCallersOnly]managed method Bar - Native method Apple calls function ptr
- Managed method Bar throws managed exception, catches managed exception, calls native method (inside catch block) Banana that throws SEH exception
- Execution returns to the start of the catch block. Catch block repeats which eventually causes a stack overflow.
Also there is a sample project that demonstrates this at https://github.com/uwx/ClrBug. Build for Debug x64 and run.
Expected behavior
SEH exception crashes the program like if it was outside of a catch block.
Actual behavior
SEH exception is "caught" (execution returns to the start of the catch block.) Sample project output looks something like this
C:/Users/maxinelocal/Git/ClrBug/ManagedSide/bin/Debug/net10.0/ManagedSide.exe
Calling native CallManagedCallback...
In managed callback, calling ThrowSehException...
Caught managed exception:
System.InvalidOperationException: This is a managed exception.
at Program.ManagedCallback() in C:\Users\maxinelocal\Git\ClrBug\ManagedSide\Program.cs:line 33
Caught managed exception:
System.Runtime.InteropServices.SEHException (0x80004005): External component has thrown an exception.
at Program.ManagedCallback() in C:\Users\maxinelocal\Git\ClrBug\ManagedSide\Program.cs:line 33
(...repeat many times)
System.Runtime.InteropServices.SEHException (0x80004005): External component has thrown an exception.
at Program.ManagedCallback() in C:\Users\maxinelocal\Git\ClrBug\ManagedSide\Program.cs:line 33
Stack overflow.
at System.Text.OSEncoding.WideCharToMultiByte(Int32, Char*, Int32, Byte*, Int32)
at System.Text.OSEncoder.GetBytes(Char*, Int32, Byte*, Int32, Boolean)
at System.Text.Encoder.GetBytes(System.ReadOnlySpan`1<Char>, System.Span`1<Byte>, Boolean)
at System.IO.StreamWriter.Flush(Boolean, Boolean)
at System.IO.StreamWriter.WriteLine(System.String)
at System.IO.TextWriter+SyncTextWriter.WriteLine(System.String)
at System.Console.WriteLine(System.String)
at Program.ManagedCallback()
at Program.CallManagedCallback(Void ())
at Program.CallManagedCallback(Void ())
at Program.Main()
Process finished with exit code -1,073,741,571.
Regression?
Also happens in 9.0.12 (Win x64)
Known Workarounds
Throw C++ exceptions outside of catch blocks
Configuration
Windows 11 x64 26220.7653, .NET 10.0.102, MSBuild 18.0.5+e22287bf1. I used JetBrains Rider but I don't think that makes a difference.
Other information
No response
Metadata
Metadata
Assignees
Labels
Type
Projects
- StatusShow more project fieldsNo status
Activity
dotnet-policy-service commentedon Apr 15, 2026
Tagging subscribers to this area: @agocke
See info in area-owners.md if you want to be subscribed.
VSadov commentedon Jul 10, 2026
CC: @janvorli
Fix EH - throw from catch in UnmanagedCallersOnly (#131163)
Fix EH - throw from catch in UnmanagedCallersOnly (dotnet#131163)
[release/10.0] Fix EH - throw from catch in UnmanagedCallersOnly (#13…