From a6553e67bd49e9f6d830d20ae565ecbe4474a948 Mon Sep 17 00:00:00 2001 From: DongHyeonka Date: Thu, 24 Sep 2026 12:03:37 +0900 Subject: [PATCH] fix(realtime): refuse recovery once its quiescence ends in DRAINING (R-4.1) runRecovery() checked lifecycle === "DRAINING" only on entry. After `await quiescence` it re-checked closed/abort/scope but not DRAINING, so an effect that timed out while the recovery waited left the stream DRAINING and the recovery still called the authority, committed a new resume state and cleared recoveryRequired while the abandoned effect kept running (the RT-RR-01/02 case). The post-quiescence check now also fails with CLOSED/RECOVER when the stream is DRAINING, without calling the authority. Test: "fails a recovery whose effect quiescence ends in DRAINING" in stream-coordinator.test.ts. RED before the fix: the recovery resolved { ok: true } (authority called and committed). After: CLOSED, authority not called, no resume state; once the late effect settles the stream is OPEN and STALE. Gates: check:architecture, check:types:app, check:types:test, lint, test:component pass. test:unit passes except removal-fixture.test.ts, which needs generated artifacts/release evidence absent from this fresh worktree (unrelated). Co-Authored-By: Claude Opus 5.5 --- src/adapters/realtime/stream-coordinator.ts | 4 +- .../unit/realtime/stream-coordinator.test.ts | 52 +++++++++++++++++++ 2 files changed, 55 insertions(+), 1 deletion(-) diff --git a/src/adapters/realtime/stream-coordinator.ts b/src/adapters/realtime/stream-coordinator.ts index f57cc960..2f702c50 100644 --- a/src/adapters/realtime/stream-coordinator.ts +++ b/src/adapters/realtime/stream-coordinator.ts @@ -707,7 +707,9 @@ export function createRealtimeStreamCoordinator( if (!calledFromCurrentJob) { await quiescence; } - if (closed || state.closed) { + // RT-RR-01. The effect this recovery waited on can time out and keep + // running; recovering beside it would let its late write win. + if (closed || state.closed || state.lifecycle === "DRAINING") { state.freshness = "UNKNOWN"; return realtimeFailure("CLOSED", "RECOVER"); } diff --git a/tests/unit/realtime/stream-coordinator.test.ts b/tests/unit/realtime/stream-coordinator.test.ts index 4bf93e45..f3d154e8 100644 --- a/tests/unit/realtime/stream-coordinator.test.ts +++ b/tests/unit/realtime/stream-coordinator.test.ts @@ -1053,6 +1053,58 @@ describe("realtime physical task ownership", () => { firstApply.resolve(realtimeSuccess(undefined)); }); + + it("fails a recovery whose effect quiescence ends in DRAINING", async () => { + const wedged = deferred>(); + let recoveries = 0; + const harness = createHarness({ + apply: async () => wedged.promise, + recover: async () => { + recoveries += 1; + return realtimeSuccess(snapshotCommit("0")); + }, + taskLimits: { effectTimeoutMs: 5, drainTimeoutMs: 5 }, + }); + await harness.initialize(); + recoveries = 0; + + const applying = harness.coordinator.accept( + harness.event({ + eventId: "event-00000001", + sequence: "1", + resumeCursor: "cursor-00000001", + }), + ); + for (let flush = 0; flush < 10; flush += 1) await Promise.resolve(); + expect(harness.effects).toHaveBeenCalledOnce(); + expect(harness.coordinator.lifecycle(STREAM_ID)).toBe("OPEN"); + // The effect ignores abort, so it outlives its deadline and the stream + // enters DRAINING while this recovery waits for quiescence. + const recovering = harness.coordinator.recover( + STREAM_ID, + "SEQUENCE_GAP", + ); + + await expect(applying).resolves.toMatchObject({ + ok: false, + error: { kind: "IDLE_TIMEOUT", operation: "APPLY" }, + }); + await expect(recovering).resolves.toMatchObject({ + ok: false, + error: { kind: "CLOSED", operation: "RECOVER" }, + }); + expect(recoveries).toBe(0); + expect(harness.coordinator.inspect(STREAM_ID).hasResumeState).toBe( + false, + ); + + // The late effect settles; the stream still needs a real recovery. + wedged.resolve(realtimeSuccess(undefined)); + await vi.waitFor(() => + expect(harness.coordinator.lifecycle(STREAM_ID)).toBe("OPEN"), + ); + expect(harness.coordinator.inspect(STREAM_ID).freshness).toBe("STALE"); + }); }); type HarnessOptions = Readonly<{