Repository navigation
Wait for EE startup before finalizer managed execution - #135452
Merged
davidwrighton merged 1 commit intoOct 9, 2026
Merged
Conversation
Co-authored-by: Copilot <[email protected]> Copilot-Session: c49ab1f9-7e7c-44a2-8f56-a17b7c1d2a70
|
Azure Pipelines: Successfully started running 3 pipeline(s). 13 pipeline(s) were filtered out due to trigger conditions. There may be pipelines that require an authorized user to comment /azp run to run. |
Contributor
|
Tagging subscribers to this area: @agocke |
jkoritzinsky
approved these changes
Oct 8, 2026
jkotas
approved these changes
Oct 8, 2026
davidwrighton
marked this pull request as ready for review
October 9, 2026 01:08
This was referenced Oct 9, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to subscribe to this conversation on GitHub.
Already have an account?
Sign in.
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Make the dedicated finalizer thread wait for EE startup to finish before entering its managed worker.
CoreLib activation is not a sufficient readiness barrier: startup initializes shared statics and thread-static infrastructure afterward. The finalizer thread can already be running during that interval, and
GC.RunFinalizersaccessesThread.CurrentThreadbefore checking for finalizable objects.After
HasStarted()and background-thread registration, callEnsureEEStarted()in preemptive GC mode. EE startup creates the finalizer thread while holding the startup lock, so this call waits for the existing initializer rather than initiating loading on the finalizer thread. Keep the early Windows COM/FLS handshake and debugger helper thread unchanged.If startup is unavailable or has failed, log the HRESULT, skip managed execution, and signal finalizer shutdown completion.
Validation
Linux x64, Checked CoreCLR, Release libraries, Debug host:
./build.sh clr+libs+host -rc Checked -lc Release -hc Debug- baseline passed, zero warnings/errors../build.sh clr.runtime -rc Checked -lc Release -hc Debug- final patched build passed, zero warnings/errors../src/tests/build.sh -GenerateLayoutOnly x64 Checked /p:HostConfiguration=Debug- passed. The Core_Root runtime hash matched the freshly built runtime..dotnet/dotnet artifacts/bin/coreclr/linux.x64.Checked/R2RDump/R2RDump.dll --in artifacts/tests/coreclr/linux.x64.Checked/Tests/Core_Root/System.Private.CoreLib.dll --header | head -35- verified CoreLib's ReadyToRun signature0x00525452and format version 31.0../src/tests/build.sh -test:GC/API/GC/Finalize.csproj -test:GC/API/GC/ReRegisterForFinalize.csproj -test:GC/API/GC/SuppressFinalize.csproj x64 Checked -priority1 /p:HostConfiguration=Debug- passed; exactly three test projects built.corerunandlibcoreclr.so, deliberately omitting CoreLib: passed.timeout 15observed corerun exit 255 with0x80070002; runtime logging confirmed that the finalizer skipped managed execution becauseEnsureEEStarted()failed. The temporary runtime fixture was removed afterward.git diff --check- passed.Windows ICorDebug validation
Locally tested on Windows 11 Enterprise x64, version
10.0.26300, with Checked CoreCLR, Release libraries, Debug host, and ReadyToRun CoreLib, using the complete public-API-onlyStartupLauncher.cppandStartup.csfrom #135448. No function evaluation was performed.The comparison used a clean isolated worktree at the PR's immediate parent,
d21d8d491d303fa65d75da14b09c8475d2a94bb5, followed by the exact PR head,34a436b16fd32634beb24b5d1561331d16006d70. The only source difference is this PR'sfinalizerthread.cppchange. Separate Core_Root snapshots were frozen and their runtime, CoreLib, corerun, DBI, and DAC SHA256 hashes checked against the corresponding build outputs. CoreLib was identical across snapshots; its managed-native header had RVA 5272, size 220, ReadyToRun signature0x00525452, version 31.0.Main; 1/10 reachedMainandDebugger.BreakMainandDebugger.Break, launcher exit 0, no contract assertionsMainandDebugger.Break, exit 0MainandDebugger.Break, exit 0Main, exit 0Main, exit 0The locally observed unpatched failure was an earlier native Checked assertion, not the exact managed
0x80131506output in the issue:CrstBase::Enterrejected lock acquisition in aGC_NOTRIGGER + MODE_COOPERATIVEregion, withGetTLSIndexForThreadStatic -> MethodTable::EnsureTlsIndexAllocated -> DynamicHelperFixup -> DynamicHelperWorker -> DelayLoad_Helper -> CoreLib -> FinalizerThread::FinalizeAllObjectson the finalizer thread. The launcher returned 2 after the target exited before its breakpoint. This establishes failure-before/pass-after for the Windows startup scenario without claiming that the issue's exact exception text was reproduced.Build and execution commands (run from the isolated source root;
$CRselects the corresponding frozen Core_Root):The same cached public Windows x64
Microsoft.Diagnostics.DbgShim.win-x64package, version8.0.0-preview.23527.1(file version8.0.9.2701, SHA2567B7ADB0E5E06C650F3D1093DCF901A5F65B4C8A2ECBF1A3343546EEF4A391648), was used for both sides. This differs from the issue's recorded shim version. All 52 runs completed without timeouts or surviving target processes. Logs, source, hashes, and result matrices are preserved locally underC:\git\runtime\artifacts\tmp\pr135452-validation.Scope and limitations
The Windows public-API startup repro now provides local failure-before/pass-after evidence. The Linux managed tests above remain smoke coverage.
The older startup ordering also allowed finalizer execution before TLS readiness; the introducing commit may have exposed a pre-existing race. This PR does not change the debugger retry path in
ThreadSuspend::SuspendEEand does not claim to resolve the separately reported startup-threadCatchAtSafePointGC-mode contract violation.Resolves #135448
Note
This candidate fix and pull request description were generated with GitHub Copilot.