From ff01fad4adda08423f657d10ecacaf682e0a1601 Mon Sep 17 00:00:00 2001 From: Jinwoo Hong <73622457+Jinwoo-H@users.noreply.github.com> Date: Wed, 5 Aug 2026 20:28:10 -0700 Subject: [PATCH] fix(runtime): stop a stalled tick from forgiving banked missed probes (#12841) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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 --- ...r-heartbeat-missed-probe-tolerance.test.ts | 44 +++++++++++++++---- .../rpc/remote-runtime-server-heartbeat.ts | 15 +++---- 2 files changed, 43 insertions(+), 16 deletions(-) diff --git a/src/main/runtime/rpc/remote-runtime-server-heartbeat-missed-probe-tolerance.test.ts b/src/main/runtime/rpc/remote-runtime-server-heartbeat-missed-probe-tolerance.test.ts index 2e3a34271..665d94b0f 100644 --- a/src/main/runtime/rpc/remote-runtime-server-heartbeat-missed-probe-tolerance.test.ts +++ b/src/main/runtime/rpc/remote-runtime-server-heartbeat-missed-probe-tolerance.test.ts @@ -53,7 +53,9 @@ describe('RemoteRuntimeServerHeartbeat missed-probe tolerance', () => { for (let probe = 0; probe < 6; probe += 1) { now += INTERVAL_MS await vi.advanceTimersByTimeAsync(INTERVAL_MS) - missesBeforeReap.push((socket.terminate as unknown as ReturnType).mock.calls.length) + missesBeforeReap.push( + (socket.terminate as unknown as ReturnType).mock.calls.length + ) } // Silence must be tolerated for more than a single probe, and must eventually end the socket. @@ -85,7 +87,7 @@ describe('RemoteRuntimeServerHeartbeat missed-probe tolerance', () => { heartbeat.stop() }) - it('clears accumulated misses when the server event loop resumes from a pause', async () => { + it('charges no miss for a paused tick but keeps the ones already banked', async () => { vi.useFakeTimers() let now = 1_000 const socket = makeSocket() @@ -114,8 +116,9 @@ describe('RemoteRuntimeServerHeartbeat missed-probe tolerance', () => { expect(toleratedMisses).toBeGreaterThan(1) heartbeat.stop() - // Replay on a fresh socket: bank misses to one short of the limit, suspend, then bank the same - // number again. The pause must write the earlier misses off, not top them up to the limit. + // Replay on a fresh socket: bank misses to one short of the limit, then suspend. The stalled tick + // must charge nothing — the client had no chance to answer it — but it must not forgive the misses + // already banked, or a host that stalls periodically could shelter a dead socket indefinitely. const resumed = new RemoteRuntimeServerHeartbeat(INTERVAL_MS, () => now) const resumedSocket = makeSocket() resumed.noteAlive(resumedSocket) @@ -129,11 +132,36 @@ describe('RemoteRuntimeServerHeartbeat missed-probe tolerance', () => { now += 3_600_000 await vi.advanceTimersByTimeAsync(INTERVAL_MS) - for (let miss = 0; miss < toleratedMisses - 1; miss += 1) { - await sweep() - } - + // The suspend itself costs the socket nothing. expect(reaped(resumedSocket)).toBe(false) + + // But the pre-suspend evidence survives, so the very next unanswered probe completes the budget. + await sweep() + expect(reaped(resumedSocket)).toBe(true) resumed.stop() }) + + // Why: pre-fix the pardon cleared banked misses, so a host stalling once every few ticks reset the + // budget forever and a dead socket was never reaped — the connection leak the reaper exists to stop. + it('still reaps a dead socket when the host stalls repeatedly', async () => { + vi.useFakeTimers() + let now = 1_000 + const socket = makeSocket() + const heartbeat = new RemoteRuntimeServerHeartbeat(INTERVAL_MS, () => now) + heartbeat.noteAlive(socket) + heartbeat.start(() => [socket]) + heartbeat.noteAlive(socket) + + const reaped = (): boolean => + (socket.terminate as unknown as ReturnType).mock.calls.length > 0 + + // Stall every third tick — the worst case the old pardon could not survive. + for (let tick = 0; tick < 30 && !reaped(); tick += 1) { + now += tick % 3 === 2 ? INTERVAL_MS * 2 : INTERVAL_MS + await vi.advanceTimersByTimeAsync(INTERVAL_MS) + } + + expect(reaped()).toBe(true) + heartbeat.stop() + }) }) diff --git a/src/main/runtime/rpc/remote-runtime-server-heartbeat.ts b/src/main/runtime/rpc/remote-runtime-server-heartbeat.ts index 3913e9d9b..6134259c5 100644 --- a/src/main/runtime/rpc/remote-runtime-server-heartbeat.ts +++ b/src/main/runtime/rpc/remote-runtime-server-heartbeat.ts @@ -52,21 +52,20 @@ export class RemoteRuntimeServerHeartbeat { const tickAt = this.now() const elapsedMs = tickAt - (this.lastTickAt ?? tickAt) this.lastTickAt = tickAt - const resumedFromPause = elapsedMs < 0 || elapsedMs > this.intervalMs * 1.5 + // Why: a delayed tick cannot infer that clients missed a probe they had no chance to answer, so + // this sweep must not charge one. It must not *forgive* either: clearing banked misses lets a host + // that stalls once every few ticks protect a dead socket forever, which is the connection leak the + // reaper exists to prevent. Skipping the charge is enough — a live client clears its own count by + // answering the probe still sent below. + const stalledTick = elapsedMs < 0 || elapsedMs > this.intervalMs * 1.5 let reaped = 0 let clientCount = 0 for (const socket of clients) { clientCount += 1 - if (resumedFromPause) { - // Why: a delayed server tick cannot infer that clients missed a probe they had no chance to - // answer. Seeding alive also clears any misses banked before the pause, so a suspend can never - // top up a budget the client was never given a chance to defend. - this.alive.add(socket) - } if (this.alive.has(socket)) { this.alive.delete(socket) this.missedProbes.delete(socket) - } else { + } else if (!stalledTick) { const missed = (this.missedProbes.get(socket) ?? 0) + 1 if (missed >= this.missedProbeLimit) { this.missedProbes.delete(socket)