Repository navigation
Answer every bb connect request within its deadline and record tunnel drop diagnostics - #5113
Merged
Merged
Conversation
The gate's 30 s response-head timer only started after the Worker's Durable Object handoff, so a request stuck in routing or in that handoff never got an answer. Two app submissions through ymichael.getbb.app on 2026-10-06 hung this way until the browser reloaded (Cloudflare 499), leaving the composer spinning. The Worker's fetch now races the whole request against the same deadline, counted from arrival, and answers 504 naming the stuck stage (routing, tunnel-object, request-body, response-head, finishing). A late response is cancelled so its relay closes. Because Workers Logs keeps about 1% of gate requests, each stall is also written to the bb_connect_gate_stalls Analytics Engine dataset. Co-Authored-By: Claude Opus 5.5 <[email protected]>
…rops The server's connect tunnel dropped about 40 times on 2026-10-06 with fresh heartbeats, and two reconnects hung for their full 10 s handshake even though Cloudflare's edge logged them. The plugin logged too little to tell a network problem from a slow gate or to tie drops to a Cloudflare location. The plugin now times each dial's DNS lookup, TCP connect, TLS handshake and upgrade response, and reads the Cloudflare colo from the upgrade's cf-ray. `tunnel connected` and `handshake timed out` carry those timings, and `tunnel closed` adds the colo, ray, and a traffic snapshot from the session: open streams, bytes each way, how long each direction was quiet, and bytes still buffered. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Two reconnects of a server's tunnel on 2026-10-06 reached Cloudflare's edge and then got no upgrade for their full 10 s handshake, so the slow step is inside the gate. The client gives up and cancels the Worker before a completion log could run. A tunnel dial still unanswered after 3 s now writes a `slow` event with its current stage and how many tunnel-object attempts it has made, from a timer that fires while the dial is still waiting. Stalls and slow dials share one Analytics Engine dataset, renamed from bb_connect_gate_stalls to bb_connect_gate_events before either shipped. Co-Authored-By: Claude Opus 5.5 <[email protected]>
A late 101 has no body, so cancelling the body left its socket registered in the tunnel object after the visitor already got a 504. The gate now accepts and closes a late WebSocket. Co-Authored-By: Claude Opus 5.5 <[email protected]>
ymichael
added a commit
that referenced
this pull request
Oct 7, 2026
…not hang others (#5142) ## Human comments ## What was wrong The gate's label, session, machine credential and desktop grant caches (`apps/connect/src/session.ts`, `desktop-session.ts`) stored the pending D1 lookup promise in isolate-global maps and let later requests await it. In Workers, a lookup belongs to the request that started it. When that request's client cancels mid-lookup (page navigation, an aborted fetch), the lookup never settles. Every later request for the same label or cookie in that isolate then waited on it until the entry expired, 15 s for labels and 20 s for sessions and credentials. Before #5113 those requests hung indefinitely; this is the "stuck submission" behind the composer that kept spinning. Since #5113 they end as `504 timed out checking access (stage: routing)`. Production `bb_connect_gate_events` recorded about 6,500 routing stalls across 179 hosts in the three hours after that deploy, clustered per host in bursts no longer than the cache TTL (for example 56 stalls on one host within 14 s). The same failure mode in another Workers app: https://zenn.dev/coji/articles/cloudflare-workers-better-auth-hanging-promise ## What changed - `apps/connect/src/session.ts`: `SettledLookupCache` replaces `cacheGet`/`cacheStore`. It stores a lookup's value after it settles and never hands a pending promise to another request. Deleting a key bumps a generation, so a lookup that was in flight when its entry was invalidated (session refresh, credential revocation) does not write a stale value back, as before. Failed lookups are still not cached. - The label, session and machine credential caches in `session.ts` and the desktop grant cache in `desktop-session.ts` use it. - This removes the single-flight coalescing added in #2435. A cold burst of requests for one label or cookie now runs one D1 query per request instead of one shared query. D1 has the headroom: over the last 6 h production queries averaged 0.36 ms and total execution time was about 3% of wall time. No wire, CLI or doc changes. ## How you verified Tests in `apps/connect/src/session.test.ts`: - A label, session cookie or machine credential whose earlier lookup never settles still resolves on the next request. All three hang and time out against the previous cache code. - A session lookup in flight when its cookie is invalidated is not cached. It fails when the generation check is removed. - The single-flight burst tests are replaced by sequential cache-hit tests; the "does not cache a failed lookup" tests are unchanged. Commands: `pnpm exec turbo run typecheck test lint --filter=@bb/connect` (220 tests pass). Staging (`bb-connect-staging`): on one HTTP/2 connection, a request is cancelled 60–200 ms after it is sent, while its lookup is in flight, then two normal requests follow. Before this change, every attempt inside that window (4 of 4) left the follow-up requests stuck in routing for 30 s and ending in 504. With this change, 6 of 6 attempts at 60, 80, 100, 120, 150 and 200 ms answered in 0.2–0.5 s (503 offline, since no bb was connected to that staging label, which means routing completed). Not changed: about 12,500 `response-head` stalls in the same window, where the request reached the bb but it did not answer within 30 s (many on `/internal/session/open`). That is a separate issue. > AGENT GENERATED 🤖 Generated with [Claude Code](https://claude.com/claude-code) --------- Co-authored-by: qa <qa@x> 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
The bb connect gate's 30 s response-head timer only started once a request had been handed to the tunnel's Durable Object. A request that stalled earlier, while the gate checked access or handed the request off, was never answered. On 2026-10-06, two app submissions through
ymichael.getbb.apphung this way until the browser reloaded (Cloudflare logged 499), and the composer kept spinning. On the same day the server's tunnel dropped about 40 times while heartbeats were current, and two reconnects used up their full 10 s handshake. The logs couldn't tell a network problem from a slow gate, and Workers Logs keeps only about 1% of gate requests.What changed
apps/connect/src/gate-deadline.ts,worker.ts,relay.ts: the Worker races each request against the same response-head deadline (30 s, 90 s for voice transcription), counted from when the request arrives. A request that misses it gets a 504 naming the stuck stage:routing,tunnel-object,request-body,response-head, orfinishing. If the real response arrives later, its body is cancelled. A late WebSocket upgrade is accepted and closed, so the tunnel object doesn't keep a socket nobody holds. The deadline covers only the response head, so streamed bodies are unaffected./__tunnel) still unanswered after 3 s writes aslowevent with its stage and how many tunnel-object attempts it has made. Stalls and slow dials go to the new Analytics Engine datasetbb_connect_gate_events(bb_connect_gate_events_stagingon staging), bound asGATE_EVENTSinwrangler.jsonc.plugins/connect/src/tunnel.ts: the plugin times each dial's DNS lookup, TCP connect, TLS handshake and upgrade response, and reads the Cloudflare colo and ray fromcf-ray. These fields appear ontunnel connectedandhandshake timed out. To get the timings, a TLScreateConnectionreplacesws's default and keeps its server-name (SNI) handling.packages/tunnel-client/src/session.ts:TunnelSession.snapshot()reports open streams, bytes each way, how long each direction has been quiet, and bytes still buffered.tunnel closednow includes this astraffic, along withcoloandray.docs/debugging-and-qa.mddocuments the new log fields, the 504 stages, the dataset's columns, and an example query.No tunnel protocol or daemon wire change, so
HOST_DAEMON_PROTOCOL_VERSIONis unchanged. No CLI or guide changes.How you verified
Tests added:
apps/connect/src/worker.test.ts: a request stuck at each stage gets the 504 for that stage at the deadline, including stalls before the tunnel object answers; voice transcription gets 90 s; late HTTP bodies are cancelled and late WebSocket upgrades are accepted and closed; a slow tunnel dial writes oneslowevent with its attempt count, and a fast one writes nothing. The late-WebSocket test failed before its fix.packages/tunnel-client/test/traffic-snapshot.test.ts: byte counts, quiet times and buffered bytes in the snapshot.Commands:
pnpm exec turbo run typecheck test lint --filter='...[origin/main]', all passing. On the first run two@bb/serverbuiltin-plugin build tests timed out at a load average of 23;test/services/plugins/builtin-plugins.test.tspasses 28/28 on rerun.Staging (gate deployed to
bb-connect-staging; a source bb from this branch signed in withpnpm dev --staging; traffic driven through the gate with a minted session cookie):/and/api/v1/threadsthrough the server host return 200; a shared port returns 200, and 401 without a cookie.bb connect: timed out waiting for the tunnel client (stage: response-head)at 30.9 s.bb_connect_gate_events_stagingwithin seconds (stall,response-head, host, path, 30000 ms, 1 attempt).{"lookupMs":8,"connectMs":90,"tlsMs":199,"upgradeMs":1558,"colo":"MIA"}.tunnel closedwithcolo,rayandtraffic.Not exercised on staging: a
slowdial event, because no dial took over 3 s inside the Worker, and a late WebSocket upgrade, because the tunnel object answers visitor upgrades with 101 before the origin does. Both are covered by unit tests. Seen during staging testing and not caused by this change: a gate deploy can drop connected tunnels with 1006, and the first reconnect goes through the tunnel object's restart-and-retry path.🤖 Generated with Claude Code