Websocket edge#350
Closed
breardon2011 wants to merge 6 commits into
Closed
Conversation
added 6 commits
June 1, 2026 10:10
…ial backoff, flap circuit breaker, drop redundant scrollback buffer
…arker when handler exits without a real process exit; ws-edge integration tests 05-08
# Conflicts: # cloudflare-workers/api-edge/wrangler.toml
Contributor
Author
|
going to make separate PR for the Cell gateway |
Merged
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.
Summary
Adds a per-sandbox Durable Object (
SandboxWsGateway) that brokers all SDKand dashboard WebSocket traffic — exec, PTY, and agent — between the client
and the sandbox's owning cell. The whole DO path is gated on
WS_VIA_DO=1;when unset, the legacy transparent CF-Tunnel forward stays in place, so the
rollback is a one-line var change.
What the DO does
Before: each WS upgrade was a transparent fetch passthrough. Any disruption
along the chain (worker restart, cell-CP blip, live migration, idle drop)
killed the user's terminal with an abrupt transport close. No state, no
recovery.
After: the DO terminates both ends with a
WebSocketPair, bridges frames,and persists across one side dying. Layered:
cleanly when D1 says the sandbox is stopped/failed or the cell returns
404/410.
0x03+exitCodemarkertriggers a clean
1000 / "exec completed"close instead of aninfinite-redial loop) + empty-frame keepalive on each alarm tick to defend
the DO↔cell hop against the CF Workers fetch-WS idle drop (~100s).
fetch()creates aSessionand thegateway holds
Set<Session>, so two clients on the same sandbox don'tclobber each other. Migration-aware backoff (2s × 30 ≈ 60s on
503 /migrating/). Per-session flap circuit breaker (>3 redial cycles in60s closes with
1011 "upstream flapping"). Dropped the v4 output ringbuffer — cell-side scrollback (1 MB ring, resent on every reattach) is a
strict superset and was duplicating output.
Worker-side: sessions survive live migration
Sessions stay alive end-to-end across a worker migration. The in-VM agent
owns the PTY/exec session map; what dies is the host-side bookkeeping on
the source worker. The fix:
SetMigrationOutgoingCallbackfiresPTYManager.ReleaseForSandboxandExecSessionManager.ReleaseForSandboxafter
LiveMigratecompletes. PTY closes its gRPC stream; exec cancelsits stream context via a new
Cancelfield on the handle. Without this,the source worker's WS handler stayed blocked on a dead vsock and the DO
never saw an upstream close.
session map misses,
handlers.gofalls back toRebindFromAgent(sandboxID, sessionID). The manager opens a freshagent.PTYAttach/agent.ExecSessionAttachagainst the existingsession_id; the agent's first message is the existence check (returns
"not found" if the in-VM session is genuinely gone). On success a fresh
local handle is registered and the WS upgrade returns 101.
0x03exit-code marker whenExitCode != nil(a real process exit). Astream cancel during migration release no longer looks like
"exec completed" to the edge DO.
No agent changes, no proto changes, no edge DO logic changes — pure
worker plumbing.
User experience: typing through a migration window pauses for ~500ms–2s
(the DO redial), then resumes on the destination worker. No reconnect, no
new session_id, no lost in-VM process.
Tested
End-to-end against dev (
scripts/integration-tests/05-08*.py, exit 0/1):ws-multi-client.pyws-exec-exit-clean.py1000 'exec completed'(pre-v6ws-pty-survives-migration.pyPOST /migrate; file written pre-mig is readable post-mig; client never closesws-exec-survives-migration.pyexec completed, not a fake one fromPlus the manual scenarios from earlier rounds: 75 s idle PTY survives the
keepalive, sandbox DELETE closes with
1000 'sandbox stopped', workerprocess restart triggers the redial path, etc.
Files
Rollback / cutover
WS_VIA_DOto anything other than"1"(or unset) andredeploy.
proxyToCellSDKandproxyWebSocketfall back to thetransparent forward immediately. No DO instances are torn down — they
just stop being routed to and the alarm eventually releases their
storage.
WS_VIA_DO=0on the edge, the destination worker still rebindscorrectly if a stale session_id happens to land on it after worker
restart. Strict net-positive over current behavior.
wrangler.prod.tomlships the binding + migration but leavesWS_VIA_DOcommented out, so promoting this PR is infrastructure-only.Flip the flag in a follow-up after dev burn-in.