Skip to content

[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

Merged
merged 1 commit into from
Mar 19, 2025

Conversation

VSadov
Copy link
Member

@VSadov VSadov commented Mar 3, 2025

Backport of #112809

/cc @VSadov

Fixes: #110350

Calling ExitThread API could cause a process-wide deadlock if called at unlucky moment.

Regression

  • Yes
  • No

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.

… 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]>
@Copilot Copilot AI review requested due to automatic review settings March 3, 2025 03:21
@VSadov VSadov requested a review from MichalStrehovsky as a code owner March 3, 2025 03:21
Copy link
Contributor

@Copilot Copilot AI left a 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.

Copy link
Contributor

Tagging subscribers to this area: @mangod9
See info in area-owners.md if you want to be subscribed.

@VSadov VSadov added the Servicing-consider Issue for next servicing release review label Mar 5, 2025
Copy link
Member

@jeffschwMSFT jeffschwMSFT left a 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

@jeffschwMSFT jeffschwMSFT added this to the 9.0.x milestone Mar 5, 2025
@rbhanda rbhanda added NO-MERGE The PR is not ready for merge yet (see discussion for detailed reasons) Servicing-approved Approved for servicing release and removed Servicing-consider Issue for next servicing release review labels Mar 6, 2025
@rbhanda rbhanda modified the milestones: 9.0.x, 9.0.5 Mar 6, 2025
@carlossanlop
Copy link
Contributor

@VSadov @mangod9 Today is code complete for the April Release. If you want this fix included in this release, please merge before 4pm PT.

@VSadov
Copy link
Member Author

VSadov commented Mar 18, 2025

Since we are past the code complete for the April Release, can we merge this?

@jeffschwMSFT
Copy link
Member

Since we are past the code complete for the April Release, can we merge this?

Yes

@VSadov VSadov removed the NO-MERGE The PR is not ready for merge yet (see discussion for detailed reasons) label Mar 19, 2025
@VSadov
Copy link
Member Author

VSadov commented Mar 19, 2025

Thanks!!

@VSadov VSadov merged commit 0fb125a into dotnet:release/9.0-staging Mar 19, 2025
99 of 105 checks passed
@VSadov VSadov deleted the backport112809 branch March 19, 2025 00:35
@github-actions github-actions bot locked and limited conversation to collaborators Apr 18, 2025
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area-VM-coreclr Servicing-approved Approved for servicing release
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants