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.)

CountLatchKind::Stealing { latch, registry, worker_index } => {
let registry = Arc::clone(registry);
let worker_index = *worker_index;
if unsafe { CoreLatch::set(latch) } {
Copy link
Member

Choose a reason for hiding this comment

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

This seems to be the "call [to] set on the internal latch", but it does not have a safety comment. It could say that this is only safe if none of the following code in this match arm "access[es] any part of this anymore".

But it is not obvious that worker_index should be considered part of this, because it is obscured by the match creating three new names that are really parts of this. Is it possible to shadow or drop all names that should not be reused?

Copy link
Member Author

Choose a reason for hiding this comment

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

My change does shadow worker_thread, and registry was already. The only thing left is latch that we're using in this call, which I guess we could shadow to *const CoreLatch if you want, since that's what we pass to set.

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.

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