Skip to content

fix(runtime): require consecutive missed probes before reaping a paired socket (STA-3320) - #12790

Merged
Jinwoo-H merged 1 commit into
mainfrom
fix/sta-3320-heartbeat-missed-probes
Aug 6, 2026
Merged

Jinwoo-H merged 1 commit into
mainfrom
fix/sta-3320-heartbeat-missed-probes

Conversation

@Jinwoo-H

@Jinwoo-H Jinwoo-H commented Aug 5, 2026

Copy link
Copy Markdown
Contributor

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/main

Real ws server (the production WebSocketTransport) and a real ws client over loopback, autoPong: false so exactly one protocol pong is dropped:

node_modules/.bin/vitest run --config config/vitest.config.ts \
  src/main/runtime/rpc/ws-transport-transient-packet-loss.test.ts
AssertionError: expected { closed: true, probesReceived: 2 } to deeply equal { closed: false, probesReceived: 4 }

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 (heartbeat calls now() 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:

  • any proof of life (pong or any inbound frame — one WeakSet write; the message hot path is unchanged), and
  • a resume from a server-loop pause, so a suspend cannot spend a budget the client never had a chance to defend.

Missed 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.ts and applies it to the web client, which had none.

Mutation evidence

Each restored by re-applying the edit, never git checkout:

Mutation Killed by
Threshold back to 1 all 5 new tests, including the real-socket one
Resume forgives the reap but keeps banked misses only test 4 — proves the resume-reset is independently pinned
Proof-of-life no longer clears misses tests 3 and 4
Never reap (false &&) 4 tests, including both pre-existing reap tests

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

  • "There is no sleep rebaseline (watchdog.ts:2)" — wrong; no such file. The nearest is relay-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.
  • "The client redial is jitterless" — true only of the web client; scheduleSharedControlReconnect already had jitter, so "N sockets re-dial in lockstep" was overstated.

Risk

WebSocketTransport is 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 use RelayTransport and 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.

…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
@coderabbitai

coderabbitai Bot commented Aug 5, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Repository UI

Review profile: CHILL

Plan: Pro Plus

Run ID: 0039d723-f6fd-40a0-ad0f-99d5b78b13c4

📥 Commits

Reviewing files that changed from the base of the PR and between ae1ed5e and c5c655f.

📒 Files selected for processing (11)
  • src/main/runtime/rpc/remote-runtime-server-heartbeat-missed-probe-tolerance.test.ts
  • src/main/runtime/rpc/remote-runtime-server-heartbeat.test.ts
  • src/main/runtime/rpc/remote-runtime-server-heartbeat.ts
  • src/main/runtime/rpc/ws-transport-accept-order.test.ts
  • src/main/runtime/rpc/ws-transport-transient-packet-loss.test.ts
  • src/main/runtime/rpc/ws-transport.ts
  • src/renderer/src/web/web-runtime-client.test.ts
  • src/renderer/src/web/web-runtime-client.ts
  • src/shared/reconnect-jitter.test.ts
  • src/shared/reconnect-jitter.ts
  • src/shared/remote-runtime-shared-control-state.ts

📝 Walkthrough

Walkthrough

Heartbeat 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)

Check name Status Explanation Resolution
Description check ⚠️ Warning The description is detailed and relevant, but it omits the required Screenshots, AI Review Report, and Security Audit sections and does not confirm cross-platform review. Add all required template sections, state whether there is visual change, complete the testing checklist, and document AI cross-platform and security reviews.
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (3 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly states the primary heartbeat change: requiring consecutive missed probes before reaping paired sockets.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
✨ Finishing Touches 💡 1
📝 Generate docstrings 💡
  • Create stacked PR
  • Commit on current branch

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.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@Jinwoo-H
Jinwoo-H merged commit 211b2d1 into main Aug 6, 2026
44 checks passed
@Jinwoo-H
Jinwoo-H deleted the fix/sta-3320-heartbeat-missed-probes branch August 6, 2026 01:59
Jinwoo-H added a commit that referenced this pull request Aug 6, 2026
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.
Jinwoo-H added a commit that referenced this pull request Aug 6, 2026
…#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]>
enrick-dev pushed a commit to enrick-dev/orca that referenced this pull request Oct 15, 2026
…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]>
enrick-dev pushed a commit to enrick-dev/orca that referenced this pull request Oct 15, 2026
…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]>
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