fix(relay): tolerate slow connections with coordinated deadlines
What & why
A host can be online on its control socket while every relay request fails: a WebSocket handshake taking four seconds is canceled by the existing three-second dial timeout. Allow up to 30 seconds for host connection setup, with the existing two dial attempts sharing that total budget. Explicit failures can still retry immediately; a timed-out attempt does not start another full timeout window. Use a monotonic clock for the shared budget.
Keep the existing partysocket control connection and its persistent reconnect/backoff behavior. Dial-backs remain raw WebSockets because their tickets are single-use; reconnecting an established stream would reuse a consumed ticket. No new retry helper or dependency.
Coordinate the relay dial deadline at 35 seconds and the HTTP exchange deadline at 65 seconds (dial window plus 30 seconds for the response). Terminal and event clients already derive their connection deadlines from the shared relay constant. Successful connections complete immediately; unreachable hosts and stalled HTTP exchanges can now take longer to fail.
Deploy the relay before shipping the host/desktop update. Older clients retain their shorter deadlines until updated. Verified in the local development desktop; not deployed or installed in the production app.
How I tested it
-
Direct relay control-registration before/after (2026-09-18): real local Wrangler Worker, placement KV, HostTunnel Durable Object, TunnelClient and partysocket; local JWT/JWKS and host-access fixtures. A TCP proxy delayed each control handshake by 25 seconds. Baseline host source from cb9803ad1d (20-second control timeout) remained offline (
_whoowns503) for 46.3 seconds across three attempts, then registered after the delay was removed. Current host (30-second timeout) registered on attempt one at 25.3 seconds (_whoowns200). Forcibly dropping its TCP connection changed presence back to 503; automatic reconnect through the same 25-second delay restored 200 at 51.6 seconds total. Explicit shutdown returned presence to 503. Both runs used the same relay build to isolate the host change; no terminal transport was involved. -
Scope correction: the previous CDP test covered dial-back traffic, not slow control registration. The control timeout changes from 20 to 30 seconds; the old 3-second timeout was for dial-backs. The new test reproduces a controlled registration failure, but does not establish the root cause of the original airplane incident.
-
94 relevant tests passed: 13 host/HTTP deadline regressions, 3 real-socket integration tests, 29 workspace-client tests, 42 desktop terminal transport/diagnostic tests, and 7 port-forward mux tests.
-
Real sockets exercised a 25-second upgrade, an HTTP 503 upgrade followed by a successful 4.5-second retry, and a stalled upgrade that reports failure at 30 seconds without forwarding the HTTP request.
-
These tests found and fixed a timer-rounding edge case: a timeout firing fractionally before the monotonic deadline could start a doomed retry. Deadline expiry now reports failure directly.
-
Original CI failed because the unit test spied on global console.warn and counted unrelated suite warnings. The test now spies only on the tunnel's failure method; tunnel tests also passed inside the full host-service suite.
-
Full local host-service suite: 1,872 passed, 8 todo, 134 failed (2014 tests). Failures include Xcode-license/Git environment errors reproduced in isolated worker/worktree tests; a test-process-only Command Line Tools override did not resolve them. Full suite is not green.
-
Host-service and relay typechecks passed after the changes; workspace-client typecheck and targeted repository lint passed. Plugin validation and diff whitespace checks passed.
-
Real Electron CDP before/after in this worktree (renderer 4725, CDP 9348, API 4721), using the authenticated workspace route and a real local Wrangler relay on 4733. Temporary fixtures routed the local host through the relay and delayed dial upgrades identically for baseline and fixed builds. With 4.5-second upgrades, the original host timed out both attempts, returned HTTP 502 after about 6 seconds, and left the terminal reconnecting with Git status unavailable. The fixed host connected successfully; real CDP clicks/keyboard input ran
echo CDP_RELAY_OK_4500and displayed its output. -
CDP recovery also passed after navigating away/back and restarting the relay with 25-second upgrades: terminal reconnected automatically, Git requests returned 200 after about 25 seconds, and a keyboard-entered
echo CDP_RELAY_OK_25000displayed its output. Before/after screenshots captured and inspected; no renderer console errors captured during fixed interactions. All temporary source fixtures removed, test processes stopped, and working tree clean. -
Not verified against a deployed Cloudflare relay or the installed desktop build. Port forwarding retains its separate 15-second HELLO deadline; its ordinary mux tests pass, but this PR does not establish slow-network resilience for that path.
Checklist
- PR title follows conventional commits (
type(scope): subject) -
bun run lintandbun run typecheckpass (targeted checks passed; full monorepo checks left to CI) - "Allow edits from maintainers" is checked on fork PRs (same-repository PR)
Final review
- Direct control-registration before/after evidence is recorded above.
- Reviewed the mobile 15-second
requestDialUrlfinding: this timer bounds the React Native bridge's URL-signing request, beforeopenSocket; it does not bound the host-to-relay upgrade. - Pending dial cleanup on host shutdown and HTTP exchange settlement on dial disconnect are pre-existing lifecycle gaps. The larger deadlines extend their bounded windows; neither blocks this timeout improvement.