Skip to content

Conversation

@joboet
Copy link
Member

@joboet joboet commented Jan 22, 2026

The clock source used for sleep might not be the same as the one used for Instant, so the implementation of sleep_until may not assume that the Instant has elapsed. This is particularly relevant on Windows, where QueryPerformanceCounter is used for Instant, but SetWaitableTimer is (probably1) based on QueryUnbiasedInterruptTimePrecise. If these clocks drift apart, the sleep might return before the deadline has passed.

Thus, this PR makes sleep_until recheck the current time after waking up and restart the sleep if necessary.

Footnotes

  1. The documentation doesn't specify a clock, but QueryUnbiasedInterruptTimePrecise uses the same units and doesn't count time during sleep either, so I'd wager it's what is used for the timer.

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-libs Relevant to the library team, which will review and decide on the PR/issue. labels Jan 22, 2026
@rustbot
Copy link
Collaborator

rustbot commented Jan 22, 2026

r? @jhpratt

rustbot has assigned @jhpratt.
They will have a look at your PR within the next two weeks and either review your PR or reassign to another reviewer.

Use r? to explicitly pick a reviewer

@rust-log-analyzer

This comment has been minimized.

@joboet joboet force-pushed the sleep_until_ensure_elapsed branch from 4a976c5 to 978c5f5 Compare January 22, 2026 15:55
@jhpratt
Copy link
Member

jhpratt commented Jan 23, 2026

The docs are incredibly clear that it will never wake before the deadline and that it may sleep longer, so this absolutely seems like the right thing to do.

@bors r+

@rust-bors
Copy link
Contributor

rust-bors bot commented Jan 23, 2026

📌 Commit 978c5f5 has been approved by jhpratt

It is now in the queue for this repository.

@rust-bors rust-bors bot added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Jan 23, 2026
rust-bors bot pushed a commit that referenced this pull request Jan 23, 2026
Rollup of 4 pull requests

Successful merges:

 - #151450 (std: use `clock_nanosleep` for `sleep` where available)
 - #151494 (std: ensure that the deadline has passed in `sleep_until`)
 - #151498 (global.rs: improve readability of re-entrance section)
 - #151504 (Reorganizing tests/ui/issues 11 tests [3/N])

r? @ghost
@rust-bors rust-bors bot merged commit 15ce06c into rust-lang:main Jan 23, 2026
11 checks passed
@rustbot rustbot added this to the 1.95.0 milestone Jan 23, 2026
rust-timer added a commit that referenced this pull request Jan 23, 2026
Rollup merge of #151494 - joboet:sleep_until_ensure_elapsed, r=jhpratt

std: ensure that the deadline has passed in `sleep_until`

The clock source used for `sleep` might not be the same as the one used for `Instant`, so the implementation of `sleep_until` may not assume that the `Instant` has elapsed. This is particularly relevant on Windows, where [`QueryPerformanceCounter`](https://learn.microsoft.com/en-us/windows/win32/api/profileapi/nf-profileapi-queryperformancecounter) is used for `Instant`, but [`SetWaitableTimer`](https://learn.microsoft.com/en-us/windows/win32/api/synchapi/nf-synchapi-setwaitabletimer) is (probably[^1]) based on [`QueryUnbiasedInterruptTimePrecise`](https://learn.microsoft.com/en-us/windows/win32/api/realtimeapiset/nf-realtimeapiset-queryunbiasedinterrupttimeprecise). If these clocks drift apart, the `sleep` might return before the deadline has passed.

Thus, this PR makes `sleep_until` recheck the current time after waking up and restart the `sleep` if necessary.

[^1]: The documentation doesn't specify a clock, but `QueryUnbiasedInterruptTimePrecise` uses the same units and doesn't count time during sleep either, so I'd wager it's what is used for the timer.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. T-libs Relevant to the library team, which will review and decide on the PR/issue.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants