Skip to content
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

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

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
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
1 change: 1 addition & 0 deletions library/std/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -291,6 +291,7 @@
#![feature(std_internals)]
#![feature(str_internals)]
#![feature(strict_provenance)]
#![feature(sync_unsafe_cell)]
//
// Library features (alloc):
#![feature(alloc_layout_extra)]
Expand Down
26 changes: 22 additions & 4 deletions library/std/src/thread/scoped.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,16 @@ 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,
// SAFETY: `ScopeData` is `&`-shared by all the scoped threads, with no
// mutation whatsoever, except when `num_running_threads` drops down to `0`,
// which is when the main thread's `scope()` may deallocate this data.
main_thread: crate::cell::SyncUnsafeCell<Thread>,
}

impl ScopeData {
Expand All @@ -51,12 +57,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` fields, *including `main_thread`*, must be interiorly
// mutable. See https://github.com/rust-lang/rust/pull/98017 for more info.
main_thread.unpark();
}
}
}
Expand Down Expand Up @@ -133,7 +151,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 +346,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