Skip to content

Conversation

cuviper
Copy link
Member

@cuviper cuviper commented Sep 20, 2025

This diverged from rayon in #142384, where a cleanup commit turned the matched worker_index into a reference, which is read after the set that may kill it. I've moved that read beforehand, and I hope the new comments will emphasize the subtlety of this unsafe code.

Hopefully fixes #146677.

@rustbot rustbot added 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. labels Sep 20, 2025
@rustbot
Copy link
Collaborator

rustbot commented Sep 20, 2025

r? @lcnr

rustbot has assigned @lcnr.
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

Comment on lines +391 to 393
// SAFETY: Once we call `set` on the internal `latch`,
// the target may proceed and invalidate `this`!
match unsafe { &(*this).kind } {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A safety comment here should explain why it is safe to create a reference out of (*this).kind. That it does not seem to do that, may be the reason it is a NOTE.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well, we added the NOTE in rayon before there was much consensus on proper SAFETY comments. It's definitely a safety-relevant comment, even if it's not directly justifying this unsafe.

(Personally, I would have used a single unsafe block around all of this, but this was also added after the port from rayon. Originally, it's still using the unsafe fn context for inner unsafe too.)

Copy link
Contributor

@lcnr lcnr left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

r=me after safety comment

View changes since this review

@cuviper cuviper force-pushed the dangling-count-latch branch from 57382b2 to 389a502 Compare September 22, 2025 21:04
@cuviper
Copy link
Member Author

cuviper commented Sep 22, 2025

Thanks for the review -- I added another SAFETY comment as requested.

@bors r=lcnr

@bors
Copy link
Collaborator

bors commented Sep 22, 2025

📌 Commit 389a502 has been approved by lcnr

It is now in the queue for this repository.

@bors bors added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Sep 22, 2025
Zalathar added a commit to Zalathar/rust that referenced this pull request Sep 23, 2025
Fix a dangling reference in `rustc_thread_pool`

This diverged from `rayon` in rust-lang#142384, where a cleanup commit turned the matched `worker_index` into a reference, which is read _after_ the `set` that may kill it. I've moved that read beforehand, and I hope the new comments will emphasize the subtlety of this unsafe code.

Hopefully fixes rust-lang#146677.
bors added a commit that referenced this pull request Sep 23, 2025
Rollup of 13 pull requests

Successful merges:

 - #146632 (Fix uses of "adaptor")
 - #146731 (test: Use SVG for terminal url test)
 - #146775 (fixes for numerous clippy warnings)
 - #146784 ([win] Use find-msvc-tools instead of cc to find the linker and rc on Windows)
 - #146799 (Fix a dangling reference in `rustc_thread_pool`)
 - #146802 (mbe: Simplifications and refactoring)
 - #146806 (add private module override re-export test)
 - #146827 (Linker-plugin-based LTO: update list of good combinations (inc. beta + nightly))
 - #146875 (tests/run-make/crate-loading: Rename source files for clarity)
 - #146896 (rustc-dev-guide subtree update)
 - #146898 (Update books)
 - #146899 (Fix a crash/mislex when more than one frontmatter closing possibility is considered)
 - #146907 (add regression test for issue 146537)

r? `@ghost`
`@rustbot` modify labels: rollup
bors added a commit that referenced this pull request Sep 23, 2025
Rollup of 10 pull requests

Successful merges:

 - #146632 (Fix uses of "adaptor")
 - #146731 (test: Use SVG for terminal url test)
 - #146775 (fixes for numerous clippy warnings)
 - #146784 ([win] Use find-msvc-tools instead of cc to find the linker and rc on Windows)
 - #146799 (Fix a dangling reference in `rustc_thread_pool`)
 - #146802 (mbe: Simplifications and refactoring)
 - #146806 (add private module override re-export test)
 - #146827 (Linker-plugin-based LTO: update list of good combinations (inc. beta + nightly))
 - #146875 (tests/run-make/crate-loading: Rename source files for clarity)
 - #146877 (prevent line number from being copied in chrome)

r? `@ghost`
`@rustbot` modify labels: rollup
@bors bors merged commit 64e81e5 into rust-lang:master Sep 23, 2025
10 checks passed
@rustbot rustbot added this to the 1.92.0 milestone Sep 23, 2025
rust-timer added a commit that referenced this pull request Sep 23, 2025
Rollup merge of #146799 - cuviper:dangling-count-latch, r=lcnr

Fix a dangling reference in `rustc_thread_pool`

This diverged from `rayon` in #142384, where a cleanup commit turned the matched `worker_index` into a reference, which is read _after_ the `set` that may kill it. I've moved that read beforehand, and I hope the new comments will emphasize the subtlety of this unsafe code.

Hopefully fixes #146677.
Muscraft pushed a commit to Muscraft/rust that referenced this pull request Sep 24, 2025
Fix a dangling reference in `rustc_thread_pool`

This diverged from `rayon` in rust-lang#142384, where a cleanup commit turned the matched `worker_index` into a reference, which is read _after_ the `set` that may kill it. I've moved that read beforehand, and I hope the new comments will emphasize the subtlety of this unsafe code.

Hopefully fixes rust-lang#146677.
Muscraft pushed a commit to Muscraft/rust that referenced this pull request Sep 24, 2025
…iaskrgr

Rollup of 10 pull requests

Successful merges:

 - rust-lang#146632 (Fix uses of "adaptor")
 - rust-lang#146731 (test: Use SVG for terminal url test)
 - rust-lang#146775 (fixes for numerous clippy warnings)
 - rust-lang#146784 ([win] Use find-msvc-tools instead of cc to find the linker and rc on Windows)
 - rust-lang#146799 (Fix a dangling reference in `rustc_thread_pool`)
 - rust-lang#146802 (mbe: Simplifications and refactoring)
 - rust-lang#146806 (add private module override re-export test)
 - rust-lang#146827 (Linker-plugin-based LTO: update list of good combinations (inc. beta + nightly))
 - rust-lang#146875 (tests/run-make/crate-loading: Rename source files for clarity)
 - rust-lang#146877 (prevent line number from being copied in chrome)

r? `@ghost`
`@rustbot` modify labels: rollup
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. 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.

Irreproducible "index out of bounds" error in rustc_thread_pool
5 participants