Skip to content

Commit b97d875

Browse files
committed
Add soundness test for dropping scoped thread results before joining.
1 parent 1c06eb7 commit b97d875

File tree

1 file changed

+24
-1
lines changed

1 file changed

+24
-1
lines changed

library/std/src/thread/tests.rs

+24-1
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,11 @@ use crate::mem;
44
use crate::panic::panic_any;
55
use crate::result;
66
use crate::sync::{
7+
atomic::{AtomicBool, Ordering},
78
mpsc::{channel, Sender},
89
Arc, Barrier,
910
};
10-
use crate::thread::{self, ThreadId};
11+
use crate::thread::{self, Scope, ThreadId};
1112
use crate::time::Duration;
1213
use crate::time::Instant;
1314

@@ -293,3 +294,25 @@ fn test_thread_id_not_equal() {
293294
assert!(thread::current().id() != spawned_id);
294295
}
295296

297+
#[test]
298+
fn test_scoped_threads_drop_result_before_join() {
299+
let actually_finished = &AtomicBool::new(false);
300+
struct X<'scope, 'env>(&'scope Scope<'scope, 'env>, &'env AtomicBool);
301+
impl Drop for X<'_, '_> {
302+
fn drop(&mut self) {
303+
thread::sleep(Duration::from_millis(20));
304+
let actually_finished = self.1;
305+
self.0.spawn(move || {
306+
thread::sleep(Duration::from_millis(20));
307+
actually_finished.store(true, Ordering::Relaxed);
308+
});
309+
}
310+
}
311+
thread::scope(|s| {
312+
s.spawn(move || {
313+
thread::sleep(Duration::from_millis(20));
314+
X(s, actually_finished)
315+
});
316+
});
317+
assert!(actually_finished.load(Ordering::Relaxed));
318+
}

0 commit comments

Comments
 (0)