-
Notifications
You must be signed in to change notification settings - Fork 13.3k
Data race in thread::scope #98498
New issue
Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? # to your account
Comments
The following patch fixes this: diff --git a/library/std/src/thread/scoped.rs b/library/std/src/thread/scoped.rs
index a387a09dc8b..06019cbc75b 100644
--- a/library/std/src/thread/scoped.rs
+++ b/library/std/src/thread/scoped.rs
@@ -55,8 +55,10 @@ pub(super) fn decrement_num_running_threads(&self, panic: bool) {
if panic {
self.a_thread_panicked.store(true, Ordering::Relaxed);
}
+ // The moment we do the `fetch_sub`, the memory we ourselves are stored in can be deallocated.
+ let main_thread = self.main_thread.clone();
if self.num_running_threads.fetch_sub(1, Ordering::Release) == 1 {
- self.main_thread.unpark();
+ main_thread.unpark();
}
}
} However, even with this patch, we still run into a problem like #55005: the Another alternative might be to put the entire |
Going to attach this to the 1.63 milestone, since this is in a newly stabilized feature -- we may wish to back out stabilization for a cycle or backport fixes. |
fix data race in thread::scope Puts the `ScopeData` into an `Arc` so it sticks around as long as we need it. This means one extra `Arc::clone` per spawned scoped thread, which I hope is fine. Fixes rust-lang#98498 r? `@m-ou-se`
fix data race in thread::scope Puts the `ScopeData` into an `Arc` so it sticks around as long as we need it. This means one extra `Arc::clone` per spawned scoped thread, which I hope is fine. Fixes rust-lang#98498 r? ``@m-ou-se``
fix data race in thread::scope Puts the `ScopeData` into an `Arc` so it sticks around as long as we need it. This means one extra `Arc::clone` per spawned scoped thread, which I hope is fine. Fixes rust-lang#98498 r? ```@m-ou-se```
fix data race in thread::scope Puts the `ScopeData` into an `Arc` so it sticks around as long as we need it. This means one extra `Arc::clone` per spawned scoped thread, which I hope is fine. Fixes rust-lang#98498 r? ````@m-ou-se````
Re-opening for beta backport. |
fix data race in thread::scope Puts the `ScopeData` into an `Arc` so it sticks around as long as we need it. This means one extra `Arc::clone` per spawned scoped thread, which I hope is fine. Fixes rust-lang#98498 r? `````@m-ou-se`````
The fix was backported in #98949. |
fix data race in thread::scope Puts the `ScopeData` into an `Arc` so it sticks around as long as we need it. This means one extra `Arc::clone` per spawned scoped thread, which I hope is fine. Fixes rust-lang/rust#98498 r? `````@m-ou-se`````
The following code sometimes causes a data race to be reported by Miri:
Miri reports:
The deallocation occurs when
scope
returns and its local variablescope
gets deallocated.The read occurs here (I am fairly sure):
rust/library/std/src/thread/scoped.rs
Lines 58 to 59 in 7098a71
This reads the contents of the field
self.main_thread
after thefetch_sub
. But the moment thefetch_sub
is done, the memory backingself
might be deallocated. That makes the read a potential use-after-free, and a race with the deallocation.Cc @m-ou-se
The text was updated successfully, but these errors were encountered: