Skip to content

Return unexpected termination error instead of panicing in Thread::join #137480

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

Merged
merged 3 commits into from
Feb 28, 2025
Merged
Changes from 1 commit
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
6 changes: 5 additions & 1 deletion library/std/src/thread/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1739,7 +1739,11 @@ struct JoinInner<'scope, T> {
impl<'scope, T> JoinInner<'scope, T> {
fn join(mut self) -> Result<T> {
self.native.join();
Arc::get_mut(&mut self.packet).unwrap().result.get_mut().take().unwrap()
if let Some(packet) = Arc::get_mut(&mut self.packet) {
packet.result.get_mut().take().unwrap()
} else {
Err(Box::new("thread terminated unexpectedly (e.g. due to OS intervention)"))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's not speculate if we don't have anything else to go on:

Suggested change
Err(Box::new("thread terminated unexpectedly (e.g. due to OS intervention)"))
Err(Box::new("thread terminated unexpectedly"))

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure if this is the right type to return.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I do think it'd be better to return an impl Error {} type that implements Display rather than just a Box<str>.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm. The std::thread::Result type states that its Err variant indicates that a thread has panicked with. So now it seems to me that I'm breaking the API, technically.

We should either use .except(..) instead, or change the documentation of std::thread::Result.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems doubtful to me anyone relies on specifically that element of the API guarantee such that they can distinguish between unexpected termination and panic. Nonetheless you are correct this would technically be an API change, so I think that I could accept the .expect and then libs-team ACP could be pursued appropriately.

}
}
}

Expand Down
Loading