Repository navigation
fix(web): detect half-open server socket with a liveness heartbeat - #6697
Conversation
The browser WebSocket API hides protocol pings/pongs, so when the server connection goes dark without a FIN/RST (mobile NAT idle timeout, server crash, wifi->cellular handoff) the socket stays readyState===OPEN with no onclose/onerror. The web/mobile client then silently freezes on stale data and never reconnects until a manual reload — the server-side 15s ping reaps the server's view of the client, but that signal is invisible to page JS. Add an app-level liveness watchdog on the connected socket: - every inbound frame (reply, subscription push, keepalive, probe echo) stamps lastInboundFrameAt and clears any outstanding probe; - a visibility-gated 10s interval, after 25s of silence, sends a cheap fire-and-forget status.get probe (id intentionally unmatched, so it adds no pending request/timeout); - ONLY if that sent probe stays unanswered for another 20s does it close the socket to drive the existing scheduleReconnect() path. Closing is gated on an unanswered PROBE, never on raw accumulated silence, and a suspended tick loop (backgrounded/frozen tab: a tick landing >=2 intervals late) re-baselines the liveness clocks. Together these guarantee a tab backgrounded for minutes cannot be mistaken for a dead socket and false-close its live subscriptions on the first tick after returning to the foreground. No server change: the client already calls status.get over this connection, and any inbound frame already proves liveness server-side. The probe is skipped while the tab is hidden (no visible stale data, saves battery). Desktop/main transports are untouched — they already heartbeat via ws ping/pong. Degraded UX: one tiny frame per ~25s only while the tab is visible and the connection has gone quiet; negligible traffic, no behavior change on a healthy connection. Adds a deterministic heartbeat test suite driving the watchdog with injected time/visibility: idle probe, probe-unanswered close + reconnect, the hidden->visible no-false-close regression, hidden-tab skip, non-connected skip, and inbound-frame reset. Provides setInterval/clearInterval in the existing client test's window stub now that 'connected' arms an interval. Co-authored-by: Orca <help@stably.ai>
📝 WalkthroughWalkthrough
🚥 Pre-merge checks | ✅ 3 | ❌ 2❌ Failed checks (2 warnings)
✅ Passed checks (3 passed)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
Actionable comments posted: 1
ℹ️ Review info
⚙️ Run configuration
Configuration used: Repository UI
Review profile: CHILL
Plan: Pro
Run ID: 2170279a-efbe-4d10-93cf-5043b95e9339
📒 Files selected for processing (3)
src/renderer/src/web/web-runtime-client-heartbeat.test.tssrc/renderer/src/web/web-runtime-client.test.tssrc/renderer/src/web/web-runtime-client.ts
| // A reply lands: onmessage stamps lastInboundFrameAt and clears the probe. | ||
| internals.lastInboundFrameAt = internals.now() | ||
| internals.heartbeatProbeSentAt = null |
There was a problem hiding this comment.
🎯 Functional Correctness | 🟡 Minor | ⚡ Quick win
Exercise the real onmessage path here.
These lines manually reset heartbeat state, so the test never verifies the upstream contract in src/renderer/src/web/web-runtime-client.ts:393-405 that inbound frames clear heartbeatProbeSentAt and refresh lastInboundFrameAt. If that wiring regresses, this test still passes. Stub handleSocketMessage and invoke socket.onmessage?.(...) instead.
…tablyai#6697) Co-authored-by: Orca <help@stably.ai>
What
Add an app-level liveness heartbeat to the web/mobile-browser runtime client so it detects a silently half-open WebSocket and reconnects, instead of freezing on stale data.
ELI5
If your phone's connection to the host dies silently (cellular handoff, NAT idle timeout, server crash with no clean close), the browser still thinks the socket is open and just sits there showing stale data forever — it never reconnects because nothing told it the line went dead. This makes the client notice the silence and reconnect.
The problem
The browser
WebSocketAPI handles protocol pings/pongs transparently — they never surface to page JS. So on a half-open connection the socket staysreadyState === OPENwith noonclose/onerror, and the only reconnect triggers (onclose/onerror) never fire. The server's 15s ping reaps the server's view of the client, but that signal is invisible to the browser. Result: silent UI freeze until a manual reload.(Desktop/main-process transports are unaffected — they already heartbeat via real
wsping/pong.)The fix
A liveness watchdog on the connected socket:
lastInboundFrameAtand clears any outstanding probe;status.getprobe (id intentionally unmatched → adds no pending request/timeout);ws.close()to drive the existingscheduleReconnect()path.Closing is gated on an unanswered probe, never on raw accumulated silence, and a suspended tick loop (backgrounded/frozen tab — a tick landing ≥2 intervals late) re-baselines the liveness clocks. Together these guarantee a tab backgrounded for minutes can't be mistaken for a dead socket and false-close its live subscriptions on resume.
No server change: the client already calls
status.getover this connection, and any inbound frame already proves liveness server-side.Measurement
Pair a browser client, then silently drop the host's packets (a firewall rule that DROPs rather than RSTs the port). Before: the client never reconnects until a manual reload. After: it detects the gap and reconnects in ~45–55s.
Degraded UX
One tiny frame per ~25s only while the tab is visible and the connection has gone quiet — negligible traffic, skipped entirely while hidden (battery). No behavior change on a healthy connection. Trade-off: worst-case dead-socket detection is ~45–55s (vs. an unbounded freeze today); this latency is the deliberate cost of guaranteeing a real round-trip attempt before declaring the socket dead (no false positives).
Tests
web-runtime-client-heartbeat.test.tsdrives the watchdog with injected time/visibility: idle probe, probe-unanswered close + reconnect, the hidden→visible no-false-close regression, hidden-tab skip, non-connected skip, inbound-frame reset. All 78 web tests pass.Verification
Independent adversarial review across two rounds. Round 1 caught a real false-close on the hidden→visible transition (raw-silence close); this PR fixes it by gating the close on an unanswered probe + suspended-tick re-baseline. Round 2 verdict: SAFE — false-close now structurally impossible, dead sockets still detected within ~45–55s, no masking of real deaths, no new leak/double-arm/probe-id collision.
Made with Orca 🐋