Skip to content

Commit 93cdf5e

Browse files
committed
Auto merge of #44112 - alexcrichton:thread-join, r=sfackler
std: Handle OS errors when joining threads Also add to the documentation that the `join` method can panic. cc #34971 cc #43539
2 parents 3e96461 + dc7c7ba commit 93cdf5e

File tree

4 files changed

+13
-2
lines changed

4 files changed

+13
-2
lines changed

src/libstd/sys/unix/thread.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,8 @@ impl Thread {
168168
unsafe {
169169
let ret = libc::pthread_join(self.id, ptr::null_mut());
170170
mem::forget(self);
171-
debug_assert_eq!(ret, 0);
171+
assert!(ret == 0,
172+
"failed to join thread: {}", io::Error::from_raw_os_error(ret));
172173
}
173174
}
174175

src/libstd/sys/windows/c.rs

+1
Original file line numberDiff line numberDiff line change
@@ -273,6 +273,7 @@ pub const FILE_END: DWORD = 2;
273273

274274
pub const WAIT_OBJECT_0: DWORD = 0x00000000;
275275
pub const WAIT_TIMEOUT: DWORD = 258;
276+
pub const WAIT_FAILED: DWORD = 0xFFFFFFFF;
276277

277278
#[cfg(target_env = "msvc")]
278279
pub const MAX_SYM_NAME: usize = 2000;

src/libstd/sys/windows/thread.rs

+5-1
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,11 @@ impl Thread {
6161
}
6262

6363
pub fn join(self) {
64-
unsafe { c::WaitForSingleObject(self.handle.raw(), c::INFINITE); }
64+
let rc = unsafe { c::WaitForSingleObject(self.handle.raw(), c::INFINITE) };
65+
if rc == c::WAIT_FAILED {
66+
panic!("failed to join on thread: {}",
67+
io::Error::last_os_error());
68+
}
6569
}
6670

6771
pub fn yield_now() {

src/libstd/thread/mod.rs

+5
Original file line numberDiff line numberDiff line change
@@ -1230,6 +1230,11 @@ impl<T> JoinHandle<T> {
12301230
/// [`Err`]: ../../std/result/enum.Result.html#variant.Err
12311231
/// [`panic`]: ../../std/macro.panic.html
12321232
///
1233+
/// # Panics
1234+
///
1235+
/// This function may panic on some platforms if a thread attempts to join
1236+
/// itself or otherwise may create a deadlock with joining threads.
1237+
///
12331238
/// # Examples
12341239
///
12351240
/// ```

0 commit comments

Comments
 (0)