Skip to content

A thread closure's destructor can panic in std::thread::spawn on Windows #124468

Open
@fuzzypixelz

Description

@fuzzypixelz

The following is part of the native Thread::new implementation on Windows:

// https://github.com/rust-lang/rust/blob/master/library/std/src/sys/pal/windows/thread.rs#L30
let ret = c::CreateThread(
    ptr::null_mut(),
    stack,
    Some(thread_start),
    p as *mut _,
    c::STACK_SIZE_PARAM_IS_A_RESERVATION,
    ptr::null_mut(),
);
let ret = HandleOrNull::from_raw_handle(ret);
return if let Ok(handle) = ret.try_into() {
    Ok(Thread { handle: Handle::from_inner(handle) })
} else {
    // The thread failed to start and as a result p was not consumed. Therefore, it is
    // safe to reconstruct the box so that it gets deallocated.
    drop(Box::from_raw(p));
    Err(io::Error::last_os_error())
};

If drop(Box::from_raw(p)); panics, then the error is not returned. I suggest to replace the drop statement with:

panic::catch_unwind(AssertUnwindSafe(|| drop(Box::from_raw(p))));

Metadata

Metadata

Assignees

Labels

A-threadArea: `std::thread`O-windowsOperating system: WindowsT-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