Skip to content

Time out requests whose headers or body never arrive - #179

Merged
A21-devs merged 2 commits into
mainfrom
fix/request-body-timeout
Dec 5, 2026
Merged

A21-devs merged 2 commits into
mainfrom
fix/request-body-timeout

Conversation

@A21-devs

@A21-devs A21-devs commented Dec 5, 2026 •

Copy link
Copy Markdown
Owner

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_request catches the timeout and closes) instead of holding a thread. For the headers this is per read, not in total (see the review).
  • The body has a deadline for the whole read, not just per recv, so a client sending a byte at a time can't stretch it out. The read loop that _discard_body already had (Read the body before answering a POST with an error #110) moved into _read_body(length, seconds, keep); _read_json uses it with REQUEST_SECONDS and answers 408 the request body didn't arrive within 10s. _discard_body keeps its 2 s and drops the bytes as before.
  • _send_file sets 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).
  • New tests in tests/test_server.py (real sockets against make_server, REQUEST_SECONDS patched down):
    • headers plus a partial body: 408 with the message, in well under 3 s
    • body sent one byte every 50 ms: 408 after the deadline, not after the 4.6 s it would take to send everything
    • request line and part of the headers, no blank line: the connection closes
    • a body sent in two parts 0.4 s apart within a 2 s limit: still 201
    • a 48 MB file read with a 64 KB receive buffer, paused for 1 s (over three times the 0.3 s timeout), then read to the end: every byte arrives
    • I ran the five new tests 8 times in a row to check for flakes (the byte-at-a-time test stops sending once the reply is readable, so unread bytes can't make Windows reset the connection and lose the 408).
  • The repro from the issue against a real clipwright serve --port 8799:
    • before: recv timed out on the client after 8 s and the handler stayed blocked
    • after: HTTP/1.0 408 Request Timeout with {"error": "the request body didn't arrive within 10s"} after 10.0 s
    • a request with unfinished headers: the client sees the connection close after 10 s, and GET /api/jobs still answers 200 right after

@A21-devs A21-devs added the bug Something isn't working label Dec 5, 2026
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 A21-devs left a comment

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Checked:

  • handle_one_request wraps the whole request, method() and the final flush included, in except 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_json sets _body_read before reading, so _error doesn't try to drain a body that already timed out and doesn't wait another 2 s.
  • _discard_body behaves the same as before, with the same 2 s deadline, and the bytes are dropped without being kept.
  • rfile.read1 returns 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_file replaced by pass, 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.

Comment thread src/clipwright/server.py
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)

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@A21-devs

A21-devs commented Dec 5, 2026

Copy link
Copy Markdown
Owner Author

Pushed b99e5df: the comment in setup() now says the header timeout is per read and why the stdlib makes a total deadline awkward there. The PR description is corrected the same way. No behavior change, tests/test_server.py 122 passed, ruff clean.

@A21-devs A21-devs left a comment

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@A21-devs
A21-devs merged commit f8bbf03 into main Dec 5, 2026
4 checks passed
@A21-devs
A21-devs deleted the fix/request-body-timeout branch December 5, 2026 00:46
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

A POST whose body never arrives holds a server thread forever

1 participant