std: remove at-least guarantee from sleep
#151407
Closed
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
#149935 demonstrates an issue with
thread::sleepon Windows wheresleepreturns early, before the given duration has elapsed. The proposed fix (#150842) only addresses parts of the problem however – the fundamental issue is that the Windows timers all use a different clock source than the performance counter used byInstant, necessitating conversions that cannot always be performed accurately: IfQueryPerformanceFrequencyoverestimates the frequency, time as measured byInstantwill advance slower than the time used forSetWaitableTimer, resulting insleepreturning early for very long duration sleeps.This problem is not even Windows-specific, e.g.
nanosleepis actually specified to useCLOCK_REALTIME, but without respecting clock adjustments, so ifCLOCK_MONOTONIChas a slight drift,nanosleepmight return early on some UNIXes as well (Linux actually usesCLOCK_MONOTONICfornanosleepso it is not affected). Actually, most our lower-tier targets don't guarantee any synchronicity between their sleep syscall and their time syscall.It is of course possible to remediate this problem by implementing
sleepas e.g.However, this adds a lot of overhead to what is supposed to be a simple function. Most users call
sleepwith the intention of just sleeping for a bit and don't care about precise timing guarantees – or else they would have noticed these issues.Thus, I'd like to propose that we remove the guarantee that
from
sleepand recommend that users usesleep_until(#113752) if they need specific timing guarantees1.sleep_untilis very much built for precise timing, uses the same clock asInstanton most UNIXes and is not affected by issues like oversleeping due to a high number of signals2 or not being scheduled for a long time.This is actually not really a breaking change: The current
sleepdocumentation does not specify the clock used3, so the change in this PR could also be phrased as saying that the clock used forsleepis not guaranteed to be the same as that ofInstant. I just find it clearer to remove the guarantee, as the guarantee is basically worthless without specifying a time source.r? libs-api
@rustbot label +I-libs-api-nominated
Footnotes
Which should be stabilised ASAP if this PR is merged. ↩
See https://manned.org/man/arch/nanosleep.2#head10 ↩
It is however implied by the example that it is the same as that of
Instant. ↩