Repository navigation
Time out requests whose headers or body never arrive - #179
Conversation
The handler's socket had no timeout, so a client that sent the headers and less body than its Content-Length held a server thread forever. Reading the request now times out after REQUEST_SECONDS (10 s), the body has a deadline for the whole read and gets a 408. File downloads lift the timeout so a paused video isn't cut off.
A21-devs
left a comment
There was a problem hiding this comment.
Checked:
handle_one_requestwraps the whole request,method()and the final flush included, inexcept TimeoutError, so a header timeout or a write that times out on a JSON response closes the connection quietly. No traceback ends up in the serve console.- The 408 path:
_read_jsonsets_body_readbefore reading, so_errordoesn't try to drain a body that already timed out and doesn't wait another 2 s. _discard_bodybehaves the same as before, with the same 2 s deadline, and the bytes are dropped without being kept.rfile.read1returns whatever the header parse already buffered first, so a body that came in the same packet as the headers isn't lost.- The paused-video test does catch a regression: with the
settimeout(None)line in_send_filereplaced bypass, it fails (the server's send times out after 0.3 s and the client gets a short file).
One limitation, inline. Nothing else to change.
| super().setup() | ||
| # reading the request line and headers times out (handle_one_request then just closes the | ||
| # connection), the body has its own deadline in _read_body. _send_file lifts it. | ||
| self.connection.settimeout(REQUEST_SECONDS) |
There was a problem hiding this comment.
This bounds each recv of the request line and headers, not the total. A client that sends a header byte every 9 s still keeps the thread, because the stdlib reads headers with readline and there's no hook for a deadline. The body has a real deadline now, so that's the only gap left. For a localhost server whose clients are our own page and MCP server, a client that dies mid-request is the case that matters, and this covers it. I wouldn't change anything here, but the docstring/PR shouldn't claim more than per-read for the headers.
|
Pushed b99e5df: the comment in |
A21-devs
left a comment
There was a problem hiding this comment.
Second pass over b99e5df. The only finding was that the header timeout is per read, and nothing in the code needed to change for it. The comment in setup() now says per read and explains why, and the PR description was corrected to match. b99e5df only touches that comment. tests/test_server.py passes on it (122). Nothing left from the first review.
Closes #113.
What
REQUEST_SECONDS = 10.0.Handler.setup()puts it on the socket after the stream handler's setup, so a client that stops sending in the middle of its request line or headers gets disconnected after 10 s without data (handle_one_requestcatches the timeout and closes) instead of holding a thread. For the headers this is per read, not in total (see the review).recv, so a client sending a byte at a time can't stretch it out. The read loop that_discard_bodyalready had (Read the body before answering a POST with an error #110) moved into_read_body(length, seconds, keep);_read_jsonuses it withREQUEST_SECONDSand answers 408the request body didn't arrive within 10s._discard_bodykeeps its 2 s and drops the bytes as before._send_filesets the socket back to no timeout before streaming. A paused<video>stops reading and the send blocks until it resumes, which isn't a dead client.10 s is generous for a localhost JSON body of at most 64 KB, and short enough that a crashed page or a killed MCP server doesn't pile up threads.
Testing
uv run ruff check src tests,uv run mypy,uv run pytest -q tests/test_server.py: 122 passed (the full suite runs in CI).tests/test_server.py(real sockets againstmake_server,REQUEST_SECONDSpatched down):clipwright serve --port 8799:recvtimed out on the client after 8 s and the handler stayed blockedHTTP/1.0 408 Request Timeoutwith{"error": "the request body didn't arrive within 10s"}after 10.0 sGET /api/jobsstill answers 200 right after