Skip to content

fix(planning): drain a refused request body and serialize tunnel syncs - #17626

Merged
kyle-sexton merged 3 commits into
mainfrom
fix/planning-server-flake
Nov 20, 2026
Merged

kyle-sexton merged 3 commits into
mainfrom
fix/planning-server-flake

Conversation

@kyle-sexton

@kyle-sexton kyle-sexton commented Nov 20, 2026 •

Copy link
Copy Markdown
Member

No related issue: CI flake on Test Macos Planning Shared (3 of 13 jobs failed 2026-11-20)

Summary

Two server bugs in plugins/planning/shared/server.py made test_server.py flake on the macOS job, on PRs that do not touch the server. Each one is fixed at its cause in the server, and each has a regression test that fails on the old code on Linux.

Fix

1. ConnectionResetError: [Errno 54] in TestUploads.test_sizes_are_checked_before_any_body_is_read. Both tracebacks raise inside the client's sock.sendall at test_server.py:2858, the /api/upload/offer POST with a body over OFFER_MAX. The chunk requests earlier in that test send headers only and had already passed. The reset came while the client was still sending its body.

  • do_POST answers 413 when Content-Length > cap without reading the body (server.py, the length > cap branch). send() sets close_connection for every code >= 400. socketserver.TCPServer.shutdown_request then runs shutdown(SHUT_WR) followed by close() at once, with the body still unread.
  • RFC 9293 §3.6.1 (SHLD-3) applies: a host that closes "while received data is still pending in the TCP connection, or if new data is received after CLOSE is called ... SHOULD send a RST". The client's sendall gets that reset.
  • The remedy follows RFC 9112 §9.6: the server half-closes, "continues to read from the connection until it receives a corresponding close by the client", then closes.
  • GuestServer.refuse already did this for the 503 path, and its docstring describes the same reset. This PR moves that code into one linger(sock, seconds, limit) helper. GuestHandler now counts the declared body bytes it has not read (parse_request sets the count, read_body lowers it). finish() calls linger with that count and DRAIN_SECONDS (2 s) as its bounds, so a client that claims a huge Content-Length costs at most 2 s.
  • The reset is standard TCP behavior on every OS. Why macOS hits it more often than Linux is not established: UNVERIFIED.

2. KeyError: 'error' in TestTunnelSwitch.test_the_old_host_is_refused_once_its_grace_time_is_up. The KeyError means /api/state answered 200 for the old quick-tunnel host. The only way that happens is moved(), so retired_host was still set when the request ran.

  • App.serve() always starts the watch thread, whether or not watch_tunnel is set. That thread runs sync_tunnel() every TUNNEL_POLL, and host_ok runs sync_tunnel() from request threads.
  • sync_tunnel reads the record (tunnel.retired(self.dir)) and then assigns self.retired_host, with no lock. A watch sync that read the record before the test set until = 0 could therefore assign the old host after the test's own sync had cleared it, and the address was allowed again.
  • This race exists in production as well. A threading.Lock now runs syncs one at a time. Before adding it, I checked that the restart path (share.lease and confirm, tunnel.ensure, fall_back) sends no request to this server that goes through host_ok: its only loopback request is /api/tunnel-probe/, which do_GET answers before refused(). So the lock cannot deadlock on that path.

Verification

Before the fix, 3 of the 13 completed macOS jobs on 2026-11-20 failed:

Regression tests, both run red on the old code on Linux first:

  • TestRawRequests.test_a_body_refused_unread_is_no_reset_while_the_client_still_sends_it failed with BrokenPipeError on the client's send after the 413.
  • TestTunnelSwitch.test_a_sync_that_read_the_record_before_the_grace_ended_does_not_allow_the_old_host_after failed with (200, None) != (403, 'bad-host'), the same 200 that caused the KeyError in CI.

