[release/9.0-staging] Backport "Use FLS detach callback as a thread termination notification. Another try." #113055
+189
−69
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
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.