Skip to content

Commit d64cd39

Browse files
fix(server): a planned detach waits out a native turn that survived its interrupt
When the native interrupt fails, or the turns to interrupt cannot be read, the moving thread's subscription stays open instead of failing. The run keeps streaming until the native turn really ends, as on main, so its continuation never starts beside a live native turn and the run does not request a restart of a shared runtime. AI-assisted; verified with the tests and tools listed above. Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent 62fe0ee commit d64cd39

2 files changed

Lines changed: 25 additions & 23 deletions

File tree

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

Lines changed: 22 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1907,8 +1907,13 @@ it.effect.each([
19071907
: offer(queue, signal);
19081908
}) as typeof Queue.offer);
19091909
const received: Array<ProviderAdapter.ProviderAdapterV2Event> = [];
1910+
const arrivals = yield* Queue.unbounded<ProviderAdapter.ProviderAdapterV2Event>();
19101911
const reading = yield* subscription.events.pipe(
1911-
Stream.tap((event) => Effect.sync(() => received.push(event))),
1912+
Stream.tap((event) =>
1913+
Effect.sync(() => received.push(event)).pipe(
1914+
Effect.andThen(Queue.offer(arrivals, event)),
1915+
),
1916+
),
19121917
Stream.runDrain,
19131918
Effect.exit,
19141919
Effect.tap(() => Effect.sync(() => milestones.push("detached"))),
@@ -1951,26 +1956,26 @@ it.effect.each([
19511956
yield* TestClock.adjust("30 seconds");
19521957
}
19531958
yield* Fiber.join(detach);
1959+
if (failure === "interrupt" || failure === "projection") {
1960+
// An unconfirmed interrupt leaves the native turn streaming to its run.
1961+
const followUp = { ...event };
1962+
yield* Queue.offer(queue, followUp);
1963+
let arrived = yield* Queue.take(arrivals);
1964+
while (arrived !== followUp) arrived = yield* Queue.take(arrivals);
1965+
assert.isUndefined(reading.pollUnsafe());
1966+
assert.deepEqual(milestones, failure === "interrupt" ? ["interrupt"] : []);
1967+
return;
1968+
}
19541969
const exit = yield* Fiber.join(reading);
19551970
assert.isTrue(Exit.isFailure(exit));
1956-
if (failure === "interrupt" && Exit.isFailure(exit)) {
1957-
assert.instanceOf(
1958-
Cause.squash(exit.cause),
1959-
ProviderAdapter.ProviderAdapterInterruptError,
1960-
);
1961-
}
1962-
if (failure === "projection" && Exit.isFailure(exit)) {
1963-
assert.instanceOf(
1964-
Cause.squash(exit.cause),
1965-
ProviderAdapter.ProviderAdapterEventStreamError,
1966-
);
1967-
}
1968-
assert.deepEqual(received, status === "completed" ? [turnEvent, event] : [event]);
1971+
// Events published while the interrupt is pending still reach the moving run.
1972+
assert.deepEqual(
1973+
received,
1974+
status === "completed" ? [turnEvent, event] : sibling ? [event, event] : [event],
1975+
);
19691976
assert.deepEqual(
19701977
milestones,
1971-
status === "completed" || failure === "projection"
1972-
? ["detached"]
1973-
: ["interrupt", "detached"],
1978+
status === "completed" ? ["detached"] : ["interrupt", "detached"],
19741979
);
19751980
}).pipe(
19761981
Effect.provide(

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

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -637,6 +637,8 @@ export const layerWithOptions = (
637637
threadId?: ThreadId,
638638
beforeFailure: Effect.Effect<void, ProviderAdapter.ProviderAdapterV2Error> = Effect.void,
639639
) {
640+
// A native turn that survived its interrupt keeps streaming to its run until it ends.
641+
if (Exit.isFailure(yield* Effect.exit(beforeFailure))) return;
640642
const subscribers = yield* Ref.modify(entry.eventSubscribers, (current) => {
641643
const failed: Array<ProviderSessionEventSubscriber> = [];
642644
const remaining = new Map(current);
@@ -648,14 +650,9 @@ export const layerWithOptions = (
648650
}
649651
return [failed, remaining] as const;
650652
}).pipe(entry.requestEventPermit.withPermits(1));
651-
const interruption = yield* Effect.exit(beforeFailure);
652653
yield* Effect.forEach(
653654
subscribers,
654-
({ queue }) =>
655-
Queue.offer(queue, {
656-
type: "failure",
657-
cause: Exit.isFailure(interruption) ? interruption.cause : Cause.fail(error),
658-
}),
655+
({ queue }) => Queue.offer(queue, { type: "failure", cause: Cause.fail(error) }),
659656
{ discard: true },
660657
).pipe(entry.requestEventPermit.withPermits(1));
661658
});

0 commit comments

Comments
 (0)