diff options
| author | Jens Reidel <adrian@travitia.xyz> | 2025-07-20 07:04:58 +0200 |
|---|---|---|
| committer | Jens Reidel <adrian@travitia.xyz> | 2025-08-14 20:37:05 +0200 |
| commit | 5d01d90ad617349109db79cb9980267fd294d735 (patch) | |
| tree | 7c1ff6f7f2acc079bb20431381d8633f5c8cdd9b /library/std/src | |
| parent | 30017c36d6b5e3382ee7cf018d330a6a4a937d39 (diff) | |
| download | rust-5d01d90ad617349109db79cb9980267fd294d735.tar.gz rust-5d01d90ad617349109db79cb9980267fd294d735.zip | |
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 Signed-off-by: Jens Reidel <adrian@travitia.xyz>
Diffstat (limited to 'library/std/src')
| -rw-r--r-- | library/std/src/sys/pal/unix/thread.rs | 13 |
1 files changed, 12 insertions, 1 deletions
diff --git a/library/std/src/sys/pal/unix/thread.rs b/library/std/src/sys/pal/unix/thread.rs index 36e53e7cadc..24b65c11fd2 100644 --- a/library/std/src/sys/pal/unix/thread.rs +++ b/library/std/src/sys/pal/unix/thread.rs @@ -77,7 +77,18 @@ impl Thread { let page_size = os::page_size(); let stack_size = (stack_size + page_size - 1) & (-(page_size as isize - 1) as usize - 1); - assert_eq!(libc::pthread_attr_setstacksize(attr.as_mut_ptr(), stack_size), 0); + + // Some libc implementations, e.g. musl, place an upper bound + // on the stack size, in which case we can only gracefully return + // an error here. + if libc::pthread_attr_setstacksize(attr.as_mut_ptr(), stack_size) != 0 { + assert_eq!(libc::pthread_attr_destroy(attr.as_mut_ptr()), 0); + drop(Box::from_raw(data)); + return Err(io::const_error!( + io::ErrorKind::InvalidInput, + "invalid stack size" + )); + } } }; } |
