Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
92 changes: 92 additions & 0 deletions packages/client-runtime/src/state/threads-sync.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)",
() =>
Expand Down
24 changes: 20 additions & 4 deletions packages/client-runtime/src/state/threads.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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: {
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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;
}),
);
Expand Down