[PR #2929] [CLOSED] [Engine] serve: the drain's deadline bounds the whole drain again, not just each chunk header #4344

Closed
opened 2026-11-07 15:35:02 +07:00 by bsakaguchi · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/Niko1221/Strata/pull/2929
Author: @ctr-137
Created: 10/17/2026
Status: ❌ Closed

Base: main ← Head: serve-drain-deadline


📝 Commits (2)

  • 9790a4a serve: the drain's deadline bounds the whole drain again, not just each chunk header
  • 921024d serve: the drain's deadline bounds a trickled chunk header, trailer and terminator too

📊 Changes

2 files changed (+181 additions, -9 deletions)

View changed files

📝 serve/server.py (+70 -9)
📝 serve/test_chunked_body.py (+111 -0)

📄 Description

_drain_body's docstring says what the drain promises:

What has not arrived DRAIN_SECONDS later is left unread: the limit is time, so that a conversation of many
megabytes, at any speed the client has, still gets its answer.

The timeout was armed once per chunk, at the header, and the chunk was then read with

piece = self.rfile.read(min(left, 1 << 20))

which waits for that whole buffer inside one call, looping over socket recvs internally. Each recv is bounded; the
call is not. A client that keeps a byte arriving inside the window — comfortably inside it — holds the drain
for ever: one MiB announced at one byte every 0.25 s is about three days, and the announced size is bounded only
by body_limit() (256 MiB by default).

The route only has to answer before reading the body for the drain to run at all, which GET /nope (a 404) does.

The change

The two things that make the bound real are the pair its sibling already uses, in _drain_body's own
Content-Length loop:

  • re-arm the deadline before every read (armed()), so it is checked per socket read and not per chunk;
  • read1, so one iteration is one socket read — the sibling's comment says it: "one socket read: read() would
    wait for it all"
    .

I wrote the first of those two alone first, and it was not enough: the re-armed deadline still sat in front of a
read() that would not come back for days. The test added here is what showed it, and it is what would catch a
regression of either half.

Scope: only the drain passes a deadline, and it reads with keep=False, so every added branch is inert on the
real body path — a chunked body from _body() and the control body both pass no deadline. The connection is
HTTP/1.0 and ends with the answer either way, so ending the drain early can only ever drop a body that was going to
be dropped.

How this was checked

Two cases added to serve/test_chunked_body.py.

The trickle case: a 404 route that reads no body, 1 MiB announced, one byte every 0.25 s, DRAIN_SECONDS 1.0.
Without the fix it fails — the client times out at 10.2 s because the drain never gives up — and with it the
connection is let go in 1.6 s.

The control: a complete 2 MiB chunked body is still read in full, and gets the answer that body deserves (a 400 about
the context, not about the framing). That is there because the re-arming must be inert when nothing rushed the
body.

No regressions: python -m unittest discover -s serve → 850 tests, 2 failures and 1 error, and all three fail on
unmodified main as well
:

  • test_winjob — a tasklist invocation returns None in this environment;
  • test_keepawake — waits on keep-awake callbacks with wait_for, fails alone on main;
  • test_responses — counts threads named (_loop) globally, fails alone on main.

Both of those last two are load- and order-dependent, which is why the same pair of files failed differently in
each arm across runs; each fails on main on its own.

There is no issue for this; it came out of a static audit of the tree rather than a report. Happy to file one if you
would rather triage it there first.

Follow-up from review (midhatn)

The header/extension and trailer lines were still read with readline(), and the two-byte chunk terminator with read(2) — one call may make several recvs, and the timeout armed before it bounds each recv, not the call. With a 200 ms deadline and a byte every 40 ms that was 750 ms (header) and 765 ms (trailer).

read_line() / read_exact() now use read1 and re-arm the deadline before every socket read when a deadline is set; with no deadline the original readline() / read() path is unchanged. Three regression tests cover a trickled header/extension, trailer and terminator.

🤖 Generated with Claude Code


🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.

## 📋 Pull Request Information **Original PR:** https://github.com/Niko1221/Strata/pull/2929 **Author:** [@ctr-137](https://github.com/ctr-137) **Created:** 10/17/2026 **Status:** ❌ Closed **Base:** `main` ← **Head:** `serve-drain-deadline` --- ### 📝 Commits (2) - [`9790a4a`](https://github.com/Niko1221/Strata/commit/9790a4ae6baca81d08d0d9202f51abb7373c9506) serve: the drain's deadline bounds the whole drain again, not just each chunk header - [`921024d`](https://github.com/Niko1221/Strata/commit/921024d0dfd78280ec557d6d866ce1168c829571) serve: the drain's deadline bounds a trickled chunk header, trailer and terminator too ### 📊 Changes **2 files changed** (+181 additions, -9 deletions) <details> <summary>View changed files</summary> 📝 `serve/server.py` (+70 -9) 📝 `serve/test_chunked_body.py` (+111 -0) </details> ### 📄 Description `_drain_body`'s docstring says what the drain promises: > *What has not arrived DRAIN_SECONDS later is left unread: the limit is time, so that a conversation of many > megabytes, at any speed the client has, still gets its answer.* The timeout was armed **once per chunk**, at the header, and the chunk was then read with ```python piece = self.rfile.read(min(left, 1 << 20)) ``` which waits for that whole buffer inside one call, looping over socket recvs internally. Each recv is bounded; the call is not. A client that keeps a byte arriving inside the window — comfortably inside it — holds the drain **for ever**: one MiB announced at one byte every 0.25 s is about three days, and the announced size is bounded only by `body_limit()` (256 MiB by default). The route only has to answer *before* reading the body for the drain to run at all, which `GET /nope` (a 404) does. ### The change The two things that make the bound real are the pair its sibling already uses, in `_drain_body`'s own Content-Length loop: - re-arm the deadline before **every** read (`armed()`), so it is checked per socket read and not per chunk; - `read1`, so one iteration **is** one socket read — the sibling's comment says it: *"one socket read: read() would wait for it all"*. I wrote the first of those two alone first, and it was not enough: the re-armed deadline still sat in front of a `read()` that would not come back for days. The test added here is what showed it, and it is what would catch a regression of either half. **Scope:** only the drain passes a deadline, and it reads with `keep=False`, so every added branch is inert on the real body path — a chunked body from `_body()` and the control body both pass no deadline. The connection is HTTP/1.0 and ends with the answer either way, so ending the drain early can only ever drop a body that was going to be dropped. ### How this was checked Two cases added to `serve/test_chunked_body.py`. The trickle case: a 404 route that reads no body, 1 MiB announced, one byte every 0.25 s, `DRAIN_SECONDS` 1.0. **Without the fix it fails** — the client times out at 10.2 s because the drain never gives up — and with it the connection is let go in 1.6 s. The control: a complete 2 MiB chunked body is still read in full, and gets the answer that body deserves (a 400 about the *context*, not about the framing). That is there because the re-arming must be inert when nothing rushed the body. No regressions: `python -m unittest discover -s serve` → 850 tests, 2 failures and 1 error, and **all three fail on unmodified `main` as well**: - `test_winjob` — a `tasklist` invocation returns `None` in this environment; - `test_keepawake` — waits on keep-awake callbacks with `wait_for`, fails alone on `main`; - `test_responses` — counts threads named `(_loop)` globally, fails alone on `main`. Both of those last two are load- and order-dependent, which is why the same pair of files failed *differently* in each arm across runs; each fails on `main` on its own. There is no issue for this; it came out of a static audit of the tree rather than a report. Happy to file one if you would rather triage it there first. ### Follow-up from review (midhatn) The header/extension and trailer lines were still read with `readline()`, and the two-byte chunk terminator with `read(2)` — one call may make several recvs, and the timeout armed before it bounds each recv, not the call. With a 200 ms deadline and a byte every 40 ms that was 750 ms (header) and 765 ms (trailer). `read_line()` / `read_exact()` now use `read1` and re-arm the deadline before every socket read when a deadline is set; with no deadline the original `readline()` / `read()` path is unchanged. Three regression tests cover a trickled header/extension, trailer and terminator. 🤖 Generated with [Claude Code](https://claude.com/claude-code) --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
bsakaguchi 2026-11-07 15:35:02 +07:00
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
Niko1221/Strata#4344
No description provided.