Repository navigation
fix(runtime): require consecutive missed probes before reaping a paired socket (STA-3320) - #12790
Conversation
…ed socket The paired-runtime WS heartbeat terminated a client after a single unanswered 15s ping. One missed pong is UNKNOWN, not proof the peer is gone: a cellular or Tailscale blackhole, or a stalled TCP retransmit, routinely swallows one pong from a peer that is still there. Users on flaky paths saw constant drops, each costing a full redial plus E2EE re-handshake and subscription replay. Reap now needs MISSED_PROBE_LIMIT (3) consecutive unanswered probes, counted per socket rather than timed. Any proof of life -- pong or any inbound frame -- clears the count, as does a resume from a server-loop pause, since a gap the client was never given a chance to answer must not top up its budget. Missed sweeps still re-probe, so a recovered path proves itself on the next tick. Three matches the liveness budgets already in the product: the web client gives 45s (25s idle + 20s probe grace) and the relay control gives 75s. The paired transport's single miss was the outlier. Also gives the web client's redial the one-sided jitter the shared-control path already had, so a fleet dropped by one shared blip does not re-dial in lockstep; the helper is extracted to src/shared/reconnect-jitter.ts and shared by both. STA-3320, #12327
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: Repository UI Review profile: CHILL Plan: Pro Plus Run ID: 📒 Files selected for processing (11)
📝 WalkthroughWalkthroughHeartbeat handling now tolerates consecutive missed probes up to a configurable limit. Responsive sockets and sockets with inbound activity reset their miss state. Tests cover delayed responses, event-loop pauses, shared probe cadence, and transient packet loss. Reconnect scheduling now uses a shared helper that adds up to 20% jitter, with tests for delay bounds and deterministic client reconnect timing. 🚥 Pre-merge checks | ✅ 3 | ❌ 2❌ Failed checks (2 warnings)
✅ Passed checks (3 passed)
✨ Finishing Touches 💡 1📝 Generate docstrings 💡
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 |
Post-merge review of #12790 demonstrated a real leak: the resume-from-pause pardon cleared banked misses outright, so a host whose sweep stalls once every three ticks reset the budget forever and a dead socket was never reaped. The reviewer ran 300 sweeps against a permanently dead peer with a >1.5x gap every third tick and observed zero terminate() calls. Pre-#12790 that required a stall on *every* tick; the counter widened the pathological window 3x, and the failure mode is permanent non-reaping — the MAX_WS_CONNECTIONS leak the reaper exists to prevent. A stalled tick now charges no miss, which is all the original rationale needed (the client had no chance to answer that probe), but no longer forgives the misses already banked. A live client still clears its own count by answering the probe that is still sent on the stalled tick. The tolerance test's pause case is rewritten to assert the new contract rather than the old forgive-everything one, and a new test pins the leak directly: a host stalling every third tick must still reap a dead socket.
…#12841) Post-merge review of #12790 demonstrated a real leak: the resume-from-pause pardon cleared banked misses outright, so a host whose sweep stalls once every three ticks reset the budget forever and a dead socket was never reaped. The reviewer ran 300 sweeps against a permanently dead peer with a >1.5x gap every third tick and observed zero terminate() calls. Pre-#12790 that required a stall on *every* tick; the counter widened the pathological window 3x, and the failure mode is permanent non-reaping — the MAX_WS_CONNECTIONS leak the reaper exists to prevent. A stalled tick now charges no miss, which is all the original rationale needed (the client had no chance to answer that probe), but no longer forgives the misses already banked. A live client still clears its own count by answering the probe that is still sent on the stalled tick. The tolerance test's pause case is rewritten to assert the new contract rather than the old forgive-everything one, and a new test pins the leak directly: a host stalling every third tick must still reap a dead socket. Co-authored-by: Jinwoo-H <[email protected]>
…ed socket (stablyai#12790) The paired-runtime WS heartbeat terminated a client after a single unanswered 15s ping. One missed pong is UNKNOWN, not proof the peer is gone: a cellular or Tailscale blackhole, or a stalled TCP retransmit, routinely swallows one pong from a peer that is still there. Users on flaky paths saw constant drops, each costing a full redial plus E2EE re-handshake and subscription replay. Reap now needs MISSED_PROBE_LIMIT (3) consecutive unanswered probes, counted per socket rather than timed. Any proof of life -- pong or any inbound frame -- clears the count, as does a resume from a server-loop pause, since a gap the client was never given a chance to answer must not top up its budget. Missed sweeps still re-probe, so a recovered path proves itself on the next tick. Three matches the liveness budgets already in the product: the web client gives 45s (25s idle + 20s probe grace) and the relay control gives 75s. The paired transport's single miss was the outlier. Also gives the web client's redial the one-sided jitter the shared-control path already had, so a fleet dropped by one shared blip does not re-dial in lockstep; the helper is extracted to src/shared/reconnect-jitter.ts and shared by both. STA-3320, stablyai#12327 Co-authored-by: Jinwoo-H <[email protected]>
…stablyai#12841) Post-merge review of stablyai#12790 demonstrated a real leak: the resume-from-pause pardon cleared banked misses outright, so a host whose sweep stalls once every three ticks reset the budget forever and a dead socket was never reaped. The reviewer ran 300 sweeps against a permanently dead peer with a >1.5x gap every third tick and observed zero terminate() calls. Pre-stablyai#12790 that required a stall on *every* tick; the counter widened the pathological window 3x, and the failure mode is permanent non-reaping — the MAX_WS_CONNECTIONS leak the reaper exists to prevent. A stalled tick now charges no miss, which is all the original rationale needed (the client had no chance to answer that probe), but no longer forgives the misses already banked. A live client still clears its own count by answering the probe that is still sent on the stalled tick. The tolerance test's pause case is rewritten to assert the new contract rather than the old forgive-everything one, and a new test pins the leak directly: a host stalling every third tick must still reap a dead socket. Co-authored-by: Jinwoo-H <[email protected]>
Fixes STA-3320 / #12327.
Symptom
The paired-runtime WebSocket heartbeat called
socket.terminate()after one missed 15s probe. One swallowed pong inside an ordinary Tailscale/cellular blackhole was treated as proof the peer was gone — and terminating tore down the E2EE channel, forcing a full handshake plus subscription replay. That is the drop users have been reporting.This bit idle sessions only. Any inbound frame already counted as proof of life, so a chatty client was never reaped. The failing case is precisely "agent is thinking, no output for 15s" — which is exactly when a user least wants their session dropped.
Worth stating plainly: STA-2006 ("does not recover after sleep") is genuinely fixed, and closing it does not help these users. It only means they recover from drops they should never have had. This is the cause.
Reproduction — red on
origin/mainReal
wsserver (the productionWebSocketTransport) and a realwsclient over loopback,autoPong: falseso exactly one protocol pong is dropped:The client answered probe #1, had probe #2's pong swallowed, and was terminated before probe #3 went out — demonstrably alive throughout. No wall-clock oracle: a step clock is injected (
heartbeatcallsnow()exactly once per sweep) so scheduler jitter cannot masquerade as a suspended loop, and assertions count probe events. Stable across 5/5 repeat runs.Main's own suite named the bug in a test title:
it('still reaps a client that misses a probe while another remains alive').Change
Reaping now requires 3 consecutive unanswered probes, counted per socket in a
WeakMap— bounded by counted events, never by elapsed time. Reset by:WeakSetwrite; the message hot path is unchanged), andMissed sweeps still re-probe, so a recovered path proves itself on the very next tick instead of serving out the remaining budget.
3 is not a tuned number. It aligns the outlier with budgets already shipped in this product: the web client allows 45s (25s idle + 20s probe grace) and relay control allows 75s (
RELAY_CONTROL_SILENCE_LIMIT_MS, 5 missed pings). The paired transport's single miss was the anomaly; 3 × 15s = 45s puts it exactly on the web client's existing budget.Also extracts the existing one-sided reconnect jitter to
src/shared/reconnect-jitter.tsand applies it to the web client, which had none.Mutation evidence
Each restored by re-applying the edit, never
git checkout:false &&)The last one matters most: the suite still demands that a genuinely dead socket dies.
Independently re-verified after rebase onto current main — reverting the threshold fails all 5. Suites:
src/main/runtime/rpc/+src/shared/— 539 files, 5232 passing.Corrections to the original report
watchdog.ts:2)" — wrong; no such file. The nearest isrelay-control-silence-watchdog.ts:2, which is the relay's 75s bound. A resume rebaseline does exist (resumedFromPause). The substance held: it covers tick gaps, not packet loss.scheduleSharedControlReconnectalready had jitter, so "N sockets re-dial in lockstep" was overstated.Risk
WebSocketTransportis instantiated exactly once (runtime-rpc.ts:1264) and requires a pairing identity, so the blast radius is exactly the three direct-dial client types: paired desktop, mobile, web — where the change is the intended one. Local panes have no WebSocket. SSH/relay useRelayTransportand their own silence watchdog; neither imports this heartbeat. Folder workspaces are orthogonal.One trade-off for reviewers: a silent half-open now lingers up to ~60s instead of ~30s against
MAX_WS_CONNECTIONS = 128. Clean FIN/RST closes remain immediate and the 10s pre-auth reaper is untouched, so the unauthenticated-socket DoS surface is unchanged — the extra lingering applies only to authenticated half-opens.Not done
No paired Electron E2E. The real-socket test drives the production transport with real ping/pong frames and is mutation-proven; an E2E that could not be mutation-verified as cheaply would have added confidence-shaped noise rather than evidence.