Repository navigation
Description
Description
A .NET 10 process on Linux can hang permanently when the DOTNET_PerfMapEnabled code path is active (env var or runtime-enabled via the Diagnostics Server IPC, e.g. dotnet-trace --enable-perfmap). The hang is a three-way deadlock between the GC suspension machinery, CodeFragmentHeap::m_Lock, and PerfMap::s_csPerfMap, triggered while a virtual-call-stub resolve worker is generating a new resolve stub.
The two locks are taken in the wrong order with respect to GC-mode handling:
CodeFragmentHeap::m_Lockis constructed withCRST_UNSAFE_ANYMODE, so acquiring it does not toggle the calling thread to preemptive GC.PerfMap::s_csPerfMapis constructed withCRST_DEFAULT(no flags), so acquiring it does toggle a cooperative thread to preemptive viaThread::RareDisablePreemptiveGC, which can block while a GC suspension is in progress.
The result: a cooperative thread holding m_Lock calls into PerfMap, gets stuck inside RareDisablePreemptiveGC waiting for the GC to finish — but the GC is waiting for that same thread to reach a safe point, and every other thread that needs to allocate a stub is queued behind the held m_Lock.
Reproduction Steps
Likely synthetic repro recipe:
- Run any .NET 10 Linux x64 workload with PerfMap enabled (
DOTNET_PerfMapEnabled=1, or attach withdotnet-trace collect -p <pid> --enable-perfmap). - Drive heavy virtual-call-stub creation (large numbers of polymorphic interface call sites being warmed up concurrently) while also driving allocation that forces frequent GCs.
- Eventually a cooperative thread is suspended inside
PerfMap::LogStubs->s_csPerfMapwhile holdingCodeFragmentHeap::m_Lock, and the process deadlocks.
Crash dumps available to debug this directly though.
Expected behavior
PerfMap logging on a stub-allocation path must not be able to deadlock with the GC.
Not sure the actual fix, as CodeFragmentHeap::m_Lock was correctly "default" leaving us in coop mode because this was meant as a quick call, and the work behind PerfMap::s_csPerfMap is heavyweight, meaning we should let the GC run. This change makes ResolveWorkerAsmStub a more heavyweight function which may need to swap to preemptive mode, or possibly the calls into PerfMap need to be lighter weight. Or maybe I'm overthinking it.
Actual behavior
The GC thread is suspending the world and waiting on a cooperative thread to reach a safe point.
Thread 60 is preemptive, was about to acquire s_csPerfMap from inside PerfMap::LogStubs. The default-flagged Crst is toggling it to preemptive via RareDisablePreemptiveGC, where it now sits indefinitely because the GC is already trying to suspend it:
02 libcoreclr!GCEvent::Impl::Wait+0xd2 unix/events.cpp:179
03 libcoreclr!Thread::RareDisablePreemptiveGC+0x14e threadsuspend.cpp:2223
04 libcoreclr!CrstBase::AcquireLock+0xc crst.h:174
05 libcoreclr!CrstBase::CrstHolder::CrstHolder+0xc crosscomp.h:349
06 libcoreclr!PerfMap::LogStubs+0x19a perfmap.cpp:462
07 libcoreclr!CodeFragmentHeap::RealAllocAlignedMem+0x122
08 libcoreclr!VirtualCallStubManager::GenerateResolveStub+0xb6
09 libcoreclr!VirtualCallStubManager::ResolveWorker+0x8a8
0a libcoreclr!VSD_ResolveWorker+0x2e7
0b libcoreclr!ResolveWorkerAsmStub+0x71
Thread 60 is still holding CodeFragmentHeap::m_Lock from frame 07.
Thread 61 is cooperative, blocked at trying to acquire m_Lock (held by thread 60):
00 libc_so!__lll_lock_wait_private+0x90
01 libc_so!pthread_mutex_lock+0x167
02 libcoreclr!CrstBase::Enter+0x94 crst.cpp:265
03 libcoreclr!CrstBase::AcquireLock+0x5 crst.h:174
04 libcoreclr!CrstBase::CrstHolder::CrstHolder+0x5 crosscomp.h:349
05 libcoreclr!CodeFragmentHeap::RealAllocAlignedMem+0x2a
06 libcoreclr!VirtualCallStubManager::GenerateResolveStub+0xb6
07 libcoreclr!VirtualCallStubManager::ResolveWorker+0x8a8
08 libcoreclr!VSD_ResolveWorker+0x2e7
09 libcoreclr!ResolveWorkerAsmStub+0x71
Because thread 61 is Cooperative and the GC is trying to suspend the runtime, the GC thread also waits on this thread to either reach a safe point or go preemptive — which it cannot do because it is parked inside pthread_mutex_lock.
Cycle:
GC -> waits for cooperative threads to suspend
T60 -> preemptive, in RareDisablePreemptiveGC waiting for GC to finish
(holds CodeFragmentHeap::m_Lock)
T61 -> cooperative, blocked on CodeFragmentHeap::m_Lock held by T60
(and the GC is waiting on T61 to suspend)
Regression?
Likely a regression from #113943.
Known Workarounds
Two environment knobs that, set together, prevent the bad path from being exercised:
DOTNET_EnableDiagnostics_IPC=0
DOTNET_PerfMapEnabled=0DOTNET_PerfMapEnabled=0ensuresPerfMap::s_enabledstays false at startup, soPerfMap::LogStubsearly-outs and never touchess_csPerfMap.DOTNET_EnableDiagnostics_IPC=0shuts off the Diagnostics Server, which is otherwise able to enable PerfMap at runtime via theds_rt_enable_perfmapIPC handler regardless of the env var (dotnet-trace --enable-perfmap,dotnet-monitor, third-party APM agents).
Both are needed: setting only one leaves the other path open.
Configuration
Likely not Linux specific, but that's where I debugged it.
Metadata
Metadata
Assignees
Labels
Type
Projects
- StatusShow more project fieldsNo status
Activity
dotnet-policy-service commentedon May 20, 2026
Tagging subscribers to this area: @JulieLeeMSFT, @dotnet/gc
See info in area-owners.md if you want to be subscribed.
leculver commentedon May 20, 2026
Here's some additional places that need to be audited. This was done via AI, so take it with a grain of salt. Needs to be investigated, but the details below might be wildly wrong.
ExecutionManager::getNextJumpStubAcquires
m_JumpStubCrst(CRST_UNSAFE_ANYMODE | CRST_DEBUGGER_THREAD), then later callsPerfMap::LogStubsdirectly. The whole function body runs under that lock per the precondition.m_JumpStubCrstinit (CRST_UNSAFE_ANYMODE):codeman.cpp:5227getJumpStub) before invokinggetNextJumpStub:codeman.cpp:5895,codeman.cpp:5938getNextJumpStubdefinition + lock precondition:codeman.cpp:5953-5962PerfMap::LogStubscall:codeman.cpp:6067EEJitManager::AllocJumpStubBlockAllocJumpStubBlockis itself called fromgetNextJumpStubwhilem_JumpStubCrstis held. The innerm_CodeHeapLockis released beforeReportStubBlock, but the outerm_JumpStubCrstis still held, soReportStubBlock(->PerfMap::LogStubs) acquires the toggling lock from a cooperative thread I think.m_CodeHeapLockdefinition (CRST_UNSAFE_ANYMODE, also no-toggle):codeman.cpp:1249-1250AllocJumpStubBlock:codeman.cpp:3311CrstHolder ch(&m_CodeHeapLock)scope:codeman.cpp:3339-3358ReportStubBlockcall (outside inner scope but inside outerm_JumpStubCrst):codeman.cpp:3360LoaderAllocator::AddRangeWorkerHelperunderLoaderHeap::m_CriticalSectionReportStubBlockis called fromAddRangeWorkerHelper, which itself is invoked from insideUnlockedAllocAlignedMemwhile the enclosingLoaderHeapis holding its ownm_CriticalSection(a rawCRITICAL_SECTIONviaCRITSEC_Holder, not aCrst). A raw critical section does not participate in GC-mode toggling at all, equivalent to no-toggle from the runtime's perspective, so a cooperative thread can be parked insideRareDisablePreemptiveGCwhile still holding it. Triggered every time aLoaderHeapcommits new pages (class loading, MethodDesc allocation, reflection-emit, etc.).LoaderAllocator::AddRangeWorkerHelperbody:loaderallocator.hpp:85-108ReportStubBlockcall (before taking_RangeListRWLock, but caller still holdsm_CriticalSection):loaderallocator.hpp:87LoaderHeap::RealAllocAlignedMemacquiresm_CriticalSectionthen callsUnlockedAllocAlignedMem:loaderheap.h:913-920UnlockedLoaderHeap::CommitNewPagescallsm_pRangeList->AddRange(→AddRangeWorker→AddRangeWorkerHelper):loaderheap.cpp:251Lower-risk sites at the introduced call but caller-dependent
These release any inner heap lock before calling
PerfMap::LogStubs; whether they can deadlock depends on what outer locks the caller of these functions holds. Common callers are class-loader and method-desc construction paths underCrstClassInit/ app-domain locks; that's a broader audit not done here.Precode::Allocate— FixupPrecode:precode.cpp:292· ThisPtrRetBuf:precode.cpp:306· StubPrecode / PInvokeImportPrecode:precode.cpp:319Precode::AllocateInterpreterPrecode:precode.cpp:257UMEntryThunkallocation via P/Invoke callback:dllimportcallback.cpp:297dotnet-policy-service commentedon May 20, 2026
Tagging subscribers to this area: @agocke
See info in area-owners.md if you want to be subscribed.
hoyosjs commentedon May 20, 2026
Given
PAL_PerfJitDump_LogMethodand the writes it does unders_csPerfMap, it doesn't feel great to doCRST_UNSAFE_ANYMODEsince we can get arbitrary pause times with slow NFS writes. However writing to file in COOP is already happening since grabbing the lock is just a toggle. This is the cheapest thing to do now. Something better would be to write in preemptive or deferred writing. @noahfalk @mdh1418 - what do you think for this one? For now I'll kick something that switches the CRST and see what happens - preemtive switching also works from the perspective of us not holding managed objects in that range, but I haven't audited the callers.hoyosjs commentedon May 21, 2026
repro stacks. coop thread waiting for Fragment lock:
Fragment owning thread (and also s_csPerfMap) trying to toggle gc state:
Hit rate is low. Testing the modeany workaround
Use UNSAFE_ANYMODE to work around dotnet#128401
Use UNSAFE_ANYMODE to work around dotnet#128401
noahfalk commentedon May 21, 2026
I'd suggest adjusting the code of RealAllocAlignedMem so that the call to ReportStubBlock occurs later in the method outside the scope of CodeFragmentHeap::m_Lock. We only need to ensure that the perfmap is updated after the allocation occurs and prior to code in that memory region being invoked.
Similar shifting might work for other code paths Lee mentioned above but I didn't investigate each one.
hoyosjs commentedon May 21, 2026
I did consider that too - although I am not sure we are getting a lot out of it
hoyosjs commentedon May 21, 2026
jkotas commentedon May 21, 2026
This allocator allocates larger chunk and then parcels it out into smaller chunks to give out. ReportStubBlock is for the larger chunk. If we were to move ReportStubBlock after the lock, other threads would be able to give out smaller chunks before the larger containing chunk got reported.
hoyosjs commentedon May 21, 2026
I was already changing a part of that in #127298 since it causes issues to report larger windows. The m_lock crst could be scoped to a subsection of the method and report before returning.
jkotas commentedon May 21, 2026
The right fix for dead locks with GC is typically running more code in preemptive mode and avoid cooperative mode. I think switching to preemptive mode around GenerateResolveStub (and other similar operations) would be the right fix for the problem. GenerateResolveStub looks like a relatively expensive operation that should not run in cooperative mode.
We may decide that the correct fix is too risky for servicing and we can do something suboptimal there instead and that's ok.
noahfalk commentedon May 21, 2026
Ah indeed, I was overlooking the fragment reuse, thanks for the call out. If we can switch the whole code path to run in pre-emptive thats great. If not we could still do a localized fix to move the logging out of the ANYMODE lock accounting for the fragment reuse, its just a little messier now.
hoyosjs commentedon May 21, 2026
@davidwrighton - I need to get back to other stuff for now. the anymode had even more problems (all paths have cases of gctrigger, so technically same issue). I think we indeed need to take the deferred logging approach.
[release/10.0] : Change the PerfMap crst into an UNSAFE_ANYMODE crst (#…
davidwrighton commentedon Jun 22, 2026
Fixed with PR #129142