Auto merge of #2466 - RalfJung:longsleep, r=RalfJung

fix an ICE in nanosleep()
This commit is contained in:
bors 2022-08-06 00:52:42 +00:00
commit 42087647d6
2 changed files with 23 additions and 1 deletions

View File

@ -210,7 +210,11 @@ fn nanosleep(
return Ok(-1); return Ok(-1);
} }
}; };
let timeout_time = Time::Monotonic(Instant::now().checked_add(duration).unwrap()); // If adding the duration overflows, let's just sleep for an hour. Waking up early is always acceptable.
let timeout_time = Instant::now()
.checked_add(duration)
.unwrap_or_else(|| Instant::now().checked_add(Duration::from_secs(3600)).unwrap());
let timeout_time = Time::Monotonic(timeout_time);
let active_thread = this.get_active_thread(); let active_thread = this.get_active_thread();
this.block_thread(active_thread); this.block_thread(active_thread);

18
tests/pass/sleep_long.rs Normal file
View File

@ -0,0 +1,18 @@
//@ignore-target-windows: no threads nor sleep on Windows
//@compile-flags: -Zmiri-ignore-leaks -Zmiri-disable-isolation
use std::sync::{Arc, Mutex};
use std::thread;
use std::time::Duration;
fn main() {
let finished = Arc::new(Mutex::new(false));
let t_finished = finished.clone();
thread::spawn(move || {
// Sleep very, very long.
thread::sleep(Duration::new(u64::MAX, 0));
*t_finished.lock().unwrap() = true;
});
thread::sleep(Duration::from_millis(100));
assert_eq!(*finished.lock().unwrap(), false);
// Stopping the main thread will also kill the sleeper.
}