Skip to content

Commit

Permalink
Add soundness test for dropping scoped thread results before joining.
Browse files Browse the repository at this point in the history
  • Loading branch information
m-ou-se committed Mar 9, 2022
1 parent 1c06eb7 commit b97d875
Showing 1 changed file with 24 additions and 1 deletion.
25 changes: 24 additions & 1 deletion library/std/src/thread/tests.rs
Expand Up @@ -4,10 +4,11 @@ use crate::mem;
use crate::panic::panic_any;
use crate::result;
use crate::sync::{
atomic::{AtomicBool, Ordering},
mpsc::{channel, Sender},
Arc, Barrier,
};
use crate::thread::{self, ThreadId};
use crate::thread::{self, Scope, ThreadId};
use crate::time::Duration;
use crate::time::Instant;

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

#[test]
fn test_scoped_threads_drop_result_before_join() {
let actually_finished = &AtomicBool::new(false);
struct X<'scope, 'env>(&'scope Scope<'scope, 'env>, &'env AtomicBool);
impl Drop for X<'_, '_> {
fn drop(&mut self) {
thread::sleep(Duration::from_millis(20));
let actually_finished = self.1;
self.0.spawn(move || {
thread::sleep(Duration::from_millis(20));
actually_finished.store(true, Ordering::Relaxed);
});
}
}
thread::scope(|s| {
s.spawn(move || {
thread::sleep(Duration::from_millis(20));
X(s, actually_finished)
});
});
assert!(actually_finished.load(Ordering::Relaxed));
}

0 comments on commit b97d875

Please sign in to comment.