Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion Pyjo/Server/Daemon.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
4 changes: 3 additions & 1 deletion t/Pyjo/Steps.py
Original file line number Diff line number Diff line change
Expand Up @@ -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():
Expand Down
4 changes: 3 additions & 1 deletion t/docs/Steps.py
Original file line number Diff line number Diff line change
Expand Up @@ -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'):

Expand Down
43 changes: 43 additions & 0 deletions t/stdlib/Server_Daemon.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
Loading