Skip to content

rustc_thread_pool: join worker threads in build_scoped - #166322

Open
weihanglo wants to merge 2 commits into
rust-lang:mainfrom
weihanglo:windows-gnu-stackoverflow
Open

weihanglo wants to merge 2 commits into
rust-lang:mainfrom
weihanglo:windows-gnu-stackoverflow

Conversation

@weihanglo

@weihanglo weihanglo commented Nov 21, 2026 •

Copy link
Copy Markdown
Member

This is an LLM-assited-human-directed PR.

Found @cuviper as pre-approved reviewer through Zulip DM :)

See #llm-reviews > LLM for debuggin windows-gnu stack overflow #139070 @ 💬


Fixes #166530.

Let me copy-paste what I wrote there

Solution

While I haven't figured out why it became more often, my agent has found one potential fix that it may have some connections with the bug. The LLM-assisted-human-directed PR is in #166322.

Basically, the hypothesis is

After worker thread ran the closure and before it really exists (e.g., hasn't yet run destructors for TLS), the main thread exits. So the program gets into a state of "main exits but thread doesn't". And then Window's ExitProcess kills worker thread midway.

I don't know how this connects to window-gnu failure, but by my stress testing it did show that after this fix, cargo test suite no longer shows the stack overflow behavior.

And #166322 is anyway more correct IMHO for a fixed size pool, as std::thread::scoped doc does say TLS destructor may still be running when existing:

/// However, note that joining will only wait for the main function of these threads to finish; even
/// when this function returns, destructors of thread-local variables in these threads might still
/// be running.

If we eventually want to shift to a dynamic-sized pool, we might want to reconsider this change.

How to review

Commit by commit.

The commit structure follows: https://epage.github.io/dev/pr-style/#c-test


try-jobs: dist-x86_64-mingw
r? cuviper

@rustbot rustbot added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Nov 21, 2026
@weihanglo

Copy link
Copy Markdown
Member Author

@bors try

@rust-bors

This comment has been minimized.

rust-bors Bot pushed a commit that referenced this pull request Nov 21, 2026
[EXPERIMENTAL] rustc_thread_pool: test whether `build_scoped` waits for thread exit

try-jobs: dist-x86_64-mingw
@rust-bors

rust-bors Bot commented Nov 21, 2026

Copy link
Copy Markdown
Contributor

☀️ Try build successful (CI)
Build commit: 6ebbc66 (6ebbc66bff3bc876e9d5803b8679e85d633cb635)
Base parent: c7d5ba5 (c7d5ba59d8e11ee2e2204b173458eec4cb41c973)

@weihanglo

Copy link
Copy Markdown
Member Author

From my stress testing this does fix the issue. Will create a new issue with some info then

@weihanglo
weihanglo force-pushed the windows-gnu-stackoverflow branch from 4e78a54 to 89e7ed0 Compare November 24, 2026 20:50
@weihanglo weihanglo changed the title [EXPERIMENTAL] rustc_thread_pool: test whether build_scoped waits for thread exit rustc_thread_pool: join worker threads in build_scoped Nov 24, 2026
@weihanglo
weihanglo marked this pull request as ready for review November 24, 2026 22:06
@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 Nov 24, 2026
@weihanglo

Copy link
Copy Markdown
Member Author

This is ready for review. See #166530 and the updated PR description.

@petrochenkov petrochenkov self-assigned this Nov 25, 2026
@petrochenkov

Copy link
Copy Markdown
Contributor

#116237 is nasty, I'd never guess that it's the case.

Comment thread compiler/rustc_thread_pool/src/lib.rs Resolved
@weihanglo
weihanglo force-pushed the windows-gnu-stackoverflow branch from 89e7ed0 to d3b0efd Compare November 26, 2026 12:54
@rustbot

rustbot commented Nov 26, 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.

@petrochenkov

petrochenkov commented Nov 26, 2026 •

Copy link
Copy Markdown
Contributor

This is probably not a problem for the original rayon-core (except in documentation?) because it probably wants to inherit the behavior of std::thread::scope, whatever it is.
The original rayon-core doesn't even call wait_until_stopped.
But I'd wait for some reaction from @cuviper anyway.

In rustc we already wait with wait_until_stopped for the threads to complete, so it makes sense to wait even harder :)
(It's possible that the new logic makes the wait_until_stopped call redundant. Upd: unlike the new logic, wait_until_stopped releases a jobserver token while waiting.)
To me the change looks good.

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

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.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

--job-frontend=2 on nightly made windows-gnu stack overflow more frequent

4 participants