Skip to content

Restart a tunnel Durable Object whose tunnel socket vanished - #4268

Merged
SawyerHood merged 1 commit into
mainfrom
fix/connect-tunnel-self-restart
Sep 24, 2026
Merged

SawyerHood merged 1 commit into
mainfrom
fix/connect-tunnel-self-restart

Conversation

@SawyerHood

Copy link
Copy Markdown
Collaborator

Human comments

What was wrong

When the gate's tunnel Durable Objects restart, hibernated tunnel WebSockets can be orphaned. The bb keeps an open socket that nothing answers, so its heartbeats go unanswered. The object has no tunnel socket, so visitors get 503 offline. After 60 s the bb redials, and while the object keeps getting requests, every new dial is orphaned the same way. An object heals only after about 45 s with no requests, which busy servers never get. #4251 reproduced this on staging: any deployment of bb-connect, even an unchanged version, triggers it (runs X0–X7 there).

This caused the 2026-09-24 01:07 outage, and it's happening again in prod without any gate deploy. Prod bb-connect has stayed on 6f79c289 since the 01:43 rollback. Since about 07:00 UTC, Durable Object outcomes show recurring bursts at 08:45–10:30, 12:30, 13:30, 14:30 and 16:15, each with responseStreamDisconnected up 5–10× and scriptThrewException up 10–20×. Each burst leaves more servers stuck:

  • the 503 share on *.getbb.app climbed from 9–10% overnight to 16–30% per hour;
  • 112 hosts had 100 or more extra 503s from 14:30 to 16:30;
  • bee's log shows tunnel heartbeat missed every minute again.

The current gate has no way to recover.

What changed

This is the TunnelDO self-restart from #4251, split out so it can ship ahead of the account stack. It contains no ticket code and no workflow changes.

  • apps/connect/src/tunnel-do.ts:
    • It records tunnelOpenedAt when it accepts a tunnel and tunnelClosedAt when the last tunnel socket closes or the owner revokes it.
    • When a visitor request, a tunnel dial, or the 50 s presence alarm finds no live tunnel socket although the last one never closed (after a 5 s grace), it writes the close, calls storage.sync(), then ctx.abort(). The restarted object drops the orphaned socket and the bb redials into a working object.
    • sync() is required. Without it, abort() discards the write and the object restarts forever (586 aborts in staging run B1 of Accept tunnel tickets behind a flag, and document deploying the gate by hand #4251).
    • Objects written by older code only have serverId/machineId; they restart once the same way, so the first deploy of this code heals existing stuck objects.
    • The alarm path covers servers nobody is visiting.
  • apps/connect/src/worker.test.ts: a TunnelDO restarts after its tunnel socket vanishes suite. It restarts on a visitor request, a dial and the alarm. It doesn't restart after a recorded close, within the grace period, or with a live socket. Legacy objects restart exactly once, and the close record survives the restart.

How you verified

  • On origin/main's tunnel-do.ts the new suite fails 5 of 9; with this change 9 of 9 pass.
  • pnpm exec turbo run typecheck lint test --filter=@bb/connect: pass (130 tests).
  • Staging evidence from Accept tunnel tickets behind a flag, and document deploying the gate by hand #4251, using the same tunnel-do.ts logic:
    • B2: the first deploy of this code over the old gate brought every tunnel back within about 40 s, and DO internalError went 205 → 0.
    • B3 and X10b: later deploys brought each tunnel back within 5–10 s.
    • Upgrade step B: a pre-stack bb held one tunnel for 30 minutes with 0 heartbeat misses, and visitors got 347/347 200s.

Rollout

Deploy Connect is enabled, so merging deploys this with wrangler deploy at 100%. That's acceptable: the deploy orphans the tunnels, and this code restarts them. On staging (#4251 B2), all tunnels were back within about 40 s. The gentler path, recommended in #4251, is:

  1. Disable Deploy Connect.
  2. Merge.
  3. pnpm --filter @bb/connect exec wrangler versions upload.
  4. pnpm --filter @bb/connect exec wrangler rollback <new-version-id> -m "gate: tunnel self-restart". A rollback to a never-deployed version closed tunnels cleanly on staging (Accept tunnel tickets behind a flag, and document deploying the gate by hand #4251 X11).
  5. Re-enable Deploy Connect.

Either way, go straight to 100%: objects still on old code can't restart.

For 30 minutes afterwards, watch:

  • the 503 share and 503s per host on *.getbb.app;
  • Durable Object internalError (one spike as stuck objects restart, then 0) and clientDisconnected;
  • grep 'plugin:connect\] tunnel' ~/.bb/logs/server-stdio.log on bee.

Rollback

pnpm --filter @bb/connect exec wrangler rollback 6f79c289-9e43-4697-b056-78e31bc3e6ad -m "<reason>". Use wrangler rollback, never versions deploy <old>@100%: rollbacks closed tunnels cleanly every time we saw one, and a deployment orphans them again.

AGENT GENERATED

🤖 Generated with Claude Code

A restart of the gate's Durable Objects (any bb-connect deployment, and
now recurring platform-side events) can orphan hibernated tunnel sockets:
the bb keeps an open socket nobody answers, visitors get 503, and a busy
object never heals. TunnelDO now records when it accepts a tunnel and
when the last tunnel socket closes. A visitor request, a tunnel dial, or
the presence alarm that finds no live socket although the last one never
closed durably records a close (storage.sync before abort, or the record
is lost and the object loops) and calls ctx.abort(), so the bb redials
into a fresh object. Objects from older code that still list a server or
machine restart once the same way.

Split out of #4251 so it can ship ahead of the account stack.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
@SawyerHood
SawyerHood merged commit 4277998 into main Sep 24, 2026
18 checks passed
@SawyerHood
SawyerHood deleted the fix/connect-tunnel-self-restart branch September 24, 2026 16:51
SawyerHood added a commit that referenced this pull request Sep 24, 2026
…with a canary

The gate half of #4227, plus the rollout tooling the 2026-09-24 outage
called for. The tunnel self-restart that fixed the outage itself landed
separately in #4268.

- The gate accepts a `bbtkt_` tunnel ticket in place of the raw
  credential when TUNNEL_TICKETS is "on". Production ships "off"; a
  refused ticket gets 401, which bb answers by dialing with its raw
  credential. Staging and local cloud dev run "on".
- Roll out Connect gate (workflow_dispatch) connects a canary, deploys
  an uploaded version at 0%, 5%, 25% or 100% with `wrangler versions
  deploy`, and requires the canary's tunnel to be back within 120 s and
  then hold. It rolls back with `wrangler rollback` if the canary fails,
  and reads each environment's canary settings without falling back to
  production's. It refuses to start while another split is deployed.
- @bb/connect-rollout holds the canary and the step planner. The canary
  holds a tunnel as a dedicated server, answers its own hostname
  through that tunnel, and with --recover-within redials after a drop
  and fails unless it is back in time.
- Deploy Connect's summary points at the workflow.
- docs/connect-gate-rollout.md covers why every deployment disconnects
  tunnels, how Cloudflare assigns versions for a Worker with Durable
  Objects (entrypoint per request, each object pinned per deployment),
  the steps, what to watch, and rollback (`wrangler rollback`, never
  `versions deploy <old>@100%`).

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
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.

1 participant