From 60b80e4a1d2e9083c0f4a42a1c54c39dda519197 Mon Sep 17 00:00:00 2001 From: Mike Olson Date: Fri, 6 Nov 2026 13:44:08 -0500 Subject: [PATCH] fix(client-runtime): Recover failed subscriptions from a fresh snapshot A failed subscription may leave its cursor ahead of unapplied data. Upstream already retries the stream, but resuming from that cursor can lose updates. Mark a cached thread synchronizing and request a fresh snapshot on recovery, while retaining upstream's retry timing. Remove a deleted thread returned by the snapshot, since it replaces the replay that would carry thread.deleted. # Conflicts: # packages/client-runtime/src/state/threads.ts --- .../src/state/threads-sync.test.ts | 92 +++++++++++++++++++ packages/client-runtime/src/state/threads.ts | 24 ++++- 2 files changed, 112 insertions(+), 4 deletions(-) diff --git a/packages/client-runtime/src/state/threads-sync.test.ts b/packages/client-runtime/src/state/threads-sync.test.ts index d14b8a66c40cd..e066e49da429d 100644 --- a/packages/client-runtime/src/state/threads-sync.test.ts +++ b/packages/client-runtime/src/state/threads-sync.test.ts @@ -2467,6 +2467,98 @@ describe("EnvironmentThreads", () => { }), ); + it.effect.each(["stream", "apply"] as const)( + "resubscribes from a fresh snapshot after a %s failure", + (failure) => + Effect.gen(function* () { + const harness = yield* makeHarness({ cached: BASE_PROJECTION, completionMarker: true }); + yield* Queue.offer(harness.inputs, synchronized()); + yield* awaitThreadState(harness.observed, (value) => value.status === "live"); + expect(yield* Ref.get(harness.lastSubscribeAfterSequence)).toBe(CACHED_SNAPSHOT_SEQUENCE); + + const dieAndResubscribe = (expectedSubscriptions: number, isResuming: boolean) => + Effect.gen(function* () { + const brokenSnapshot = { + ...snapshot(BASE_PROJECTION, CACHED_SNAPSHOT_SEQUENCE + 1), + get snapshotSequence(): number { + throw new Error("Snapshot application failed"); + }, + }; + if (failure === "stream" && isResuming) { + // A resumed stream defect retries once immediately from a snapshot, then backs off if that reload fails too. + yield* Queue.offer(harness.inputs, TEST_DEFECT); + yield* SubscriptionRef.changes(harness.subscriptionCount).pipe( + Stream.filter((count) => count >= expectedSubscriptions), + Stream.runHead, + ); + expect(yield* Ref.get(harness.lastSubscribeAfterSequence)).toBeUndefined(); + yield* Queue.offer(harness.inputs, TEST_DEFECT); + const failed = yield* awaitThreadState(harness.observed, (value) => + Option.isSome(value.error), + ); + expect(failed.status).toBe("synchronizing"); + yield* TestClock.adjust("60 seconds"); + yield* SubscriptionRef.changes(harness.subscriptionCount).pipe( + Stream.filter((count) => count >= expectedSubscriptions + 1), + Stream.runHead, + ); + expect(yield* Ref.get(harness.lastSubscribeAfterSequence)).toBeUndefined(); + return; + } + if (failure === "stream") { + yield* Queue.offer(harness.inputs, TEST_DEFECT); + } else { + yield* Queue.offerAll(harness.inputs, [brokenSnapshot, synchronized()]); + } + const failed = yield* awaitThreadState(harness.observed, (value) => + Option.isSome(value.error), + ); + expect(failed.status).toBe("synchronizing"); + yield* TestClock.adjust("60 seconds"); + yield* SubscriptionRef.changes(harness.subscriptionCount).pipe( + Stream.filter((count) => count >= expectedSubscriptions), + Stream.runHead, + ); + expect(yield* Ref.get(harness.lastSubscribeAfterSequence)).toBeUndefined(); + }); + + yield* dieAndResubscribe(2, true); + yield* Queue.offerAll(harness.inputs, [ + snapshot( + { + ...BASE_PROJECTION, + thread: { ...BASE_PROJECTION.thread, title: "Recovered thread" }, + }, + CACHED_SNAPSHOT_SEQUENCE + 5, + ), + synchronized(), + ]); + const recovered = yield* awaitThreadState( + harness.observed, + (value) => + value.status === "live" && + Option.isSome(value.data) && + value.data.value.thread.title === "Recovered thread", + ); + expect(Option.isNone(recovered.error)).toBe(true); + + // The resync snapshot stands in for the replay that would carry thread.deleted. + yield* dieAndResubscribe(failure === "stream" ? 4 : 3, false); + yield* Queue.offerAll(harness.inputs, [ + snapshot( + { ...BASE_PROJECTION, thread: { ...BASE_PROJECTION.thread, deletedAt: v2Now } }, + CACHED_SNAPSHOT_SEQUENCE + 9, + ), + synchronized(), + ]); + const removed = yield* awaitThreadState( + harness.observed, + (value) => value.status === "deleted", + ); + expect(Option.isNone(removed.data)).toBe(true); + }), + ); + it.effect( "resubscribes after a snapshot decode defect ends the thread subscription (#23041)", () => diff --git a/packages/client-runtime/src/state/threads.ts b/packages/client-runtime/src/state/threads.ts index c0921417ba5cf..525723d6394a9 100644 --- a/packages/client-runtime/src/state/threads.ts +++ b/packages/client-runtime/src/state/threads.ts @@ -695,14 +695,19 @@ export const makeEnvironmentThreadState = Effect.fn("EnvironmentThreadState.make } if (item.kind === "snapshot") { - yield* Ref.set(forceSnapshot, false); + const resynced = yield* Ref.getAndSet(forceSnapshot, false); yield* SubscriptionRef.set(lastSequence, item.snapshotSequence); + const projection = boundedSnapshotProjection(item); + if (resynced && projection.thread.deletedAt !== null) { + yield* setDeleted(); + return; + } const hasProgressiveHistory = item.historyCursor !== undefined || item.hasMoreHistory !== undefined || item.latestLocalTurnOrdinal !== undefined; yield* setThread( - boundedSnapshotProjection(item), + projection, hasProgressiveHistory ? { history: { @@ -1073,10 +1078,20 @@ export const makeEnvironmentThreadState = Effect.fn("EnvironmentThreadState.make // A 404 can arrive before the thread exists, so a miss asks HTTP again while mounted. let httpMisses = 0; let defectRetries = 0; - const backOffAfterDefect = Effect.suspend(() => { + const backOffAfterDefect = Effect.gen(function* () { const retryDelay = subscriptionRetryDelay(THREAD_SUBSCRIPTION_RETRY_DELAY, defectRetries); defectRetries += 1; - return setStreamError(THREAD_SYNC_ERROR).pipe(Effect.andThen(Effect.sleep(retryDelay))); + yield* Ref.set(forceSnapshot, true); + yield* Ref.set(resumingLive, false); + yield* Ref.set(awaitingCompletion, false); + yield* SubscriptionRef.update(state, (current) => { + let status = current.status; + if (status !== "deleted") { + status = Option.isSome(current.data) ? "synchronizing" : "empty"; + } + return { ...current, status, error: Option.some(THREAD_SYNC_ERROR) }; + }); + yield* Effect.sleep(retryDelay); }); const followThread = (): Effect.Effect< void, @@ -1148,6 +1163,7 @@ export const makeEnvironmentThreadState = Effect.fn("EnvironmentThreadState.make } : EMPTY_THREAD_HISTORY_META; yield* setThread(httpResult.snapshot.projection, { history }); + yield* Ref.set(forceSnapshot, false); yield* remember; }), );