~ knotless Search/
↑↓ move↵ open⇧↵ all results
0
0

pds-server: a body left unread is read after the answer, its connection kept

Merged
Merged chadtmiller.com asked to merge into main from chadtmiller.com/monolith:limits/refused-body-connection

The 2026-11-19 nightly failed on a "socket hang up" in a crash run. The server answered an oversized OAuth sign-in with 413 and closed the connection without saying so, and the harness sent its next request on that closed connection. Now the server reads and throws away whatever is left of a body it answered without (up to 4 MiB), so the connection stays usable. A new harness check fails every time against the old server.

Claude Opus 5.5 · high

What changes

Spec. spec/LIMITS.md, "Request size": when a request is answered before its body was read to its end and its Content-Length leaves at most 4 MiB unread, the connection is kept. The server reads the rest after the answer and throws it away. A body with more left than that, or one sent chunked, gets Connection: close, and the connection is closed after the answer. "How it is checked" lists the new check.

Harness. A new random step, refusedBodies in harness/checks/limits.mjs (weight 2). It sends one of four requests, each with a Content-Length and on a raw connection of its own:

  • OAuth's sign-in with a body one byte past 100 KiB;
  • a sign-in of a few bytes refused for its CSRF token;
  • PAR with a body one byte past 100 KiB;
  • createRecord one byte past its cap.

On the server, the same connection must then answer GET /xrpc/_health, sent 100 ms later. The reference may do that or say Connection: close; it keeps the connection in each case seen. Statuses are compared.

Server. In limits::serve, the Idle body wrapper records when the body was read to its end. If a handler drops the body before then, the wrapper hands what's left back to serve. After the answer is ready, serve spawns drain to read and throw away the rest when the Content-Length leaves at most DRAIN (4 MiB), stopping if it sends nothing for 30 s. Otherwise it adds Connection: close to the answer.

Why

Nightly crash failed at --seed 21 --crash-every 15, step 186: createInviteCodes came back request failed: socket hang up on the server's side and 200 on the reference's. The server's log ends at the step before, an OAuth sign-in answered 413 in 0.04 ms. The createInviteCodes request never reached the server.

oauth::read_body (PAR and the sign-in API) refuses by Content-Length without reading the body. When the body is dropped unread, hyper tries one read; if the body isn't complete it gives up and closes the connection after the answer. It often notices only after writing the answer's head, so the answer goes out with keep-alive and no Connection: close. Node's HTTP agent, which the harness's send and Browser.request use, keeps that socket for the next request. If that request leaves before Node sees the close, it goes out on a closed connection. Whether it fails depends on timing.

The first version of this pull only added Connection: close whenever a body was left unread. That also closed connections hyper used to keep: small bodies refused before being read, whose bytes hyper's one read had already taken. A draft pull that printed each close (closed now) showed these on the runner: passkey-register and sign-in 400s, and POST /oauth/passkey 404s, sent by the random steps' passkey and sign-in checks. Reading the rest of the body is what the reference does. It keeps those connections and closes none that hyper used to keep, so this version does that.

How it was checked

  • node compare.mjs --steps 40 --seed 1 --only refusedBodies, against three servers:
    • main: fails at step 1. PAR's 413 says Connection: close, which the check no longer accepts for a body this small.
    • The first version (close only): fails the same way.
    • This one: all 40 steps agree, all four kinds included. The reference kept the connection in every case.
  • Bug planted: the two servers above are the plants, main's handling and the close-only handling, and each fails the check. Earlier, against main's server, the sign-in case failed with read ECONNRESET on the next request.
  • Pages runs: the first version's gates failed --pages --seed 1 three times at its passkey flows (step 31), each on a different check, and passed it on the draft pull. With this version's server, instrumented, no request in a pages run here was drained or closed, so the pages run does not reach the new code. Here, --pages --seed 1 passed 8 runs out of 10 with this version, the 4 at once among them, and 10 out of 10 with main's server. The 2 failures were in the passkey flows: a navigation interrupted twice, and a sign-in page whose assets never loaded.
  • The gates are on the pull.

Shortcomings

  • A body with more than 4 MiB left, or one sent chunked, still closes its connection. The answer now says so, but a slow client may get a reset before it reads the answer.
  • Draining after the answer means the server reads up to 4 MiB a request it refused, the same bound as the XRPC drain before it.
  • Adding a step to the random steps changes the requests every seed makes from here on.
Approved

This pull makes limits::serve add Connection: close to any answer given before the request body was read to its end. That fixes the nightly "socket hang up". The Idle wrapper's ended flag starts true for bodyless requests, so GETs and the firehose upgrade are not affected. I found no bugs.

  • Spec: spec/LIMITS.md carries the new rule and the new check, and the changeset is written for people who run or call the server.
  • Harness: refusedBodies is a new check and does not loosen an existing one. It names what it compares. The helpers it uses (connect, sized, reach) exist.
  • Server code: it uses no atproto or MCP crates.

I did not run anything; the review is from reading the diff.

Approved

Nothing changed in the pull itself since my approval at 7d680e7. The new head adds a changeset, .changeset/lettre-0-11-24.md, and a Cargo.lock bump of lettre to 0.11.24 and email-encoding to 0.4.3. These look like main's changes merged in, not this pull's. They are patch-level dependency updates, and the changeset is written for people who run the server. They don't touch the limits::serve fix, the spec or the refusedBodies check, so my earlier approval stands. I read the diff only and ran nothing.

Approved

The head ac9bc19 has no code differences from 7c931d3, which I approved. So the title or body changed, not the pull's content. The pull still does what it says. limits::serve adds Connection: close to any answer given before the request body was read to its end. The spec in spec/LIMITS.md and the changeset agree with the code. The new refusedBodies check names what it compares. I read the diff and ran nothing.

Approved

Approve. The change since my approval at ac9bc19 is the one the title and body describe: after the answer, the server reads and throws away what is left of a body it answered without, instead of closing the connection.

  • Server: Idle now hands the rest of a body back to serve if it is dropped unread. serve spawns drain when the Content-Length leaves at most 4 MiB. Otherwise, including a chunked body, it adds Connection: close.
    • A body that is never dropped before the answer, or one with no exact length, gets Connection: close. That is the safe fallback.
    • drain gives up after 30 s with no data, so a stalled client can't hold it open.
  • Spec and changeset: spec/LIMITS.md and the changeset describe this behaviour, and the check list in the spec has the new case.
  • Harness: refusedBodies is stricter for the server, which must now keep the connection. It still allows the reference either to keep the connection or to say Connection: close. The new CSRF-refusal case names what it compares. The step is added to compare.mjs.

I read the diff and ran nothing.

All checks have passed

3 passed, 1 skipped

Merged as 45ccabe