-
Notifications
You must be signed in to change notification settings - Fork 13.7k
Open
Labels
A-panicArea: Panicking machineryArea: Panicking machineryA-threadArea: `std::thread`Area: `std::thread`C-bugCategory: This is a bug.Category: This is a bug.T-libsRelevant to the library team, which will review and decide on the PR/issue.Relevant to the library team, which will review and decide on the PR/issue.
Description
I tried this code:
use std::{panic::catch_unwind, thread};
fn main() {
let res = catch_unwind(|| {
thread::scope(|scope| {
scope.spawn(|| panic!("my panic message with useful information"));
});
});
let payload = res.unwrap_err();
let message = payload.downcast_ref::<&str>().unwrap();
dbg!(message);
}
I expected to see this happen: the panic payload from the scoped thread should be forwarded to the main thread using panic::resume_unwind
. The payload passed to panic!
inside the scope should be retrievable there, and there should not be any additional panics.
Instead, this happened: a second panic with no link to the original is caused by the thread::scope
implementation, and the payload is a meaningless "a scoped thread panicked" message.
Meta
rustc --version --verbose
:
rustc 1.85.1
beta and nightly also behave like this
Metadata
Metadata
Assignees
Labels
A-panicArea: Panicking machineryArea: Panicking machineryA-threadArea: `std::thread`Area: `std::thread`C-bugCategory: This is a bug.Category: This is a bug.T-libsRelevant to the library team, which will review and decide on the PR/issue.Relevant to the library team, which will review and decide on the PR/issue.
Type
Projects
Milestone
Relationships
Development
Select code repository
Activity
theemathas commentedon Mar 28, 2025
The
scope()
documentation states:You can get the payload with the following code:
SludgePhD commentedon Mar 28, 2025
Sure, but there is no good reason for the behavior in the first place. Structured concurrency primitives like
thread::scope
have the opportunity to perfectly forward panics across thread boundaries, it would be a waste not to use it.hanna-kruppe commentedon Mar 28, 2025
What do you expect to happen the implicit join at the end of the scope finds more than one panicked thread?
SludgePhD commentedon Mar 28, 2025
Then any of the panics can be forwarded (which is the same behavior as rayon's). The code running in the threads also has to ensure that it doesn't cause any knock-on panics for this strategy to yield a "useful" panic payload, of course.