Skip to content

Commit ac76d3d

Browse files
t3dotggclaude
andcommitted
fix(client): a cancelled subscription keeps the record of the shell load it started
Two review findings in the early shell load. - The early load ref was stored after the uninterruptible callback. A subscription cancelled while the callback waited for the previous load started the next load, then took the interrupt before the store. That load ran on with no record, and the next caller started a second one. The whole modification is now uninterruptible. - A `resumable` answer skipped the age check at socket time. Rows that pass the 12 hour limit while the socket opens now ask the socket for a full snapshot. Co-Authored-By: Claude Fable 5.2 <[email protected]>
1 parent da42b86 commit ac76d3d

2 files changed

Lines changed: 81 additions & 20 deletions

File tree

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

Lines changed: 71 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1167,7 +1167,12 @@ describe("environment shell cold start", () => {
11671167
const makeColdStart = Effect.fn("test.makeColdStartShell")(function* (
11681168
origin: Persistence.ShellCacheOrigin | undefined,
11691169
// Set for a connection that is prepared but has no session yet.
1170-
early?: { readonly serverResumes: boolean; readonly holdLoads?: boolean },
1170+
// Each held load waits for one `releaseLoads`. An "uninterruptible" hold stands for a load
1171+
// that applies its answer, which an interrupt has to wait for.
1172+
early?: {
1173+
readonly serverResumes: boolean;
1174+
readonly holdLoads?: boolean | "uninterruptible";
1175+
},
11711176
) {
11721177
yield* TestClock.setTime(NOW);
11731178
const subscriptions = yield* Queue.unbounded<{
@@ -1180,8 +1185,17 @@ describe("environment shell cold start", () => {
11801185
}>();
11811186
const httpLoads = yield* Ref.make(0);
11821187
const loadOptions = yield* Queue.unbounded<ShellSnapshotLoader.ShellSnapshotOptions>();
1183-
const loadsReleased = yield* Deferred.make<void>();
1184-
if (early?.holdLoads !== true) yield* Deferred.succeed(loadsReleased, undefined);
1188+
const releases = yield* Queue.unbounded<void>();
1189+
const scope = yield* Effect.scope;
1190+
const holdLoad =
1191+
early?.holdLoads === "uninterruptible"
1192+
? // Registered after the load's fiber, so the end of a failed test does not wait for it.
1193+
Scope.addFinalizer(scope, Queue.shutdown(releases)).pipe(
1194+
Effect.andThen(Effect.uninterruptible(Queue.take(releases))),
1195+
)
1196+
: early?.holdLoads === true
1197+
? Queue.take(releases)
1198+
: Effect.void;
11851199
const client = {
11861200
[ORCHESTRATION_V2_WS_METHODS.subscribeShell]: (input: unknown) =>
11871201
Stream.unwrap(
@@ -1230,7 +1244,7 @@ describe("environment shell cold start", () => {
12301244
const snapshotLoader = ShellSnapshotLoader.ShellSnapshotLoader.of({
12311245
load: (_prepared, options = {}) =>
12321246
Queue.offer(loadOptions, options).pipe(
1233-
Effect.andThen(Deferred.await(loadsReleased)),
1247+
Effect.andThen(holdLoad),
12341248
Effect.andThen(Ref.updateAndGet(httpLoads, (count) => count + 1)),
12351249
Effect.map((count) =>
12361250
Option.some<ShellSnapshotLoader.ShellHttpAnswer>(
@@ -1250,21 +1264,23 @@ describe("environment shell cold start", () => {
12501264
Effect.provideService(Persistence.EnvironmentCacheStore, cache),
12511265
Effect.provideService(ShellSnapshotLoader.ShellSnapshotLoader, snapshotLoader),
12521266
);
1267+
const nextConnection = Effect.suspend(() =>
1268+
SubscriptionRef.set(
1269+
prepared,
1270+
Option.some({ ...PREPARED, descriptor: { ...FAMILY_DESCRIPTOR } }),
1271+
),
1272+
);
12531273
return {
12541274
shellState,
12551275
subscriptions,
12561276
saved,
12571277
httpLoads,
12581278
loadOptions,
1259-
releaseLoads: Deferred.succeed(loadsReleased, undefined),
1279+
releaseLoads: Queue.offer(releases, undefined),
1280+
nextConnection,
12601281
// A connection attempt that ended, then the next one.
12611282
prepareAgain: SubscriptionRef.set(prepared, Option.none()).pipe(
1262-
Effect.andThen(
1263-
SubscriptionRef.set(
1264-
prepared,
1265-
Option.some({ ...PREPARED, descriptor: { ...FAMILY_DESCRIPTOR } }),
1266-
),
1267-
),
1283+
Effect.andThen(nextConnection),
12681284
),
12691285
// Resolves `configRead` when the subscription reads the config, just before it claims the load.
12701286
connect: (config: object, configRead?: Deferred.Deferred<void>) =>
@@ -1374,6 +1390,16 @@ describe("environment shell cold start", () => {
13741390
loads: [{ ...FAMILY_FLAGS, resumeFrom: CACHED_SEQUENCE }],
13751391
subscribes: { afterSequence: HTTP_SEQUENCE, knownProjectIdentities: "identities-1" },
13761392
},
1393+
{
1394+
// The server answered that it can replay, then the rows passed their age limit.
1395+
label: "rows that expire before the socket is ready",
1396+
origin: { ...RECENT_ORIGIN, ...FAMILY_FLAGS, snapshotAt: NOW - 12 * HOUR + 1 },
1397+
serverResumes: true,
1398+
config: FAMILY_CONFIG,
1399+
loads: [{ ...FAMILY_FLAGS, resumeFrom: CACHED_SEQUENCE }],
1400+
subscribes: {},
1401+
expires: true,
1402+
},
13771403
{
13781404
// The rows loaded with the descriptor's omissions are replaced, never kept.
13791405
label: "a live config with other omissions",
@@ -1385,7 +1411,7 @@ describe("environment shell cold start", () => {
13851411
},
13861412
])(
13871413
"asks for the shell once the connection is prepared, with $label",
1388-
({ origin, serverResumes, config, loads, subscribes }) =>
1414+
({ origin, serverResumes, config, loads, subscribes, expires }) =>
13891415
Effect.gen(function* () {
13901416
const h = yield* makeColdStart(origin, { serverResumes });
13911417
expect(yield* Queue.take(h.loadOptions)).toEqual(loads[0]);
@@ -1406,14 +1432,21 @@ describe("environment shell cold start", () => {
14061432
}
14071433
expect(yield* Queue.size(h.subscriptions)).toBe(0);
14081434

1435+
if (expires) yield* TestClock.adjust("1 millis");
14091436
yield* h.connect(config);
14101437
const first = yield* Queue.take(h.subscriptions);
14111438
expect(first.input).toMatchObject(subscribes);
1412-
for (const key of ["httpSnapshotFallback", "knownProjectIdentities"] as const) {
1439+
for (const key of [
1440+
"afterSequence",
1441+
"httpSnapshotFallback",
1442+
"knownProjectIdentities",
1443+
] as const) {
14131444
if (!(key in subscribes)) expect(first.input).not.toHaveProperty(key);
14141445
}
14151446
expect(yield* Queue.clear(h.loadOptions)).toEqual(loads.slice(1));
14161447
expect(yield* Ref.get(h.httpLoads)).toBe(loads.length);
1448+
// The socket sends the full snapshot these rows asked for; the rest needs a cursor.
1449+
if (!("afterSequence" in subscribes)) return;
14171450

14181451
yield* Queue.offer(first.events, { kind: "synchronized" });
14191452
yield* h.liveAt(subscribes.afterSequence);
@@ -1441,7 +1474,10 @@ describe("environment shell cold start", () => {
14411474

14421475
it.effect("a replaced session waits for the load its connection already started", () =>
14431476
Effect.gen(function* () {
1444-
const h = yield* makeColdStart(undefined, { serverResumes: false, holdLoads: true });
1477+
const h = yield* makeColdStart(undefined, {
1478+
serverResumes: false,
1479+
holdLoads: "uninterruptible",
1480+
});
14451481
expect(yield* Queue.take(h.loadOptions)).toEqual(FAMILY_FLAGS);
14461482
const claimed = yield* Deferred.make<void>();
14471483
yield* h.connect(FAMILY_CONFIG, claimed);
@@ -1452,6 +1488,27 @@ describe("environment shell cold start", () => {
14521488
// the older answer would then replace its rows under a cursor the socket has passed.
14531489
yield* h.connect(FAMILY_CONFIG);
14541490
yield* TestClock.adjust("1 millis");
1491+
expect(yield* Queue.size(h.loadOptions)).toBe(0);
1492+
1493+
// The connection changes twice while the first load ends. The next session reads the
1494+
// last connection before its load exists, so it replaces the load of the one between.
1495+
yield* h.nextConnection;
1496+
yield* TestClock.adjust("1 millis");
1497+
yield* h.nextConnection;
1498+
yield* h.connect(FAMILY_CONFIG);
1499+
yield* TestClock.adjust("1 millis");
1500+
yield* h.releaseLoads;
1501+
expect(yield* Queue.take(h.loadOptions)).toEqual(FAMILY_FLAGS);
1502+
yield* TestClock.adjust("1 millis");
1503+
// That session is replaced while it waits for the load between to end. The load it then
1504+
// starts must stay the one load of the last connection.
1505+
yield* h.connect(FAMILY_CONFIG);
1506+
yield* TestClock.adjust("1 millis");
1507+
yield* h.releaseLoads;
1508+
expect(yield* Queue.take(h.loadOptions)).toEqual(FAMILY_FLAGS);
1509+
yield* TestClock.adjust("1 millis");
1510+
expect(yield* Queue.size(h.loadOptions)).toBe(0);
1511+
14551512
yield* h.releaseLoads;
14561513
const subscribed = yield* Queue.take(h.subscriptions);
14571514
expect(subscribed.input).toMatchObject({ afterSequence: HTTP_SEQUENCE });

‎packages/client-runtime/src/state/shell.ts‎

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -575,7 +575,11 @@ export const makeEnvironmentShellState = Effect.fn("EnvironmentShellState.make")
575575
const fiber = yield* loadOverHttp(prepared, identity).pipe(Effect.forkIn(stateScope));
576576
const next: EarlyShellLoad = { prepared, identity, fiber, claimed: claim };
577577
return [next, next] as const;
578-
}, Effect.uninterruptible),
578+
}),
579+
).pipe(
580+
// Covers the store too. A cancel that lands during the wait above would otherwise take
581+
// effect before the store, and the load just started would run on with no record.
582+
Effect.uninterruptible,
579583
);
580584
/**
581585
* The subscription waits for the early load, so its answer is applied before the socket
@@ -652,12 +656,12 @@ export const makeEnvironmentShellState = Effect.fn("EnvironmentShellState.make")
652656

653657
// Rows from a recent server snapshot with the same omissions and server build resume
654658
// from their cursor, in memory or from the cache. Anything else reloads the HTTP
655-
// snapshot so a valid cursor cannot preserve stale or incomplete data.
659+
// snapshot so a valid cursor cannot preserve stale or incomplete data. A `resumable`
660+
// answer is checked again too: the rows can pass their age limit while the socket opens.
656661
const resumingHeldRows =
657-
loaded?.kind === "resumable" ||
658-
(loaded?.kind !== "snapshot" &&
659-
Option.isSome((yield* SubscriptionRef.get(state)).snapshot) &&
660-
canResumeFrom(yield* Ref.get(origin), identity, yield* Clock.currentTimeMillis));
662+
loaded?.kind !== "snapshot" &&
663+
Option.isSome((yield* SubscriptionRef.get(state)).snapshot) &&
664+
canResumeFrom(yield* Ref.get(origin), identity, yield* Clock.currentTimeMillis);
661665
if (!resumingHeldRows && loaded === null) {
662666
const prepared = yield* SubscriptionRef.get(supervisor.prepared).pipe(
663667
Effect.flatMap(

0 commit comments

Comments
 (0)