Skip to content

Commit 752139c

Browse files
authored
Unrolled build for #144210
Rollup merge of #144210 - Gelbpunkt:thread-stack-size-musl, r=jhpratt std: thread: Return error if setting thread stack size fails Currently, when setting the thread stack size fails, it would be rounded up to the nearest multiple of the page size and the code asserts that the next call to `pthread_attr_setstacksize` succeeds. This may be true for glibc, but it isn't true for musl, which not only enforces a minimum stack size, but also a maximum stack size of `usize::MAX / 4 - PTHREAD_STACK_MIN` [1], triggering the assert rather than erroring gracefully. There isn't any way to handle this properly other than bailing out and letting the user know it didn't succeed. [1]: https://git.musl-libc.org/cgit/musl/tree/src/thread/pthread_attr_setstacksize.c#n5
2 parents c018ae5 + 5d01d90 commit 752139c

File tree

1 file changed

+12
-1
lines changed

1 file changed

+12
-1
lines changed

library/std/src/sys/pal/unix/thread.rs

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,18 @@ impl Thread {
7777
let page_size = os::page_size();
7878
let stack_size =
7979
(stack_size + page_size - 1) & (-(page_size as isize - 1) as usize - 1);
80-
assert_eq!(libc::pthread_attr_setstacksize(attr.as_mut_ptr(), stack_size), 0);
80+
81+
// Some libc implementations, e.g. musl, place an upper bound
82+
// on the stack size, in which case we can only gracefully return
83+
// an error here.
84+
if libc::pthread_attr_setstacksize(attr.as_mut_ptr(), stack_size) != 0 {
85+
assert_eq!(libc::pthread_attr_destroy(attr.as_mut_ptr()), 0);
86+
drop(Box::from_raw(data));
87+
return Err(io::const_error!(
88+
io::ErrorKind::InvalidInput,
89+
"invalid stack size"
90+
));
91+
}
8192
}
8293
};
8394
}

0 commit comments

Comments
 (0)