runtime: CoreCLR crash under heavy load when using custom host on Windows
Description
When using a custom host (nethost/hostfxr) on Windows, I experience intermittent crashes when my program is under heavy GC load (in my case, specifically in a code path that creates a lot of short-lived byte arrays). This was also independently observed at https://github.com/Reloaded-Project/Reloaded-II/issues/588.
Reproduction Steps
I don’t have exact reproduction steps available for this crash, as it’s not 100% consistent, but here’s what I’ve observed so far:
- This seems to only happen on custom hosts - I can’t get this to happen in a standard C# console application. In all cases I’ve observed this crash, it’s been from a C# library loaded in a C++ application using hostfxr.
- Applications seem to crash semi-consistently when making large amounts of allocations, which leads me to think it’s GC related. While I haven’t had much success trying to intentionally trigger the crash, @Sewer56 was able to reproduce a crash in their codebase fairly easily with this snippet:
for (int x = 0; x < 10000; x++)
{
var buf = new byte[1337];
GC.Collect();
}
- I was unable to get a MRE in a C++ console application using hostfxr, so there’s likely something more at play here.
I apologize for the vague reproduction steps, this bug is quite elusive and I hope to find a proper MRE soon. Hopefully there might be some ideas on how to reproduce this from a .NET team member?
Expected behavior
The runtime should not crash.
Actual behavior
The program displays the message Fatal error. Internal CLR error. (0x80131506) (COR_E_EXECUTIONENGINE) in standard output and then crashes with STATUS_ACCESS_VIOLATION dereferencing a null pointer. While I can’t upload the entire minidump here (it contains sensitive information), here’s a snippet from WinDbg (referenced source is the commit linked below):
FAULTING_SOURCE_LINE: D:\code\csharp\crashrepro\runtime\src\coreclr\gc\gc.cpp
FAULTING_SOURCE_FILE: D:\code\csharp\crashrepro\runtime\src\coreclr\gc\gc.cpp
FAULTING_SOURCE_LINE_NUMBER: 41333
FAULTING_SOURCE_CODE:
11606: inline size_t my_get_size (Object* ob)
11607: {
11608: MethodTable* mT = header(ob)->GetMethodTable();
11609:
>11610: return (mT->GetBaseSize() +
11611: (mT->HasComponentSize() ?
11612: ((size_t)((CObjectHeader*)ob)->GetNumComponents() * mT->RawGetComponentSize()) : 0));
11613: }
11614:
11615: #define size(i) my_get_size (header(i))
SYMBOL_NAME: coreclr!WKS::gc_heap::find_first_object+e8
Regression?
This started happening with .NET 9.0.5 and previously worked on .NET 9.0.4.
Known Workarounds
None, beyond pinning to 9.0.4.
Configuration
- Version: .NET 9.0.5, .NET 9.0.6, .NET 10 Preview 5
- OS: Windows 11 23H2 (22631.5189)
- Architecture: x64
Other information
A git bisect led me to https://github.com/dotnet/runtime/commit/0fb125acfb165229d55976349adf8f510b9fb3b0 as the first bad commit, which matches my testing (the commit before https://github.com/dotnet/runtime/commit/92262983b92920487bc819dcc4d6948b4e1e3b24 is good). I’ve also heard from a friend that this started with .NET 10 Preview 3, and while I haven’t tested the reproduction case myself, that lines up (https://github.com/dotnet/runtime/commit/fa3beb9521adb450863f3460d0e71bc0c5498d02).
About this issue
- Original URL
- State: open
- Created a year ago
- Reactions: 6
- Comments: 17 (13 by maintainers)
Reproduction: Start from official sample https://github.com/dotnet/samples/tree/main/core/hosting.
Change nativehost.cpp:
Change Lib.cs:
This application crashes every time on .NET 9 with the following stack trace. It does not crash on .NET 8
Ok we will port https://github.com/dotnet/runtime/pull/115546 to 9. @NotNite, can you please confirm that your test doesnt fail on latest .NET 10 previews?
That actually makes a lot of sense in context, also regarding why it was hard to reproduce before.
In some of our projects which were affected, people loading the code were doing so via DLL Injection, i.e. using CreateRemoteThread under the hood. (inject a native bootstrapper which then loads .NET)
In that case, the spawned thread would exit after everything was loaded, triggering the bug; as opposed to say, something closer to apphost, where everything kicks off from the main thread.
If possible, the best way to debug this is using time-travel debugger built into windbg: https://learn.microsoft.com/en-us/windows-hardware/drivers/debuggercmds/time-travel-debugging-overview
Once you collect the time travel trace, set a write breakpoint on the thread allocation context that’s gone and run back to see where that allocation context got created.
The call stack leading up to the crash looks like this:
Given that https://github.com/dotnet/runtime/commit/0fb125acfb165229d55976349adf8f510b9fb3b0 is responsible for the change in behavior, and that the call stack is extremely similar to #110442, it suggests the cause is similar to https://github.com/dotnet/winforms/pull/12631: the thread exit notification is being lost, leaving invalid entries in the thread store.
Do you have tips on debugging where this happens? It’s still unclear to us whether the native application is using an FLS callback as in the COM scenario.
That was actually @nenkai , after I asked them to put some GC pressure, as the crash point around Windows Crash Dumps was around the GC in native parts of the runtime. It doesn’t cause the issue, but just increases probability; at least from what I’ve heard so far. Testing on my own end has been limited; after all, I needed to spend a full weekend getting thousands of users to an earlier runtime; writing a bunch of code to check, install, etc. for an emergency workaround.
It’s been hard to get a MRE for this; but essentially, from our observations so far it was:
That on itself seems fine, but once you get additional Threads and AssemblyLoadContext(s) involved; things just fall apart. Having another peek, it’s more likely thread related (edit: Seems like it’s an issue deallocating on the finalizer thread?), but we’ve not got around to finding out the details yet.