From 9d2d534cde52d17f44dd05dbfcd47235905d02af Mon Sep 17 00:00:00 2001 From: Piotr Roszatycki Date: Thu, 5 Nov 2026 14:59:12 +0100 Subject: [PATCH 1/2] Tests: a sleep of Pyjo.Steps on an event loop of libuv, whose timer may be due a millisecond early Co-Authored-By: Claude Opus 5.5 --- t/Pyjo/Steps.py | 4 +++- t/docs/Steps.py | 4 +++- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/t/Pyjo/Steps.py b/t/Pyjo/Steps.py index db65057b..e79f099e 100644 --- a/t/Pyjo/Steps.py +++ b/t/Pyjo/Steps.py @@ -167,7 +167,9 @@ async def nap(seconds): Pyjo.IOLoop.remove(timer) value, slept = result['value'] is_ok(value, None, 'nothing, non-blocking') - ok(slept >= 0.2, 'slept, non-blocking') + # A timer of an event loop on libuv, as the ones of uvloop and gevent, counts whole milliseconds of a clock that + # libuv rounds down, so it may be due up to a millisecond early by time.monotonic + ok(slept >= 0.19, 'slept, non-blocking') ok(len(ticks) >= 3, 'other events meanwhile') async def retried(): diff --git a/t/docs/Steps.py b/t/docs/Steps.py index 38aa70ad..7dd29019 100644 --- a/t/docs/Steps.py +++ b/t/docs/Steps.py @@ -117,7 +117,9 @@ async def sleeps(): ok(Steps(sleeps()).run(ua) >= 0.6, 'sleeps, blocking') result = [] Pyjo.Steps.new(sleeps()).run_p(ua).then(result.append).wait() - ok(result[0] >= 0.6, 'sleeps, non-blocking') + # A timer of an event loop on libuv, as the ones of uvloop and gevent, counts whole milliseconds of a clock that + # libuv rounds down, so each one may be due up to a millisecond early by time.monotonic + ok(result[0] >= 0.59, 'sleeps, non-blocking') with subtest('Steps'): From fff6578dfe0cf0fd404c0af29d13c6273bc98c4b Mon Sep 17 00:00:00 2001 From: Piotr Roszatycki Date: Thu, 5 Nov 2026 15:17:08 +0100 Subject: [PATCH 2/2] Pyjo.Server.Daemon: the data of a connection that the daemon has removed, whose stream reads on while it closes gracefully, is discarded Co-Authored-By: Claude Opus 5.5 --- Pyjo/Server/Daemon.py | 6 +++++- t/stdlib/Server_Daemon.py | 43 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 48 insertions(+), 1 deletion(-) diff --git a/Pyjo/Server/Daemon.py b/Pyjo/Server/Daemon.py index a0711b7d..f73dc57f 100644 --- a/Pyjo/Server/Daemon.py +++ b/Pyjo/Server/Daemon.py @@ -914,8 +914,12 @@ def _error(self, id: str, err: Any) -> None: self._close(id) def _read(self, id: str, chunk: bytes) -> None: + # Unlike Mojolicious: the data of a connection that the daemon has removed, whose stream reads on while it closes + # gracefully, is discarded, where Mojo makes the connection again and builds a transaction of it + if (c := self._connections.get(id)) is None: + return + # Make sure we have a transaction - c = self._connections[id] if (fcgi := c.get('fcgi')) is not None: return fcgi.read(chunk) if (tx := c.get('tx')) is None: diff --git a/t/stdlib/Server_Daemon.py b/t/stdlib/Server_Daemon.py index bcfc039b..594c2cdd 100644 --- a/t/stdlib/Server_Daemon.py +++ b/t/stdlib/Server_Daemon.py @@ -261,6 +261,49 @@ def client(): ok(read.is_set(), 'request read') is_deeply_ok(errors, [], 'no error of the reactor') +with subtest('Data of a connection that the daemon has removed'): + # The stream of a connection that the daemon removes closes gracefully, as long as it is writing, and reads on, as + # while a callback of drain of another write, which comes in the emit of drain of the last one, waits for the next one + finish = Pyjo.Server.Daemon.object._finish + streams = [] + + def kept_writing(self, id): + stream = self.ioloop.stream(id) + streams.append(stream) + + # Writing until the data that the client sends after the response has been read, or a second, as the error + # of the daemon stops the event read before the subscriber of the test + def keep(stream): + pass + + def stop(*args): + stream.unsubscribe('drain', keep) + + stream.on('drain', keep) + stream.on('read', stop) + self.ioloop.timer(1, stop) + return finish(self, id) + + def client(): + with socket.create_connection(('127.0.0.1', port)) as conn: + conn.settimeout(10) + conn.sendall(b'GET /gone HTTP/1.1\r\nHost: 127.0.0.1\r\nConnection: close\r\n\r\n') + response = b'' + while b'\r\n\r\n' not in response or not response.endswith(b'0'): + response += conn.recv(4096) + conn.sendall(b'more') + while conn.recv(4096): + pass + return response + + errors = [] + error = Pyjo.IOLoop.singleton.reactor.on('error', lambda reactor, err: errors.append(repr(err))) + with patch.object(Pyjo.Server.Daemon.object, '_finish', kept_writing): + like_ok(run(client), rb'\r\n\r\nGET /gone 1\.1 0$', 'response') + Pyjo.IOLoop.singleton.reactor.unsubscribe('error', error) + ok(streams and streams[0].handle is None, 'stream closed after the data') + is_deeply_ok(errors, [], 'no error of the reactor') + with subtest('Context manager'): with ( Pyjo.IOLoop.new() as loop,