Repository navigation
Keep bb connect up under large uploads and wait for slow responses on a healthy tunnel - #6723
Merged
Merged
Conversation
The connect worker and TunnelDO sent each request-body read as its own body-chunk frame. Workers hand request bodies over in 4 KiB reads, so a 15 MiB model request crossed the tunnel object as ~3,800 websocket messages in and ~3,800 out. Remote hosts that route Claude Code through the Account Pooler send many such requests at once, and the one TunnelDO per server fell behind: production logged "Durable Object is overloaded. Requests queued for too long." for every request to that server, including tunnel redials, and the tunnel closed with 1013. Both pumps now buffer reads into MAX_CHUNK_BYTES frames. The tunnel client already buffers the whole body until body-end, so nothing downstream changes and no protocol bump is needed. Co-Authored-By: Claude Opus 5.5 <[email protected]>
The gate gave every relayed request 30 seconds to receive response headers from the bb server, then answered 504 "timed out waiting for the tunnel client" even when the tunnel was healthy and the bb server was still working. Large model requests through the Account Pooler, image generation and long voice transcriptions all take longer than that to start answering. Dead tunnels are already caught elsewhere: a closed tunnel fails its requests at once, and a silent one is replaced after the client misses heartbeats. So when the deadline passes while a relayed request waits for its head, the gate now asks the tunnel object for its last heartbeat. A heartbeat under 45 seconds old earns another 30 seconds, up to 10 minutes per request; a stale, missing or unanswered status still ends in a 504. The gate aborts the relay when it gives up, so the relay no longer keeps its own timer, and the voice transcription budget it carried is no longer needed. Requests sent without the worker-held relay keep the fixed 30 second deadline, because the gate cannot tell a slow origin from a stuck tunnel object on that path. Co-Authored-By: Claude Opus 5.5 <[email protected]>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Human comments
What was wrong
1. Large uploads overloaded the tunnel object. Both relay paths sent each request-body read as its own
body-chunkframe. Workers deliver request bodies in 4 KiB reads, so a 15 MiB body crossed the server's single TunnelDO as 3,841 websocket messages in and 3,841 out.On Oct 30 a user ran 9 Claude Code threads on a remote host, reached through the Account Pooler. Eight of them sent screenshot-heavy requests of up to ~24 MB, peaking around 500 MB per minute. Their TunnelDO stayed busy almost the whole time but used only 4–8 s of CPU per minute, while sending ~50k websocket messages per minute against ~350 normally. Production Workers Logs recorded
Durable Object is overloaded. Requests queued for too long.for every kind of request to that server, including tunnel redials (/__tunnel) and the realtime socket (/ws). As a result:1013 Service overloaded; retry later;502 bb connect hit a temporary problem;2. Slow responses hit a fixed 30 s deadline. The gate answered
504 timed out waiting for the tunnel client (stage: response-head)whenever a relayed request had no response head 30 s after it arrived, even with a healthy tunnel and a bb server still working. Large model requests through the Account Pooler and image generation (#4832) routinely take longer. The 30 s isn't needed to catch dead tunnels:abandonStreams);#4832 also blames upload time counting against the 30 s. On real Cloudflare it doesn't: the edge buffers the whole request body before invoking the Worker. On staging, uploads of 4 and 24 MiB spread over 20–45 s, with
content-lengthor chunked, all succeeded onmain.Report: https://discord.com/channels/1522478522332090528/1565810805096845352
What changed
apps/connect/src/request-body-chunks.tsbuffers request-body reads intoMAX_CHUNK_BYTES(1 MiB) frames. Both pumps use it: the worker relay (relay.ts) and the TunnelDO proxy (tunnel-do.ts). The tunnel client already buffers the whole body untilbody-end, so this needs no tunnel protocol change and no client update.response-head:withGateDeadlineasks the tunnel object for/__control/status. That's an in-memory read of the last heartbeat time, not a D1 query.slowgate event tobb_connect_gate_events.AbortSignalwhen it gives up. The relay's own head timer is gone. Closing the relay still sendsclose-stream, so the tunnel client cancels the origin request.WORKER_HELD_RESPONSESis off, the gate can't tell a slow origin from a stuck tunnel object. That path keeps a fixed 30 s, including voice, which drops from 90 s. Production has the relay on.docs/debugging-and-qa.mddocuments the new deadline rule and the extraslowevent.HOST_DAEMON_PROTOCOL_VERSION, the tunnel protocol, or the bb app.connect-next (
apps/connect-next) has the same 30 s problems, plus upload time does count there because Fly streams uploads. It's tracked separately.How you verified
Unit tests. All of these fail on
mainand pass here:relay.test.tsandworker.test.ts: 513 × 4 KiB reads become frames of 1 MiB, 1 MiB and 4 KiB, with the bytes intact.worker.test.ts, new cases: a fresh heartbeat keeps waiting and relays a head at 70 s; a stale heartbeat gives a 504 and closes the relay; an unanswered status gives a 504; the 10-minute cap gives a 504.relay.test.ts: the gate's abort gives a 504 and closes the relay.Local end to end. The real worker and TunnelDO ran under
wrangler dev --localwith the real@bb/tunnel-clientTunnelSession. A 15 MiB upload took 3,841 → 15 frames; 8 parallel uploads took 30,728 → 120. Every body arrived byte-for-byte.Staging (
bb-connect-staging). A throwaway server and machine were seeded in staging D1, with a localTunnelSessiondialing*.vibecodethis.site. Before =main, or the coalescing commit alone for the deadline rows:502; 6 ×Durable Object is overloadedin Workers Logs; ~50 s per round200, ~27 s per round, no overload errors200, no tunnel drops504at 30 s200at 35 s / 75 s200at 200 s504at 30 s200/200/504at 92 s200/200/200at 102 s200at 51 s504at 31 sWorkers Logs confirmed each staging request ran on the intended version. Staging was rolled back to
e783f7faand the seeded rows deleted.Commands.
pnpm exec turbo run typecheck test lint --filter='...[origin/main]'passed, as didoxfmt --checkandoxlintonapps/connect.Fixes #4832
🤖 Generated with Claude Code