std: ensure that the deadline has passed in sleep_until

This commit is contained in:
joboet
2026-01-20 11:27:56 +01:00
parent 838db25382
commit 978c5f567e

View File

@@ -139,9 +139,16 @@ cfg_select! {
pub fn sleep_until(deadline: crate::time::Instant) {
use crate::time::Instant;
let now = Instant::now();
if let Some(delay) = deadline.checked_duration_since(now) {
// The clock source used for `sleep` might not be the same used for `Instant`.
// Since this function *must not* return before the deadline, we recheck the
// time after every call to `sleep`. See #149935 for an example of this
// occurring on older Windows systems.
while let Some(delay) = deadline.checked_duration_since(Instant::now()) {
// Sleep for the estimated time remaining until the deadline.
//
// If your system has a better way of estimating the delay time or
// provides a way to sleep until an absolute time, specialize this
// function for your system.
sleep(delay);
}
}