-
Notifications
You must be signed in to change notification settings - Fork 13.8k
Fix a dangling reference in rustc_thread_pool
#146799
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
base: master
Are you sure you want to change the base?
Conversation
// SAFETY: Once we call `set` on the internal `latch`, | ||
// the target may proceed and invalidate `this`! | ||
match unsafe { &(*this).kind } { |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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) } { |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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
.
This diverged from
rayon
in #142384, where a cleanup commit turned the matchedworker_index
into a reference, which is read after theset
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.