After the fix, all runs local on Linux:

  • test_server.py in full: 287 tests OK in 148.9 s. The baseline before the change was 285 tests OK in 141.2 s; one of the two new tests waits 0.5 s.
  • A 5-run loop over TestTunnelSwitch, TestUploads, TestBusy, TestRawRequests and the new tests (35 tests per run) passed all 5 runs.
  • scripts/run-ruff.sh check passes.

The macOS behavior is proven only by this PR's own Test Macos Planning Shared run.

Related

  • GuestServer.refuse and TestBusy.test_the_503_is_no_reset_while_the_client_still_sends: the earlier fix for the same reset on the 503 path, which linger() now shares.

🤖 Generated with Claude Code

https://claude.ai/code/session_01KeBVgdbwavfuWZGmKnMhWX

The shared session server answered a request it refused before reading
its body (a body over its cap) and then closed the socket with that body
unread. A socket closed with received data pending sends a reset
(RFC 9293 section 3.6.1, SHLD-3), so a client still sending its body
got ECONNRESET instead of the 413. The server now half-closes after the
answer and reads the declared body until the client closes, the body
is read or DRAIN_SECONDS pass (RFC 9112 section 9.6), through one
linger() helper the 503 path shares.

sync_tunnel ran on the tunnel watch thread and on request threads at
once, so a sync that read the tunnel record before a switch's grace
time ended could set retired_host after a later sync had cleared it,
and the old address was allowed again. One lock now serializes syncs.

Both show as macOS CI flakes in test_server.py; each has a regression
test that fails on the old code on Linux.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01KeBVgdbwavfuWZGmKnMhWX
@kyle-sexton
kyle-sexton marked this pull request as ready for review November 20, 2026 08:36
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 20, 2026 •

Copy link
Copy Markdown
Codex Review Summary: ✅ Completed

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-20T08:38:59.553250Z 4394430 Draft marked ready
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@github-actions

github-actions Bot commented Nov 20, 2026 •

Copy link
Copy Markdown
Contributor

claude-security-review reviewed 47cda29c2c073f1196fdc22b1745c52c8f5be94b: read 2 of 2 changed file(s); 0 finding(s), 0 as inline comments.

Security review: PR #17626, incremental (03e3cf0 → 47cda29)

I found no security issues. I read every hunk of both files in scope, plugins/planning/shared/server.py and plugins/planning/shared/tests/test_server.py. Neither file is on a trusted path. The change does not touch a trust control, a plugin manifest or a standing instruction. This lane already reviewed an earlier head, so this pass reports blocking findings only.

