-
Notifications
You must be signed in to change notification settings - Fork 5k
[release/9.0-staging] Backport "Use FLS detach callback as a thread termination notification. Another try." #113055
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
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 <[email protected]> * Undo unnecessary finalizer thread changes * reverse the failfast condition * handle destruction of unattached threads * PR feedback Co-authored-by: Jan Kotas <[email protected]> * 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 <[email protected]>
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Copilot reviewed 8 out of 8 changed files in this pull request and generated no comments.
Tagging subscribers to this area: @mangod9 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
lgtm. we will take for consideration in 9.0.x
Since we are past the code complete for the April Release, can we merge this? |
Yes |
Thanks!! |
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
DllMain
THREAD_DETACH
callback, 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.