Skip to content

Commit de80de5

Browse files
t3dotggclaude
andcommitted
fix(server): release holds the request permit while it removes the session
The event pump persists runtime requests under the session's request permit. Release now removes the live entry and reads its cleanup cutoff under the same permit, so a request the pump is persisting lands before the cutoff and is settled. Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
1 parent 94d5f78 commit de80de5

2 files changed

Lines changed: 117 additions & 18 deletions

File tree

‎apps/server/src/orchestration-v2/ProviderSessionManager.test.ts‎

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,11 @@ interface FlakyReleaseWrites {
8686
readonly failing: Ref.Ref<"none" | "session" | "session-and-requests">;
8787
/** Receives one item per failed write. */
8888
readonly failures: Queue.Queue<void>;
89+
/** Holds runtime request writes: completes `paused`, then waits for `resume`. */
90+
readonly pauseRequestWrites?: {
91+
readonly paused: Deferred.Deferred<void>;
92+
readonly resume: Deferred.Deferred<void>;
93+
};
8994
}
9095

9196
// Fails release writes with a defect, the way a failed SQL commit surfaces.
@@ -106,6 +111,14 @@ const makeFlakyReleaseEventSinkLayer = (flaky: FlakyReleaseWrites) =>
106111
(event.payload.status === "stopped" || event.payload.status === "error")) ||
107112
(failing === "session-and-requests" && event.type === "runtime-request.updated"),
108113
);
114+
const pause = flaky.pauseRequestWrites;
115+
if (
116+
pause !== undefined &&
117+
input.events.some((event) => event.type === "runtime-request.updated")
118+
) {
119+
yield* Deferred.succeed(pause.paused, undefined);
120+
yield* Deferred.await(pause.resume);
121+
}
109122
if (!fails) return yield* delegate.write(input);
110123
yield* Queue.offer(flaky.failures, undefined);
111124
return yield* Effect.die(new Error("simulated commit failure"));
@@ -2564,6 +2577,72 @@ it.effect("ProviderSessionManagerV2 keeps each failed release's cleanup", () =>
25642577
}),
25652578
);
25662579

