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
67 changes: 67 additions & 0 deletions packages/client-runtime/src/state/threads-sync.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2452,6 +2452,73 @@ 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) =>
Effect.gen(function* () {
const brokenSnapshot = {
...snapshot(BASE_PROJECTION, CACHED_SNAPSHOT_SEQUENCE + 1),
get snapshotSequence(): number {
throw new Error("Snapshot application failed");
},
};
yield* Queue.offer(harness.inputs, failure === "stream" ? TEST_DEFECT : brokenSnapshot);
const failed = yield* awaitThreadState(harness.observed, (value) =>
Option.isSome(value.error),
);
expect(failed.status).toBe("synchronizing");
yield* TestClock.adjust("250 millis");
yield* SubscriptionRef.changes(harness.subscriptionCount).pipe(
Stream.filter((count) => count >= expectedSubscriptions),
Stream.runHead,
);
expect(yield* Ref.get(harness.lastSubscribeAfterSequence)).toBeUndefined();
});

yield* dieAndResubscribe(2);
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(3);
yield* Queue.offer(
harness.inputs,
snapshot(
{ ...BASE_PROJECTION, thread: { ...BASE_PROJECTION.thread, deletedAt: v2Now } },
CACHED_SNAPSHOT_SEQUENCE + 9,
),
);
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
27 changes: 23 additions & 4 deletions packages/client-runtime/src/state/threads.ts
Original file line number Diff line number Diff line change
Expand Up @@ -317,6 +317,8 @@ export const makeEnvironmentThreadState = Effect.fn("EnvironmentThreadState.make
};
if (resumeCache?.owner === owner) resumeCache.snapshot = committed;
const awaitingCompletion = yield* Ref.make(false);
// A failed application may leave the cursor ahead of data, so recovery needs a snapshot.
const resyncFromSnapshot = yield* Ref.make(false);
const applyLock = yield* Semaphore.make(1);
// Save only completed data/cursor updates. A canceled scope must not cache
// a cursor whose event has not reached the data yet.
Expand Down Expand Up @@ -674,13 +676,19 @@ export const makeEnvironmentThreadState = Effect.fn("EnvironmentThreadState.make
}

if (item.kind === "snapshot") {
const resynced = yield* Ref.getAndSet(resyncFromSnapshot, 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 @@ -1013,10 +1021,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(resyncFromSnapshot, 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 @@ -1087,6 +1105,7 @@ export const makeEnvironmentThreadState = Effect.fn("EnvironmentThreadState.make
}
: EMPTY_THREAD_HISTORY_META;
yield* setThread(httpResult.snapshot.projection, { history });
yield* Ref.set(resyncFromSnapshot, false);
yield* remember;
}),
);
Expand All @@ -1110,7 +1129,7 @@ export const makeEnvironmentThreadState = Effect.fn("EnvironmentThreadState.make
}

const sequence = yield* SubscriptionRef.get(lastSequence);
const canResume = Option.isSome(current.data);
const canResume = Option.isSome(current.data) && !(yield* Ref.get(resyncFromSnapshot));
const acceptBoundedSnapshot = yield* Ref.get(acceptsBoundedSocketSnapshots);

return {
Expand Down