Skip to content

thread::scope clobbers the panic payload #139017

Open
@SludgePhD

Description

@SludgePhD

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

No one assigned

    Labels

    A-panicArea: Panicking machineryA-threadArea: `std::thread`C-bugCategory: This is a bug.T-libsRelevant to the library team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions