Skip to content

Commit 18a83d8

Browse files
fix(preview): serialize host eviction with request delivery
1 parent e11b3db commit 18a83d8

2 files changed

Lines changed: 77 additions & 21 deletions

File tree

‎apps/server/src/mcp/PreviewAutomationBroker.test.ts‎

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1221,6 +1221,51 @@ it.effect("evicts an unanswered host and lets later calls use a healthy runtime"
12211221
),
12221222
);
12231223

1224+
it.effect("discards buffered actions before completing an evicted host stream", () =>
1225+
Effect.scoped(
1226+
Effect.gen(function* () {
1227+
const broker = yield* makeBroker;
1228+
const connected = yield* Deferred.make<void>();
1229+
const received = yield* Deferred.make<void>();
1230+
const actionRouted = yield* Deferred.make<void>();
1231+
const releaseConsumer = yield* Deferred.make<void>();
1232+
const operations: string[] = [];
1233+
const consumer = yield* Stream.runForEach(yield* broker.connect(makeHost()), (event) => {
1234+
if (event.type === "connected") return Deferred.succeed(connected, undefined);
1235+
operations.push(event.request.operation);
1236+
return Deferred.succeed(received, undefined).pipe(
1237+
Effect.andThen(Deferred.await(releaseConsumer)),
1238+
);
1239+
}).pipe(Effect.forkScoped);
1240+
yield* Deferred.await(connected);
1241+
const timedOut = yield* broker
1242+
.invoke<void>({ scope, operation: "snapshot", input: {}, timeoutMs: 1_000 })
1243+
.pipe(Effect.flip, Effect.forkScoped);
1244+
yield* Deferred.await(received);
1245+
const buffered = yield* broker
1246+
.invoke<void>({
1247+
scope,
1248+
operation: "click",
1249+
input: {},
1250+
timeoutMs: 10_000,
1251+
onTargetTab: () => {
1252+
Deferred.doneUnsafe(actionRouted, Effect.void);
1253+
},
1254+
})
1255+
.pipe(Effect.flip, Effect.forkScoped);
1256+
yield* Deferred.await(actionRouted);
1257+
yield* TestClock.adjust(1_000);
1258+
expect(yield* Fiber.join(timedOut)).toMatchObject({ _tag: "PreviewAutomationTimeoutError" });
1259+
expect(yield* Fiber.join(buffered)).toMatchObject({
1260+
_tag: "PreviewAutomationClientDisconnectedError",
1261+
});
1262+
yield* Deferred.succeed(releaseConsumer, undefined);
1263+
expect(Exit.isSuccess(yield* Fiber.await(consumer))).toBe(true);
1264+
expect(operations).toEqual(["snapshot"]);
1265+
}),
1266+
),
1267+
);
1268+
12241269
it.effect("keeps a host that responds with an operation timeout", () =>
12251270
Effect.scoped(
12261271
Effect.gen(function* () {

‎apps/server/src/mcp/PreviewAutomationBroker.ts‎

Lines changed: 32 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -329,12 +329,6 @@ export const make = Effect.gen(function* PreviewAutomationBrokerMake() {
329329
disconnected: ReadonlyArray<PendingRequest>,
330330
completeStream = false,
331331
) {
332-
yield* Effect.forEach(
333-
disconnected,
334-
({ deferred, context }) =>
335-
Deferred.fail(deferred, new PreviewAutomationClientDisconnectedError(context)),
336-
{ discard: true },
337-
);
338332
if (completeStream) {
339333
// Discard this generation's commands and complete the RPC stream so a
340334
// responsive desktop can re-register after a timeout eviction.
@@ -344,18 +338,25 @@ export const make = Effect.gen(function* PreviewAutomationBrokerMake() {
344338
// Replaced registrations must not reconnect and displace their successor.
345339
yield* Queue.shutdown(queue);
346340
}
341+
yield* Effect.forEach(
342+
disconnected,
343+
({ deferred, context }) =>
344+
Deferred.fail(deferred, new PreviewAutomationClientDisconnectedError(context)),
345+
{ discard: true },
346+
);
347347
});
348348

349349
const disconnect = Effect.fn("PreviewAutomationBroker.disconnect")(function* (
350350
clientId: string,
351351
queue: ClientConnection["queue"],
352352
completeStream = false,
353353
) {
354-
const disconnected = yield* SynchronizedRef.modify(state, (current) => {
354+
yield* SynchronizedRef.modifyEffect(state, (current) => {
355355
const removed = removeConnectionFromState(current, clientId, queue);
356-
return [removed.disconnected, removed.state] as const;
356+
return closeConnection(queue, removed.disconnected, completeStream).pipe(
357+
Effect.as([undefined, removed.state] as const),
358+
);
357359
});
358-
yield* closeConnection(queue, disconnected, completeStream);
359360
});
360361

361362
const acquireConnection = Effect.fn("PreviewAutomationBroker.acquireConnection")(function* (
@@ -564,18 +565,28 @@ export const make = Effect.gen(function* PreviewAutomationBrokerMake() {
564565
return { ...next, pending };
565566
});
566567
const awaitResponse = Effect.fn("PreviewAutomationBroker.awaitResponse")(function* () {
567-
const offered = yield* Queue.offer(connection.queue, {
568-
type: "request",
569-
connectionId: connection.connectionId,
570-
request: {
571-
requestId,
572-
threadId: input.scope.threadId,
573-
tabId: requestContext.tabId,
574-
tabIdExplicit: input.tabId !== undefined,
575-
operation: input.operation,
576-
input: input.input,
577-
timeoutMs,
578-
},
568+
const offered = yield* SynchronizedRef.modifyEffect(state, (current) => {
569+
// A route can outlive its generation while another request evicts it.
570+
// Serialize the live-generation check and offer with queue closure.
571+
if (
572+
current.clients.get(connection.clientId)?.queue !== connection.queue ||
573+
!current.pending.has(requestId)
574+
) {
575+
return Effect.succeed([false, current] as const);
576+
}
577+
return Queue.offer(connection.queue, {
578+
type: "request",
579+
connectionId: connection.connectionId,
580+
request: {
581+
requestId,
582+
threadId: input.scope.threadId,
583+
tabId: requestContext.tabId,
584+
tabIdExplicit: input.tabId !== undefined,
585+
operation: input.operation,
586+
input: input.input,
587+
timeoutMs,
588+
},
589+
}).pipe(Effect.map((offered) => [offered, current] as const));
579590
});
580591
if (!offered) {
581592
const completion = yield* Deferred.poll(deferred);

0 commit comments

Comments
 (0)