Skip to content

Commit 14ea5c4

Browse files
t3dotggclaude
andcommitted
fix(client): a ready socket waits at most 3 seconds for the early shell answer
A client with rows that can resume waited for the HTTP answer before it subscribed. A proxy that drops /api without an answer held each connect for the 20 second HTTP limit. After 3 seconds the request is now interrupted and the socket resumes the held rows, as on main. The interrupt waits for an answer that is being applied, so a late answer cannot replace rows the socket has moved on. Co-Authored-By: Claude Fable 5.2 <[email protected]>
1 parent f52a48a commit 14ea5c4

3 files changed

Lines changed: 72 additions & 2 deletions

File tree

‎docs/internals/connection-runtime.md‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,11 @@ the server answers `resumable` instead of a snapshot when the socket can replay
8484
from them, so a cold start costs one shell request plus the subscription. The
8585
live config stays the authority: rows loaded for a build or with omissions it
8686
does not confirm are replaced, never resumed. A server whose descriptor does not
87-
name the flags is asked only after its config arrives.
87+
name the flags is asked only after its config arrives. The subscription waits
88+
for that answer, so a snapshot never lands on rows the socket has moved on. When
89+
held rows can resume, the wait ends 3 seconds after the socket is ready: the
90+
request is interrupted and the socket resumes the rows, so a proxy that drops
91+
`/api` without an answer does not hold the connect for the HTTP timeout.
8892

8993
Shell snapshots can leave rows out when the client asks. Servers that advertise
9094
`shellThreadFamilies` leave out idle subagent children: a child stays while it

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

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1460,6 +1460,46 @@ describe("environment shell cold start", () => {
14601460
}),
14611461
);
14621462

1463+
it.effect("resumes held rows on the socket when the HTTP answer does not come", () =>
1464+
Effect.gen(function* () {
1465+
const h = yield* makeColdStart(
1466+
{ ...RECENT_ORIGIN, ...FAMILY_FLAGS },
1467+
{ serverResumes: false, holdLoads: true },
1468+
);
1469+
yield* Queue.take(h.loadOptions);
1470+
const claimed = yield* Deferred.make<void>();
1471+
yield* h.connect(FAMILY_CONFIG, claimed);
1472+
yield* Deferred.await(claimed);
1473+
// Lets the subscription run on from its config read to its wait for the load.
1474+
yield* TestClock.adjust("1 millis");
1475+
expect(yield* Queue.size(h.subscriptions)).toBe(0);
1476+
1477+
yield* TestClock.adjust("3 seconds");
1478+
const first = yield* Queue.take(h.subscriptions);
1479+
expect(first.input).toMatchObject({
1480+
afterSequence: CACHED_SEQUENCE,
1481+
httpSnapshotFallback: true,
1482+
});
1483+
yield* Queue.offer(first.events, {
1484+
kind: "thread.removed",
1485+
sequence: HTTP_SEQUENCE + 1,
1486+
location: "active",
1487+
threadId: ThreadId.make("no-such-thread"),
1488+
});
1489+
yield* Queue.offer(first.events, { kind: "synchronized" });
1490+
yield* h.liveAt(HTTP_SEQUENCE + 1);
1491+
1492+
// The late answer is an older snapshot. It must not replace the rows the socket moved on.
1493+
yield* h.releaseLoads;
1494+
yield* TestClock.adjust("1 millis");
1495+
expect(yield* Ref.get(h.httpLoads)).toBe(0);
1496+
expect(yield* SubscriptionRef.get(h.shellState)).toMatchObject({
1497+
status: "live",
1498+
snapshot: Option.some({ snapshotSequence: HTTP_SEQUENCE + 1 }),
1499+
});
1500+
}),
1501+
);
1502+
14631503
it.effect("drops the load of a connection that ended and waits for the next one", () =>
14641504
Effect.gen(function* () {
14651505
const h = yield* makeColdStart(undefined, { serverResumes: false, holdLoads: true });

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

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,11 @@ const SHELL_SYNCHRONIZATION_ERROR_MESSAGE = "Could not synchronize environment d
9292
*/
9393
const CACHE_RESUME_MAX_AGE_MILLIS = 12 * 60 * 60 * 1000;
9494

95+
// How long a ready socket waits for the early HTTP answer before it resumes held rows itself.
96+
// The supervisor's quick probe allows a live host the same time. A proxy that drops `/api`
97+
// without an answer would otherwise hold each connect for the full HTTP timeout.
98+
const EARLY_LOAD_GRACE = "3 seconds";
99+
95100
type ShellOriginIdentity = Omit<Persistence.ShellCacheOrigin, "snapshotAt">;
96101

97102
interface ShellSubscription {
@@ -572,6 +577,27 @@ export const makeEnvironmentShellState = Effect.fn("EnvironmentShellState.make")
572577
return [next, next] as const;
573578
}, Effect.uninterruptible),
574579
);
580+
/**
581+
* The subscription waits for the early load, so its answer is applied before the socket
582+
* moves the rows on. Held rows that can resume wait a limited time only: after that the
583+
* load is ended, so a late answer applies nothing, and the socket resumes them.
584+
*/
585+
const awaitEarlyLoad = Effect.fnUntraced(function* (
586+
early: EarlyShellLoad,
587+
identity: ShellOriginIdentity | null,
588+
) {
589+
const resumable =
590+
Option.isSome((yield* SubscriptionRef.get(state)).snapshot) &&
591+
canResumeFrom(yield* Ref.get(origin), identity, yield* Clock.currentTimeMillis);
592+
if (!resumable) return yield* Fiber.await(early.fiber);
593+
const answered = yield* Fiber.await(early.fiber).pipe(Effect.timeoutOption(EARLY_LOAD_GRACE));
594+
if (Option.isSome(answered)) return answered.value;
595+
// Returns after an answer that is being applied has finished, so the cursor below is read
596+
// from rows that no longer change under it.
597+
yield* Fiber.interrupt(early.fiber);
598+
return yield* Fiber.await(early.fiber);
599+
});
600+
575601
yield* SubscriptionRef.changes(supervisor.prepared).pipe(
576602
// Reads the connection of this moment: a change handled late must not start a load for a
577603
// connection that ended, which would also drop the items of the session that replaced it.
@@ -609,7 +635,7 @@ export const makeEnvironmentShellState = Effect.fn("EnvironmentShellState.make")
609635
Option.getOrNull(yield* SubscriptionRef.get(supervisor.prepared)),
610636
true,
611637
);
612-
const earlyExit = early === null ? null : yield* Fiber.await(early.fiber);
638+
const earlyExit = early === null ? null : yield* awaitEarlyLoad(early, identity);
613639
// Rows the descriptor's flags or build loaded are not kept when the live config differs:
614640
// their origin then cannot resume, so the load below replaces them.
615641
let loaded =

0 commit comments

Comments
 (0)