Rollup merge of #129754 - alexcrichton:fix-wasi-long-sleep, r=workingjubilee

wasi: Fix sleeping for `Duration::MAX`

This commit fixes an assert in the WASI-specific implementation of thread sleep to ensure that sleeping for a very large period of time blocks instead of panicking. This can come up when testing programs that sleep "forever", for example.

I'll note that I haven't included a test for this since it's sort of difficult to test. I've tested this locally though that long sleeps do indeed block and short sleeps still only sleep for a short amount of time.
This commit is contained in:
Matthias Krüger 2024-08-31 10:08:59 +02:00 committed by GitHub
commit 25e3b66410
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -136,17 +136,17 @@ pub fn set_name(_name: &CStr) {
}
pub fn sleep(dur: Duration) {
let nanos = dur.as_nanos();
assert!(nanos <= u64::MAX as u128);
let mut nanos = dur.as_nanos();
while nanos > 0 {
const USERDATA: wasi::Userdata = 0x0123_45678;
let clock = wasi::SubscriptionClock {
id: wasi::CLOCKID_MONOTONIC,
timeout: nanos as u64,
timeout: u64::try_from(nanos).unwrap_or(u64::MAX),
precision: 0,
flags: 0,
};
nanos -= u128::from(clock.timeout);
let in_ = wasi::Subscription {
userdata: USERDATA,
@ -169,6 +169,7 @@ pub fn sleep(dur: Duration) {
}
}
}
}
pub fn join(self) {
cfg_if::cfg_if! {