Skip to content

Keep bb connect up under large uploads and wait for slow responses on a healthy tunnel - #6723

Merged
ymichael merged 2 commits into
mainfrom
bb/connect-relay-overload-and-slow-heads
Oct 30, 2026
Merged

ymichael merged 2 commits into
mainfrom
bb/connect-relay-overload-and-slow-heads

Conversation

@ymichael

Copy link
Copy Markdown
Collaborator

Human comments

What was wrong

1. Large uploads overloaded the tunnel object. Both relay paths sent each request-body read as its own body-chunk frame. 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:

  • the tunnel closed with 1013 Service overloaded; retry later;
  • in-flight requests failed together in bursts of 502 bb connect hit a temporary problem;
  • the remote host flapped between connected and disconnected.

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:

  • a closed tunnel already fails its requests at once (abandonStreams);
  • a silent tunnel is replaced after the client misses heartbeats for 60 s.

#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-length or chunked, all succeeded on main.

Report: https://discord.com/channels/1522478522332090528/1565810805096845352

What changed

  • Coalesced body frames. apps/connect/src/request-body-chunks.ts buffers request-body reads into MAX_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 until body-end, so this needs no tunnel protocol change and no client update.
  • Waiting on a healthy tunnel. When the gate deadline passes while a relayed request is in response-head:
    • withGateDeadline asks the tunnel object for /__control/status. That's an in-memory read of the last heartbeat time, not a D1 query.
    • A heartbeat under 45 s old earns another 30 s, up to 10 minutes from arrival.
    • A stale or missing heartbeat, or no status answer within 2 s, still gets the 504.
    • The first extension writes a slow gate event to bb_connect_gate_events.
  • Single place that gives up. The gate now aborts the relay through an AbortSignal when it gives up. The relay's own head timer is gone. Closing the relay still sends close-stream, so the tunnel client cancels the origin request.
  • Voice budget removed. The 90 s voice-transcription special case is gone, because the rule above covers it.
  • Relay off. When WORKER_HELD_RESPONSES is 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. docs/debugging-and-qa.md documents the new deadline rule and the extra slow event.
  • Deploy scope. Worker-only. No change to 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 main and pass here:

  • relay.test.ts and worker.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 --local with the real @bb/tunnel-client TunnelSession. 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 local TunnelSession dialing *.vibecodethis.site. Before = main, or the coalescing commit alone for the deadline rows:

Scenario Before After
Frames for one 15 MiB upload 3,841 × 4 KiB 15 × 1 MiB
8 parallel 15 MiB uploads, 4 rounds round 4: 6/8 502; 6 × Durable Object is overloaded in Workers Logs; ~50 s per round 32/32 200, ~27 s per round, no overload errors
12 parallel 15 MiB uploads, 3 rounds — 36/36 200, no tunnel drops
Origin head at 35 s / 75 s 504 at 30 s 200 at 35 s / 75 s
Origin head at 200 s — 200 at 200 s
Tunnel not heartbeating, head at 75 s — 504 at 30 s
Voice transcription head at 60 / 85 / 100 s 200 / 200 / 504 at 92 s 200 / 200 / 200 at 102 s
Voice, tunnel not heartbeating, head at 50 s 200 at 51 s 504 at 31 s

Workers Logs confirmed each staging request ran on the intended version. Staging was rolled back to e783f7fa and the seeded rows deleted.

Commands. pnpm exec turbo run typecheck test lint --filter='...[origin/main]' passed, as did oxfmt --check and oxlint on apps/connect.

Fixes #4832

🤖 Generated with Claude Code

AGENT GENERATED

ymichael and others added 2 commits October 30, 2026 14:59
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]>
@ymichael
ymichael merged commit a0e5133 into main Oct 30, 2026
51 checks passed
@ymichael
ymichael deleted the bb/connect-relay-overload-and-slow-heads branch October 30, 2026 22:07
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Connect's 30s response-header timeout breaks model calls for large-context threads

1 participant