Skip to content

Tests: a sleep of Pyjo.Steps on an event loop of libuv, whose timer may be due a millisecond early - #553

Merged
dex4er merged 1 commit into
mainfrom
tests-timer-margin
Nov 5, 2026
Merged

dex4er merged 1 commit into
mainfrom
tests-timer-margin

Conversation

@dex4er

@dex4er dex4er commented Nov 5, 2026 •

Copy link
Copy Markdown
Collaborator

The tests of a Sleep of Pyjo.Steps from #541, non-blocking, measure it with time.monotonic and fail now and then on the reactors of an event loop on libuv: on CI of the distributions, t/docs/Steps.py and t/Pyjo/Steps.py failed on Pyjo.Reactor.Uvloop with Python 3.12 and 3.13, and on Pyjo.Reactor.Gevent with GEVENT_LOOP=libuv-cffi with Python 3.13 and 3.14.

libuv counts a timer in whole milliseconds of uv_now, its clock rounded down, so a timer may be due up to a millisecond before its time by time.monotonic. That is MagicStack/uvloop#739, which MagicStack/uvloop#755 fixes in uvloop, not released yet, and which pyjoyment/Pyjo-Reactor-Uvloop#8 works around in its reactor; gevent counts its timers on libuv the same way. The cause is in the event loop, outside Pyjoyment; the tests of Pyjo.Steps check that a sleep sleeps and that the loop runs the other events meanwhile, not the resolution of the clock of an event loop.

Changes

  • t/Pyjo/Steps.py: slept, non-blocking takes 0.19 seconds for a sleep of 0.2.
  • t/docs/Steps.py: sleeps, non-blocking takes 0.59 seconds for two sleeps of 0.5 and 0.1.

The blocking ones stay, as run sleeps with time.sleep, which is never early. t/Pyjo/Server_WSGI.py, next chunk after the timer, has a margin already: 0.4 for a timer of 0.5.

Validation

  • Both tests with PYTHONDEVMODE=1 on the default reactor, Pyjo.Reactor.Poll, Pyjo.Reactor.Asyncio and Pyjo.Reactor.Uvloop, and three times on Pyjo.Reactor.Gevent with GEVENT_LOOP=libuv-cffi, on macOS
  • trunk check
  • CI

🤖 Generated with Claude Code

…ay be due a millisecond early

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@dex4er
dex4er enabled auto-merge (squash) November 5, 2026 14:00
@dex4er
dex4er merged commit a8dfa92 into main Nov 5, 2026
26 checks passed
@dex4er
dex4er deleted the tests-timer-margin branch November 5, 2026 14:09
@github-actions

github-actions Bot commented Nov 5, 2026

Copy link
Copy Markdown
Contributor

Tests

Job Tests Passed ✅ Skipped ⚠️ Failed ❌ Log
prove (python 3.12, Asyncio) 7368 7340 28 0 ✅ 127290373414
prove (python 3.12, Poll) 7340 7310 30 0 ✅ 127290373248
prove (python 3.13, Asyncio) 7368 7340 28 0 ✅ 127290373554
prove (python 3.13, Poll) 7340 7310 30 0 ✅ 127290372528
prove (python 3.14, Asyncio) 7399 7376 23 0 ✅ 127290373160
prove (python 3.14, Poll) 7371 7346 25 0 ✅ 127290373436
prove (python 3.15, Asyncio) 7399 7376 23 0 ✅ 127290372527
prove (python 3.15, Poll) 7371 7346 25 0 ✅ 127290372596
prove (python 3.15t, Asyncio) 7399 7376 23 0 ✅ 127290372738
prove (python 3.15t, Poll) 7371 7346 25 0 ✅ 127290372547
prove (python mise, Asyncio) 7478 7444 34 0 ✅ 127290372602
prove (python mise, Asyncio, Windows) 7205 7141 64 0 ✅ 127290372730
prove (python mise, Poll) 7450 7414 36 0 ✅ 127290372211
prove (python mise, Poll, Windows) 7177 7111 66 0 ✅ 127290372572
prove (python pypy3.12, Asyncio) 7368 7340 28 0 ✅ 127290372830
prove (python pypy3.12, Poll) 7340 7310 30 0 ✅ 127290372517
pytest (python 3.12, ubuntu-26.04-arm) 434 409 25 0 ✅ 127290373221
pytest (python 3.15, ubuntu-26.04-arm) 434 414 20 0 ✅ 127290372608
pytest (python 3.15, windows-2025) 434 387 47 0 ✅ 127290373094
servers (python 3.12) 45 45 0 0 ✅ 127290372877
servers (python 3.15) 44 44 0 0 ✅ 127290372876
All 119135 118525 610 0 ✅

The run of the workflow

dex4er added a commit to pyjoyment/Pyjo-Reactor-Winloop that referenced this pull request Nov 5, 2026
Pyjoyment of Git main with pyjoyment/Pyjoyment#553, a sleep of
Pyjo.Steps in its tests on an event loop of libuv, whose timer may be
due a millisecond early, and pyjoyment/Pyjoyment#554, the data of a
connection that the daemon has removed discarded.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
dex4er added a commit to pyjoyment/Pyjo-Reactor-Winloop that referenced this pull request Nov 5, 2026
…ime, and a timer not due before its time (#6)

* Pyjo.Reactor.Winloop: a socket that loops watch, polled by one at a time

libuv of Windows polls a socket with an exclusive request of AFD, which
ends the request of any other loop for the same socket, also of a copy
of it, and that loop polls it again, in a busy loop of C with no event
at all. The workers of Pyjo.Server.Prethread watch the listen sockets of
the manager on Windows, the same sockets in every thread: four idle
loops took 240% of the processors and starved the process and the tests
next to it, with or without the GIL.

_Loop keeps the readers and the writers of each socket, and a registry
of the process has the loops that watch it, the owner that libuv polls
it for, and the loops that run and wait for it. The owner hands it over
to the next one after each of its events, so that the connections go to
every worker in turn, and at the end of its run; a loop changes the
polls of libuv only in its thread. A socket that one loop watches goes
to libuv as before.

Closes #5

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* uv.lock: Pyjoyment of Git main at 1fc984a

Pyjoyment of Git main with pyjoyment/Pyjoyment#550 and
pyjoyment/Pyjoyment#552, the tests of a page of GitHub without its title
in the tutorial and of the servers of Hot and Reload that did not answer
or did not stop.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* uv.lock: Pyjoyment of Git main at 7b09754

Pyjoyment of Git main with pyjoyment/Pyjoyment#553, a sleep of
Pyjo.Steps in its tests on an event loop of libuv, whose timer may be
due a millisecond early, and pyjoyment/Pyjoyment#554, the data of a
connection that the daemon has removed discarded.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* Pyjo.Reactor.Winloop: a timer not due before its time

libuv counts a timer in whole milliseconds from uv_now, its clock
rounded down, and Winloop rounds the delay to them, as uvloop does, so a
timer, as asyncio.sleep, was due up to a millisecond before its time,
where the loop of asyncio is never early: a sleep of 2 ms took 1.5 ms.
call_later of _Loop, which call_at of Winloop calls too, rounds the
delay up to a millisecond and adds the one that the clock loses, as
_Loop of Pyjo.Reactor.Uvloop does, after MagicStack/uvloop#755.

The subtest of Pyjo-Reactor-Uvloop measures with time.perf_counter,
which time.monotonic is on Windows since Python 3.13, and which counted
in the ticks of the system of 15.6 ms before.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

---------

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
@dex4er dex4er added this to the 0.3.0 milestone Nov 6, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant