Skip to content

Commit 0895455

Browse files
committed
fix(client-runtime): terminate durable subscription on terminal thread miss
Session replacements re-issued subscribeThread after a not-found tombstone because the terminal latch only filtered foreground wakeups while the outer session stream in subscribeDynamic stayed alive. Signal a halt Deferred from the terminalFailure handler and interrupt the outer session stream so no new subscribe issues; non-matching failures keep session-driven resubscription.
1 parent a506661 commit 0895455

2 files changed

Lines changed: 91 additions & 1 deletion

File tree

‎packages/client-runtime/src/rpc/client.ts‎

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { ORCHESTRATION_WS_METHODS, WS_METHODS } from "@t3tools/contracts";
22
import * as Cause from "effect/Cause";
33
import * as Context from "effect/Context";
4+
import * as Deferred from "effect/Deferred";
45
import type * as Duration from "effect/Duration";
56
import * as Effect from "effect/Effect";
67
import * as Option from "effect/Option";
@@ -200,6 +201,10 @@ export function subscribeDynamic<TTag extends EnvironmentSubscriptionRpcTag>(
200201
Effect.gen(function* () {
201202
const supervisor = yield* EnvironmentSupervisor;
202203
const observer = yield* EnvironmentRpcSubscriptionObserver;
204+
// Signaled once a terminalFailure handler runs. Interrupts the outer
205+
// session stream below so a later supervisor.session replacement cannot
206+
// re-issue the subscription after a terminal tombstone.
207+
const terminalHalt = yield* Deferred.make<void>();
203208
const sessionChanges = SubscriptionRef.changes(supervisor.session);
204209
const sessions =
205210
options?.resubscribe === undefined
@@ -211,6 +216,7 @@ export function subscribeDynamic<TTag extends EnvironmentSubscriptionRpcTag>(
211216
),
212217
);
213218
return sessions.pipe(
219+
Stream.interruptWhen(Deferred.await(terminalHalt)),
214220
Stream.switchMap(
215221
Option.match({
216222
onNone: () => Stream.empty,
@@ -267,7 +273,15 @@ export function subscribeDynamic<TTag extends EnvironmentSubscriptionRpcTag>(
267273
).pipe(Stream.drain);
268274
}
269275
if (isTerminal && terminal !== undefined) {
270-
return Stream.fromEffect(terminal.handle(cause)).pipe(Stream.drain);
276+
return Stream.fromEffect(
277+
terminal
278+
.handle(cause)
279+
.pipe(
280+
Effect.ensuring(
281+
Deferred.succeed(terminalHalt, undefined).pipe(Effect.asVoid),
282+
),
283+
),
284+
).pipe(Stream.drain);
271285
}
272286
if (hasOnlyExpectedFailures && options?.onExpectedFailure !== undefined) {
273287
const handled = Stream.fromEffect(

‎packages/client-runtime/src/state/threads-sync.test.ts‎

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1049,6 +1049,82 @@ describe("EnvironmentThreads", () => {
10491049
}),
10501050
);
10511051

1052+
it.effect("does not resubscribe a missing thread on session replacement", () =>
1053+
Effect.gen(function* () {
1054+
const harness = yield* makeHarness({ cached: BASE_THREAD });
1055+
yield* Queue.offer(
1056+
harness.inputs,
1057+
new OrchestrationGetSnapshotError({
1058+
message: `Thread ${THREAD_ID} was not found`,
1059+
cause: THREAD_ID,
1060+
threadDisposition: "not-found",
1061+
}),
1062+
);
1063+
yield* awaitThreadState(harness.observed, (value) => value.status === "deleted");
1064+
expect(yield* Ref.get(harness.subscriptionCount)).toBe(1);
1065+
1066+
// A supervisor.session replacement drives the outer session stream in
1067+
// subscribeDynamic. The terminal tombstone must terminate that path too,
1068+
// not just foreground wakeups.
1069+
yield* harness.replaceSession;
1070+
yield* TestClock.adjust("30 seconds");
1071+
for (let attempt = 0; attempt < 100; attempt += 1) {
1072+
yield* Effect.yieldNow;
1073+
}
1074+
1075+
expect(yield* Ref.get(harness.subscriptionCount)).toBe(1);
1076+
const latest = yield* Ref.get(harness.latest);
1077+
expect(latest.status).toBe("deleted");
1078+
expect(Option.isNone(latest.data)).toBe(true);
1079+
expect(yield* Ref.get(harness.loaderCalls)).toBe(0);
1080+
}),
1081+
);
1082+
1083+
it.effect("resubscribes on session replacement after a non-matching failure", () =>
1084+
Effect.gen(function* () {
1085+
const harness = yield* makeHarness();
1086+
yield* Queue.offer(
1087+
harness.inputs,
1088+
new OrchestrationGetSnapshotError({
1089+
message: `Failed to load thread ${THREAD_ID}`,
1090+
cause: THREAD_ID,
1091+
}),
1092+
);
1093+
1094+
const failed = yield* awaitThreadState(harness.observed, (value) =>
1095+
Option.isSome(value.error),
1096+
);
1097+
expect(failed.status).not.toBe("deleted");
1098+
expect(yield* Ref.get(harness.subscriptionCount)).toBe(1);
1099+
1100+
// Non-terminal failures keep the session-driven path alive: a
1101+
// replacement session must re-issue subscribeThread.
1102+
yield* harness.replaceSession;
1103+
for (let attempt = 0; attempt < 100; attempt += 1) {
1104+
if ((yield* Ref.get(harness.subscriptionCount)) >= 2) break;
1105+
yield* Effect.yieldNow;
1106+
}
1107+
expect(yield* Ref.get(harness.subscriptionCount)).toBe(2);
1108+
1109+
yield* Queue.offer(
1110+
harness.inputs,
1111+
snapshot({
1112+
...BASE_THREAD,
1113+
title: "Recovered thread",
1114+
}),
1115+
);
1116+
const recovered = yield* awaitThreadState(
1117+
harness.observed,
1118+
(value) =>
1119+
value.status === "live" &&
1120+
Option.isSome(value.data) &&
1121+
value.data.value.title === "Recovered thread",
1122+
);
1123+
expect(Option.isNone(recovered.error)).toBe(true);
1124+
expect(yield* Ref.get(harness.removedThreads)).toEqual([]);
1125+
}),
1126+
);
1127+
10521128
it.effect("does not resurrect a missing thread via queued persistence", () =>
10531129
Effect.gen(function* () {
10541130
const harness = yield* makeHarness({ cached: BASE_THREAD });

0 commit comments

Comments
 (0)