Skip to content

std: eargerly run TLS destructors to catch stack overflows - #164917

Open
joboet wants to merge 3 commits into
rust-lang:mainfrom
joboet:eager_tls_dtors
Open

joboet wants to merge 3 commits into
rust-lang:mainfrom
joboet:eager_tls_dtors

Conversation

@joboet

@joboet joboet commented Oct 25, 2026 •

Copy link
Copy Markdown
Member

This addresses #111272 on UNIX platforms with native TLS by eagerly running the destructors of TLS variables inside the thread main function while the stack overflow detection logic is still active. I've previously tried this strategy in #109858, but that failed on the Apple platforms because I removed the _tlv_atexit implementation of guard::enable in favour of the pthread-TLS-based implementation which doesn't work on Apple (see the updated documentation in the corresponding file for why that is).

This obviously requires us to always manage the list of destructors manually, even on platforms like Linux that allow registering a per-object destructor. While that makes it slightly harder to support the case where a library referencing an instance of std defined in a separate DSO is unloaded, we currently don't support that anyway – __cxa_thread_atexit_impl prevents the unloading of the DSO referenced by the dso_symbol argument, but we always reference the dso_handle living alongside std instead of the one of the DSO where the variable is defined.

I've also added a drive-by commit updating the documentation of the Apple thread guard with some interesting caveats I've recently come across.

@rustbot rustbot added O-hermit Operating System: Hermit S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. T-libs Relevant to the library team, which will review and decide on the PR/issue. labels Oct 25, 2026
@rustbot

rustbot commented Oct 25, 2026

Copy link
Copy Markdown
Collaborator

r? @jhpratt

rustbot has assigned @jhpratt.
They will have a look at your PR within the next two weeks and either review your PR or reassign to another reviewer.

Use r? to explicitly pick a reviewer

Why was this reviewer chosen?

The reviewer was selected based on:

  • Owners of files modified in this PR: @ChrisDenton, libs
  • @ChrisDenton, libs expanded to 37 candidates
  • Random selection from 6 candidates

@joboet joboet changed the title Eager tls dtors std: eargerly run TLS destructors to catch stack overflows Oct 25, 2026
@rust-log-analyzer

This comment has been minimized.

@joboet

joboet commented Oct 25, 2026

Copy link
Copy Markdown
Member Author

@bors try jobs=test-aarch64-apple-,test-x86_64-msvc-

@rust-bors

This comment has been minimized.

rust-bors Bot pushed a commit that referenced this pull request Oct 25, 2026
std: eargerly run TLS destructors to catch stack overflows 


try-job: test-aarch64-apple-*
try-job: test-x86_64-msvc-*
@rust-log-analyzer

This comment has been minimized.

@rust-bors rust-bors Bot added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Oct 25, 2026
@rust-bors

rust-bors Bot commented Oct 25, 2026

Copy link
Copy Markdown
Contributor

💔 Test for 0f4573b failed: CI. Failed jobs:

@joboet

joboet commented Oct 28, 2026 •

Copy link
Copy Markdown
Member Author

I don't get why that test is failing – it's completely unrelated, and doesn't even fail when I run it locally. Is that known to be spurious?

Anyway, I'll mark this as ready for review.
@rustbot ready

@rust-lang rust-lang deleted a comment from rust-bors Bot Oct 28, 2026
@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. and removed S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Oct 28, 2026
@rustbot rustbot added the A-thread Area: `std::thread` label Nov 24, 2026
@rustbot

rustbot commented Nov 24, 2026

Copy link
Copy Markdown
Collaborator

This PR was rebased onto a different main commit. Here's a range-diff highlighting what actually changed.

Rebasing is a normal part of keeping PRs up to date, so no action is needed—this note is just to help reviewers.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

A-thread Area: `std::thread` O-hermit Operating System: Hermit S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. T-libs Relevant to the library team, which will review and decide on the PR/issue.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants