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

Catch panics in critical section of thread::scope #724

Closed
wants to merge 1 commit into from
Closed
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
32 changes: 19 additions & 13 deletions crossbeam-utils/src/thread.rs
Original file line number Diff line number Diff line change
Expand Up @@ -159,20 +159,26 @@ where
// Execute the scoped function, but catch any panics.
let result = panic::catch_unwind(panic::AssertUnwindSafe(|| f(&scope)));

// Wait until all nested scopes are dropped.
drop(scope.wait_group);
wg.wait();

// Join all remaining spawned threads.
let panics: Vec<_> = scope
.handles
.lock()
.unwrap()
// Filter handles that haven't been joined, join them, and collect errors.
.drain(..)
.filter_map(|handle| handle.lock().unwrap().take())
.filter_map(|handle| handle.join().err())
.collect();
// If an unwinding panic occurs before all threads are joined
// promote it to an aborting panic to prevent any threads from escaping the scope.
let panics: Vec<_> = panic::catch_unwind(panic::AssertUnwindSafe(|| {
// Wait until all nested scopes are dropped.
drop(scope.wait_group);
wg.wait();

// Join all remaining spawned threads.
scope
.handles
.lock()
.unwrap()
// Filter handles that haven't been joined, join them, and collect errors.
.drain(..)
.filter_map(|handle| handle.lock().unwrap().take())
.filter_map(|handle| handle.join().err())
.collect()
}))
.unwrap_or_else(|_| std::process::abort());

// If `f` has panicked, resume unwinding.
// If any of the child threads have panicked, return the panic errors.
Expand Down