Skip to content

Commit 9c5d97f

Browse files
author
Konstantin Vlasov
committed
asyncio: Use adjusted clock resolution for next tick
asyncio event loop uses monotonic timer which in many systems is the OS uptime. With low enough clock resolution (often being 1e-09) and high enough uptime (~194 days), adding the "clock tick" to the current time hits the floating point precision limits and does not change the time value. The comparison then returns invalid result, and tasks scheduled to trigger at this exact moment are not triggered. They will be triggered only later, at the next call. This commit fixes the issue by making sure the "next tick" is adjusted to the current time's floating point precision. Therefore, end_time is guaranteed to be incremented.
1 parent f31ac36 commit 9c5d97f

1 file changed

Lines changed: 5 additions & 1 deletion

File tree

Lib/asyncio/base_events.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
import sys
3030
import warnings
3131
import weakref
32+
import math
3233

3334
try:
3435
import ssl
@@ -2022,7 +2023,10 @@ def _run_once(self):
20222023
event_list = None
20232024

20242025
# Handle 'later' callbacks that are ready.
2025-
end_time = self.time() + self._clock_resolution
2026+
now = self.time()
2027+
# If clock resolution is too small, make sure end_time has the minimal
2028+
# possible increment
2029+
end_time = now + max(self._clock_resolution, math.ulp(now))
20262030
while self._scheduled:
20272031
handle = self._scheduled[0]
20282032
if handle._when >= end_time:

0 commit comments

Comments
 (0)