Repository navigation
fix(planning): drain a refused request body and serialize tunnel syncs - #17626
Conversation
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
Codex Review Summary: ✅ CompletedThis comment shows the latest Codex review activity on this pull request.
ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings. |
|
claude-security-review reviewed Security review: PR #17626, incremental (03e3cf0 → 47cda29)I found no security issues. I read every hunk of both files in scope, What I checked
critical-findings: 0 |
|
claude-review reviewed Code review (incremental, blocking only):
|
…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
… 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
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.pymadetest_server.pyflake 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]inTestUploads.test_sizes_are_checked_before_any_body_is_read. Both tracebacks raise inside the client'ssock.sendallattest_server.py:2858, the/api/upload/offerPOST with a body overOFFER_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_POSTanswers 413 whenContent-Length > capwithout reading the body (server.py, thelength > capbranch).send()setsclose_connectionfor every code >= 400.socketserver.TCPServer.shutdown_requestthen runsshutdown(SHUT_WR)followed byclose()at once, with the body still unread.sendallgets that reset.GuestServer.refusealready did this for the 503 path, and its docstring describes the same reset. This PR moves that code into onelinger(sock, seconds, limit)helper.GuestHandlernow counts the declared body bytes it has not read (parse_requestsets the count,read_bodylowers it).finish()callslingerwith that count andDRAIN_SECONDS(2 s) as its bounds, so a client that claims a hugeContent-Lengthcosts at most 2 s.2.
KeyError: 'error'inTestTunnelSwitch.test_the_old_host_is_refused_once_its_grace_time_is_up. TheKeyErrormeans/api/stateanswered 200 for the old quick-tunnel host. The only way that happens ismoved(), soretired_hostwas still set when the request ran.App.serve()always starts thewatchthread, whether or notwatch_tunnelis set. That thread runssync_tunnel()everyTUNNEL_POLL, andhost_okrunssync_tunnel()from request threads.sync_tunnelreads the record (tunnel.retired(self.dir)) and then assignsself.retired_host, with no lock. A watch sync that read the record before the test setuntil = 0could therefore assign the old host after the test's own sync had cleared it, and the address was allowed again.threading.Locknow runs syncs one at a time. Before adding it, I checked that the restart path (share.leaseandconfirm,tunnel.ensure,fall_back) sends no request to this server that goes throughhost_ok: its only loopback request is/api/tunnel-probe/, whichdo_GETanswers beforerefused(). So the lock cannot deadlock on that path.Verification
Before the fix, 3 of the 13 completed macOS jobs on 2026-11-20 failed:
test_server.py:2858test_server.py:2858KeyError: 'error'attest_server.py:434Regression 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_itfailed withBrokenPipeErroron 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_afterfailed with(200, None) != (403, 'bad-host'), the same 200 that caused theKeyErrorin CI.After the fix, all runs local on Linux:
test_server.pyin 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.TestTunnelSwitch,TestUploads,TestBusy,TestRawRequestsand the new tests (35 tests per run) passed all 5 runs.scripts/run-ruff.sh checkpasses.The macOS behavior is proven only by this PR's own
Test Macos Planning Sharedrun.Related
GuestServer.refuseandTestBusy.test_the_503_is_no_reset_while_the_client_still_sends: the earlier fix for the same reset on the 503 path, whichlinger()now shares.🤖 Generated with Claude Code
https://claude.ai/code/session_01KeBVgdbwavfuWZGmKnMhWX