2580+
it.effect("ProviderSessionManagerV2 settles a request the event pump persists during release", () =>
2581+
Effect.gen(function* () {
2582+
const state = yield* Ref.make(emptyState);
2583+
const flaky: FlakyReleaseWrites = {
2584+
failing: yield* Ref.make<"none" | "session" | "session-and-requests">("none"),
2585+
failures: yield* Queue.unbounded<void>(),
2586+
pauseRequestWrites: {
2587+
paused: yield* Deferred.make<void>(),
2588+
resume: yield* Deferred.make<void>(),
2589+
},
2590+
};
2591+
const pause = flaky.pauseRequestWrites!;
2592+
const effect = Effect.gen(function* () {
2593+
const eventSink = yield* EventSink.EventSinkV2;
2594+
const idAllocator = yield* IdAllocator.IdAllocatorV2;
2595+
const manager = yield* ProviderSessionManager.ProviderSessionManagerV2;
2596+
const projectionStore = yield* ProjectionStore.ProjectionStoreV2;
2597+
const now = yield* DateTime.now;
2598+
const threadId = ThreadId.make("thread-provider-session-manager-release-pump");
2599+
const providerSessionId = yield* idAllocator.allocate.providerSession({
2600+
providerInstanceId: modelSelection.instanceId,
2601+
threadId,
2602+
});
2603+
yield* eventSink.write({
2604+
events: [yield* makeThreadCreatedEvent({ idAllocator, threadId, now })],
2605+
});
2606+
yield* manager.open({ threadId, providerSessionId, modelSelection, runtimePolicy });
2607+
// The runtime creates the request a moment after the release starts.
2608+
const createdAt = DateTime.add(now, { seconds: 1 });
2609+
const pendingRequest = yield* makePendingRuntimeRequestEvents({
2610+
idAllocator,
2611+
threadId,
2612+
providerSessionId,
2613+
providerThread: makeProviderThread({
2614+
idAllocator,
2615+
threadId,
2616+
providerSessionId,
2617+
now: createdAt,
2618+
}),
2619+
now: createdAt,
2620+
});
2621+
const adapterEvents = (yield* Ref.get(state)).eventQueues.get(String(providerSessionId));
2622+
assert.isDefined(adapterEvents);
2623+
yield* Queue.offerAll(adapterEvents!, pendingRequest.providerEvents);
2624+
// The event pump holds the request permit while it persists the request.
2625+
yield* Deferred.await(pause.paused);
2626+
const closed = yield* manager
2627+
.close(providerSessionId)
2628+
.pipe(Effect.forkScoped({ startImmediately: true }));
2629+
yield* TestClock.adjust("1 second");
2630+
yield* Deferred.succeed(pause.resume, undefined);
2631+
yield* Fiber.join(closed);
2632+
2633+
const projection = yield* projectionStore.getThreadProjection(threadId);
2634+
const request = projection.runtimeRequests.find(
2635+
(candidate) => candidate.id === pendingRequest.requestId,
2636+
);
2637+
assert.equal(request?.responseCapability.type, "not_resumable");
2638+
});
2639+
2640+
yield* effect.pipe(
2641+
Effect.provide(makeTestLayer({ state, idleTimeoutMs: 60_000, flakyReleaseWrites: flaky })),
2642+
);
2643+
}),
2644+
);
2645+
25672646
it.effect("ProviderSessionManagerV2 terminalizes a pending input transcript item on release", () =>
25682647
Effect.gen(function* () {
25692648
const state = yield* Ref.make(emptyState);

‎apps/server/src/orchestration-v2/ProviderSessionManager.ts‎

Lines changed: 38 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -793,38 +793,58 @@ export const layerWithOptions = (
793793
),
794794
);
795795

796-
const releaseEntry = (input: {
796+
// Removes the live entry and reads the request cleanup cutoff while
797+
// holding the entry's request permit. A request the event pump is
798+
// persisting for this runtime lands before the cutoff, and once the
799+
// entry is gone the pump persists no more for it. A replacement's
800+
// requests come after its own open.
801+
const removeLiveEntry = (input: {
797802
readonly providerSessionId: ProviderSessionId;
798-
readonly reason: ProviderSessionReleaseReason;
799-
readonly detail?: string;
800-
readonly cancelIdleFiber?: boolean;
801803
readonly onlyIfIdleGeneration?: number;
802-
readonly gracefulSubscribers?: boolean;
803-
}) =>
804-
Effect.acquireUseRelease(
805-
// Read the clock as the entry leaves the live map, before teardown.
806-
// The old runtime's requests were created before the event pump
807-
// checked this entry, and a replacement's requests come after its
808-
// open, so the cutoff separates them.
809-
Effect.zip(
804+
}): Effect.Effect<readonly [Option.Option<LiveSessionEntry>, DateTime.Utc]> =>
805+
Effect.gen(function* () {
806+
const key = sessionKey(input.providerSessionId);
807+
const candidate = (yield* Ref.get(sessions)).get(key);
808+
if (candidate === undefined) {
809+
return [Option.none<LiveSessionEntry>(), yield* DateTime.now] as const;
810+
}
811+
const removed = yield* Effect.zip(
810812
Ref.modify(sessions, (current) => {
811-
const key = sessionKey(input.providerSessionId);
812813
const existing = current.get(key);
813-
if (existing === undefined) {
814-
return [Option.none<LiveSessionEntry>(), current] as const;
814+
if (existing !== candidate) {
815+
return [existing === undefined ? "gone" : "changed", current] as const;
815816
}
816817
if (
817818
input.onlyIfIdleGeneration !== undefined &&
818819
(existing.busyCount > 0 || existing.idleGeneration !== input.onlyIfIdleGeneration)
819820
) {
820-
return [Option.none<LiveSessionEntry>(), current] as const;
821+
return ["kept", current] as const;
821822
}
822823
const updated = new Map(current);
823824
updated.delete(key);
824-
return [Option.some(existing), updated] as const;
825+
return ["removed", updated] as const;
825826
}),
826827
DateTime.now,
827-
),
828+
).pipe(candidate.requestEventPermit.withPermits(1));
829+
const [outcome, releasedAt] = removed;
830+
// Another entry took this id while the permit was held; release it instead.
831+
if (outcome === "changed") return yield* removeLiveEntry(input);
832+
return [
833+
outcome === "removed" ? Option.some(candidate) : Option.none<LiveSessionEntry>(),
834+
releasedAt,
835+
] as const;
836+
});
837+
838+
const releaseEntry = (input: {
839+
readonly providerSessionId: ProviderSessionId;
840+
readonly reason: ProviderSessionReleaseReason;
841+
readonly detail?: string;
842+
readonly cancelIdleFiber?: boolean;
843+
readonly onlyIfIdleGeneration?: number;
844+
readonly gracefulSubscribers?: boolean;
845+
}) =>
846+
Effect.acquireUseRelease(
847+
removeLiveEntry(input),
828848
([entry, releasedAt]) =>
829849
Option.match(entry, {
830850
onNone: () => Effect.void,

0 commit comments

Comments
 (0)