Skip to content

Fix data race in thread::scope() #98504

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

Closed
Closed
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 19 additions & 4 deletions library/std/src/thread/scoped.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,13 @@ pub struct Scope<'scope, 'env: 'scope> {
#[stable(feature = "scoped_threads", since = "1.63.0")]
pub struct ScopedJoinHandle<'scope, T>(JoinInner<'scope, T>);

// Note: all of `ScopeData` fields must be interiorly mutable since
// it may be deallocated in the middle of a `&self` method:
// see `decrement_num_running_threads` below for more info.
pub(super) struct ScopeData {
num_running_threads: AtomicUsize,
a_thread_panicked: AtomicBool,
main_thread: Thread,
main_thread: UnsafeCell<Thread>,
}

impl ScopeData {
Expand All @@ -51,12 +54,24 @@ impl ScopeData {
panic!("too many running threads in thread scope");
}
}
fn main_thread(&self) -> &Thread {
unsafe { &*self.main_thread.get() }
}
pub(super) fn decrement_num_running_threads(&self, panic: bool) {
if panic {
self.a_thread_panicked.store(true, Ordering::Relaxed);
}
let main_thread = self.main_thread().clone();
if self.num_running_threads.fetch_sub(1, Ordering::Release) == 1 {
self.main_thread.unpark();
// By now, `num_running_threads` is `0`, so when `scope()` in the main thread
// is unparked, it will complete its business and deallocate `*self`!
// Two things to look after:
// - it can spuriously unpark / wake up, **so `self` can no longer be used**, even
// before we, ourselves, unpark. Hence why we've cloned the `main_thread`'s handle.
// - no matter how it unparks, `*self` may be deallocated before this function
// returns, so all of `*self` data, **including `main_thread`**, must be interiorly
// mutable. See https://github.com/rust-lang/rust/issues/55005 for more info.
main_thread.unpark();
}
}
}
Expand Down Expand Up @@ -133,7 +148,7 @@ where
let scope = Scope {
data: ScopeData {
num_running_threads: AtomicUsize::new(0),
main_thread: current(),
main_thread: current().into(),
a_thread_panicked: AtomicBool::new(false),
},
env: PhantomData,
Expand Down Expand Up @@ -328,7 +343,7 @@ impl fmt::Debug for Scope<'_, '_> {
f.debug_struct("Scope")
.field("num_running_threads", &self.data.num_running_threads.load(Ordering::Relaxed))
.field("a_thread_panicked", &self.data.a_thread_panicked.load(Ordering::Relaxed))
.field("main_thread", &self.data.main_thread)
.field("main_thread", self.data.main_thread())
.finish_non_exhaustive()
}
}
Expand Down