Repository navigation
Fix timers firing up to 1ms early - #755
Open
pctablet505 wants to merge 2 commits into
Open
pctablet505 wants to merge 2 commits into
pctablet505 wants to merge 2 commits into
Conversation
pctablet505
marked this pull request as ready for review
July 17, 2026 12:47
libuv resamples its internal clock as the floor of the current monotonic time truncated to whole milliseconds, then schedules a timer for loop->time + timeout. Since the fractional millisecond already elapsed at the moment of sampling is discarded, the timer can fire up to ~1ms before the requested delay has actually passed. This made asyncio.sleep(n) occasionally return slightly under n, as reported in MagicStack#739. Pad the timeout handed to uv_timer_start() by 1ms to compensate, while leaving the reported deadline (get_when()/TimerHandle.when()) as the originally requested value.
pctablet505
force-pushed
the
fix-early-timer-firing
branch
from
October 8, 2026 11:35
66f1983 to
f2b3a0d
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Fixes #739
Under uvloop,
asyncio.sleep(n)can return up to ~1 ms beforenhas actually elapsed. This change pads the timeout passed to libuv by 1 ms, so timers never fire early.Problem
libuv refreshes
loop->timeas the monotonic clock floored to whole milliseconds, anduv_timer_start()schedules the callback atloop->time + timeout. Whatever fraction of a millisecond had already passed when the clock was sampled is lost, so the timer comes due early in real time. The issue showsasyncio.sleep(0.1)returning after0.0991s.Example with a 100 ms timer started at real time 1000.9 ms:
loop->timeat startFix
Only the value handed to libuv changes (
self.timeout + 1).self.timeout, and thereforeTimerHandle.when()/get_when(), still reports the requested deadline. The trade-off is that a timer can fire up to ~1 ms later than before, which is within libuv's 1 ms resolution.This is a different truncation from #233, which was fixed by rounding the float-to-ms conversion in
call_at(). That path is unchanged.Tests
test_call_later_not_earlyrunsasyncio.sleep(0.01)300 times and asserts that each one lasts at least the delay, measured withtime.monotonic(). The existingtest_call_later_rounding(#233) can't catch this, because it measures with the loop's own millisecond-floored clock.