What I checked

  • The new drain_read fallback is still bounded. finish() now passes self.drain_read to linger() (server.py#L900).
    • drain_read tries rfile.read1 first. It drops to self.connection.recv(n) only on a non-timeout OSError, which is what the reader raises once a body timeout has marked it timed out.
    • n is still min(limit, 65536), and linger() still stops the loop at DRAIN_SECONDS, which is 2.0 seconds (server.py#L809-L814). It also stops when the client closes or after body_left bytes, the declared body length the server has not read.
    • The fallback therefore does not let a client hold a handler slot longer than before.
  • Errors still end the drain.
    • A TimeoutError is re-raised, and linger()'s contextlib.suppress(OSError, ValueError) ends the drain.
    • If the socket was reset or closed, the fallback recv raises OSError again. That error is suppressed the same way, and super().finish() still runs.
  • No buffered bytes are skipped. BufferedReader.read1 returns bytes it already holds before it reads from the socket, so the fallback is reached only once that buffer is empty. Nothing is read twice or out of order.
  • The new test only talks to the local server. It uses 127.0.0.1 with a patched BODY_SECONDS, so it has no effect outside the machine.

critical-findings: 0

@github-actions github-actions Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

claude-review: 1 inline finding(s) on 439443089a82efcf0959c7214279835110b0641c; the summary is in this lane's pull request comment.

claude-lane: claude-review

Comment thread plugins/planning/shared/server.py Resolved
@github-actions

github-actions Bot commented Nov 20, 2026 •

Copy link
Copy Markdown
Contributor

claude-review reviewed 47cda29c2c073f1196fdc22b1745c52c8f5be94b: read 2 of 2 changed file(s); 0 finding(s), 0 as inline comments.

Code review (incremental, blocking only): 03e3cf09 → 47cda29c

This lane already reviewed an earlier head, so this review covers only blocking findings. I read both files in scope from start to end: plugins/planning/shared/server.py and plugins/planning/shared/tests/test_server.py.

No blocking findings. The earlier 408 drain finding is fixed. GuestHandler.drain_read (server.py:903-910) reads through rfile.read1 first, so bytes the header read already buffered are still counted. It re-raises TimeoutError, which linger suppresses, so the drain still ends at its deadline. On any other OSError, including the cannot read from timed out object error SocketIO raises after a body timeout, it reads from self.connection.recv instead. That fallback loses no data: BufferedReader.read1 only calls the raw reader when its buffer is empty, so the buffer is empty whenever that error comes up. If the socket was reset, the recv fallback raises again and linger suppresses it, so the loop stops. The repository targets Python 3.11 and later, where socket.timeout is TimeoutError, so the except TimeoutError clause catches the drain's own timeout.

The new test covers the path. test_a_body_that_times_out_is_drained_while_the_client_still_sends_it sets BODY_SECONDS to 1.0, below the handler's 30 s timeout. The socket timeout therefore fires inside rfile.read1, which puts rfile in its timed-out state. The test reads the 408 to EOF, which the server's SHUT_WR produces, then sends the other 93 of the 100 declared bytes inside DRAIN_SECONDS. A reset on the first send would make the second send fail. The author reports it fails with BrokenPipeError on 03e3cf0. I have not checked that.

Verification. The author reports 289 tests passing and ruff clean. I have not checked either, and I ran nothing. Everything above comes from reading the code and from how CPython's BufferedReader and SocketIO behave.

Settled threads. Both settled threads are marked lines changed since resolution: no, and I raised neither again.

Closing issue. The PR closes no issue, so there are no acceptance criteria to check.

Loosened checks. The prompt had no loosened_check_verdict: or loosened_check_patterns: line, so I used fail with no consumer entries. It also had no loosened_check_decision_record: line, so I did not check the citation requirement. The incremental diff loosens no check. Loosened checks would be marked: fail (default); patterns: 0.

No embedded directives. The PR context, settled threads and prior findings contain no instructions aimed at this lane.

Verifier dropped 0 of 0 candidates.

…ader

The header read can buffer a small body sent in the same write, and
finish() drained the socket only after closing rfile, so it waited for
bytes the kernel no longer held and kept a handler slot for
DRAIN_SECONDS while the client held the socket open. linger() now takes
a read callable; the handler drains through rfile.read1, which returns
the buffered bytes first, before rfile closes.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01KeBVgdbwavfuWZGmKnMhWX

@github-actions github-actions Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

claude-review: 1 inline finding(s) on 03e3cf0954160f209869b5bd6b9e8233c5e42265; the summary is in this lane's pull request comment.

claude-lane: claude-review

Comment thread plugins/planning/shared/server.py Resolved
… reader

After a body timeout the handler's buffered reader refuses every read,
so the drain read nothing and the socket could still reset while the
client sent the rest of its body. drain_read() falls back to the socket
then; the reader's buffer is empty at that point.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01KeBVgdbwavfuWZGmKnMhWX
@kyle-sexton
kyle-sexton added this pull request to the merge queue Nov 20, 2026
Merged via the queue into main with commit af30892 Nov 20, 2026
54 checks passed
@kyle-sexton
kyle-sexton deleted the fix/planning-server-flake branch November 20, 2026 09:18
@github-actions github-actions Bot added the merge-path: owner-agent Post-merge accounting record (README.md, Merge paths) label Nov 20, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

merge-path: owner-agent Post-merge accounting record (README.md, Merge paths)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant