Repository navigation
Tolerate event-loop starvation in the connect tunnel heartbeat - #2718
Closed
vburojevic wants to merge 1 commit into
Closed
vburojevic wants to merge 1 commit into
vburojevic wants to merge 1 commit into
Conversation
The tunnel client terminated healthy tunnels whenever the bb server event loop could not process heartbeat acks within the 60s deadline, then waited up to 80s for the delayed close event before reconnecting. Grant ack grace when the heartbeat timer itself ran late, defer the kill decision one setImmediate so queued acks drain first, and schedule the reconnect immediately on a genuine heartbeat timeout. Co-Authored-By: Claude Fable 5 <[email protected]>
Collaborator
|
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
bb connect's tunnel client kills healthy tunnels when the bb server process is starved. The relay answers heartbeats at the Cloudflare edge via
setWebSocketAutoResponse(apps/connect/src/tunnel-do.ts), so acks are essentially guaranteed while the link is up — but under host overload (load average 143–234 on 10 CPUs observed) the server event loop cannot process the queued ack messages within the 60sHEARTBEAT_DEADLINE_MS. The heartbeat tick then sees a stalelastAck, logstunnel heartbeat missed; reconnecting, and terminates a live socket. Reconnection then waits for the wscloseevent, which under the same starvation was measured arriving 50–82 seconds later. During that window remote visitors getbb connect: tunnel disconnected mid-requestand the offline interstitial even though bb is running. One affected machine logged 123 such false terminations in a single day (2026-08-29).Node's event-loop phase ordering makes the false positive structural: expired timers run before poll-phase I/O, so on a recovering loop the heartbeat tick always observes
lastAckas stale even when the acks are already sitting in the socket buffer.What changed
packages/tunnel-client/src/session.tssetImmediateso acks already queued in the poll phase are counted first.onHeartbeatTimeoutsession callback; adisposedflag makes a pending deferred decision inert afterdispose().plugins/connect/src/tunnel.ts: on a genuine heartbeat timeout the tunnel now schedules its reconnect immediately via the existing idempotentscheduleReconnectinstead of waiting for the delayedcloseevent (which also disposes the session, stopping the repeatedheartbeat missedwarns). The latercloseremains a guarded no-op.No wire or protocol change:
packages/tunnel-contract,apps/connect, andHOST_DAEMON_PROTOCOL_VERSIONare untouched.How you verified
packages/tunnel-client/test/session-heartbeat.test.ts(fake timers, fake ws): timely acks keep the session alive; a genuine miss terminates and reports exactly once; an ack queued at decision time rescues the tunnel; a starved timer grants grace instead of terminating; a deferred decision is inert after disposal. The four behavioral cases fail against the previous implementation.plugins/connect/src/tunnel-lifecycle.test.ts: heartbeat timeout schedules the retry before anycloseevent, and a laterclosedoes not double-schedule (fails before the change with the state stuckconnected).pnpm exec turbo run typecheck --filter=@bb/tunnel-client --filter=bb-plugin-connect— clean.pnpm exec turbo run test --filter=@bb/tunnel-client --filter=bb-plugin-connect --force— 17/17 and 91/91 pass.🤖 Generated with Claude Code