[release/9.0-staging] Backport "Use FLS detach callback as a thread termination notification. Another try."#113055
Merged
VSadov merged 1 commit intodotnet:release/9.0-stagingfrom Mar 19, 2025
Conversation
… try. (dotnet#112809) * Use FLS detach as thread termination notification on windows. * use _ASSERTE_ALL_BUILDS * one more case * InitFlsSlot throws per convention used in threading initialization * OsDetachThread could be void * Update src/coreclr/vm/ceemain.cpp * ensure CoInitialize * Asserts to fail deterministically. * comments * scope the failfast to Windows and tweak the comments a bit. * better assert * Apply suggestions from code review Co-authored-by: Jan Kotas <jkotas@microsoft.com> * Undo unnecessary finalizer thread changes * reverse the failfast condition * handle destruction of unattached threads * PR feedback Co-authored-by: Jan Kotas <jkotas@microsoft.com> * Update src/coreclr/vm/ceemain.cpp * set g_fComStarted as soon as we call CoInitialize * Naming nit * fix a typpo + better comment --------- Co-authored-by: Jan Kotas <jkotas@microsoft.com>
Contributor
|
Tagging subscribers to this area: @mangod9 |
mangod9
approved these changes
Mar 3, 2025
jeffschwMSFT
approved these changes
Mar 5, 2025
Member
jeffschwMSFT
left a comment
There was a problem hiding this comment.
lgtm. we will take for consideration in 9.0.x
Contributor
Member
Author
|
Since we are past the code complete for the April Release, can we merge this? |
Member
Yes |
Member
Author
|
Thanks!! |
This was referenced Apr 3, 2025
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.
Backport of #112809
/cc @VSadov
Fixes: #110350
Calling ExitThread API could cause a process-wide deadlock if called at unlucky moment.
Regression
The root cause is running thread termination routines directly or indirectly from
DllMainTHREAD_DETACHcallback, which is called while holding OS Loader Lock. The termination callback may arrive when GC is in progress and deinitialization of a managed thread must wait for GC completion (in case GC is scanning the thread's stack or other state), at the same time GC may call something that takes Loader Lock - for example create a background thread.The potential for the deadlock was present for a long time. However, the conditions for the deadlock are relatively narrow and the issue has not been observed until recently. It is likely that some changes in the timings around thread termination/creation and timing of GC background thread creation made the deadlock more likely to happen in 9.0, thus the deadlock appears as a regression to code that used to work reliably.
Testing
Added directed asserts to fail deterministically if unexpected managed code reentrancy happens after observing OS FLS detach callback.
Regular testing.
Running WinForms tests locally.
The WinForms repo has been running for a while with the runtime version that contains the 10.0 fix.
Risk
Moderate
The fix is essentially a switch to a different OS callback that has advantage as it does not run with Loader Lock acquired.
The original fix had to be reverted once when we discovered bad interactions with COM per-thread cleanup, which uses the same OS notification mechanism and, as it turned out, has implicit dependency on the order of deinitialization. If COM clean up needs to run managed code and happens after our clean up has run already, we may end up with GC holes.
The new PR contains an extra fix for the COM interaction by ensuring that COM is initialized before managed runtime is initialized, thus the COM callback is registered before ours, thus satisfying the ordering expectations for the callbacks.
A lot of additional effort went into validating the fix.
I am setting the risk level as "Moderate" mostly because graceful thread shutdown is evidently a delicate area with potential for "long distance interactions" with unrelated features.