Skip to content

Commit c318d72

Browse files
committed
std: Fail more gracefully on thread spawn errors
On windows, correctly check for errors when spawning threads, and on both windows and unix handle the error more gracefully rather than printing an opaque assertion failure. Closes #13589
1 parent ce2bab6 commit c318d72

File tree

1 file changed

+17
-4
lines changed

1 file changed

+17
-4
lines changed

Diff for: src/libstd/rt/thread.rs

+17-4
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,7 @@ mod imp {
150150
use libc;
151151
use libc::types::os::arch::extra::{LPSECURITY_ATTRIBUTES, SIZE_T, BOOL,
152152
LPVOID, DWORD, LPDWORD, HANDLE};
153+
use os;
153154
use ptr;
154155
use rt::stack::RED_ZONE;
155156

@@ -168,8 +169,15 @@ mod imp {
168169
// kernel does, might as well make it explicit. With the current
169170
// 20 kB red zone, that makes for a 64 kB minimum stack.
170171
let stack_size = (cmp::max(stack, RED_ZONE) + 0xfffe) & (-0xfffe - 1);
171-
CreateThread(ptr::mut_null(), stack_size as libc::size_t,
172-
super::thread_start, arg, 0, ptr::mut_null())
172+
let ret = CreateThread(ptr::mut_null(), stack_size as libc::size_t,
173+
super::thread_start, arg, 0, ptr::mut_null());
174+
175+
if ret as uint == 0 {
176+
// be sure to not leak the closure
177+
let _p: ~proc():Send = cast::transmute(arg);
178+
fail!("failed to spawn native thread: {}", os::last_os_error());
179+
}
180+
return ret;
173181
}
174182

175183
pub unsafe fn join(native: rust_thread) {
@@ -243,9 +251,14 @@ mod imp {
243251
};
244252

245253
let arg: *libc::c_void = cast::transmute(p);
246-
assert_eq!(pthread_create(&mut native, &attr,
247-
super::thread_start, arg), 0);
254+
let ret = pthread_create(&mut native, &attr, super::thread_start, arg);
248255
assert_eq!(pthread_attr_destroy(&mut attr), 0);
256+
257+
if ret != 0 {
258+
// be sure to not leak the closure
259+
let _p: ~proc():Send = cast::transmute(arg);
260+
fail!("failed to spawn native thread: {}", os::last_os_error());
261+
}
249262
native
250263
}
251264

0 commit comments

Comments
 (0)