Skip to content

Commit e040999

Browse files
fix(server): expire queued browser work at the request timeout
The run's abort signal now fires at the request's own timeoutMs on the Effect clock, while the server still answers at timeoutMs plus its margin. Queued work whose request expired never starts, as before, and a started run past its timeout still marks the tab busy. Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent 3da8321 commit e040999

2 files changed

Lines changed: 59 additions & 12 deletions

File tree

‎apps/server/src/preview/ServerBrowser.test.ts‎

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2589,3 +2589,39 @@ it.effect("a recording stop that never finishes leaves its tab busy", () =>
25892589
}),
25902590
).pipe(Effect.provide(layer)),
25912591
);
2592+
2593+
it.effect("a queued action whose timeout passes while it waits never runs", () =>
2594+
Effect.scoped(
2595+
Effect.gen(function* () {
2596+
const { broker, tabId } = yield* ready;
2597+
const page = contexts[0]!.page;
2598+
const started = Promise.withResolvers<void>();
2599+
const finish = Promise.withResolvers<void>();
2600+
page.emulateMedia.mockImplementationOnce(() => {
2601+
started.resolve();
2602+
return finish.promise;
2603+
});
2604+
const slow = yield* broker
2605+
.invoke<void>({
2606+
scope,
2607+
tabId,
2608+
operation: "setColorScheme",
2609+
input: { colorScheme: "dark" },
2610+
timeoutMs: 3_000,
2611+
})
2612+
.pipe(Effect.forkScoped);
2613+
yield* Effect.promise(() => started.promise);
2614+
const queued = yield* broker
2615+
.invoke<void>({ scope, tabId, operation: "click", input: { ref: "e1" }, timeoutMs: 1_000 })
2616+
.pipe(Effect.flip, Effect.forkScoped);
2617+
// The host answers in arrival order, so the click is queued once this answers.
2618+
yield* broker.invoke({ scope, tabId, operation: "status", input: {} });
2619+
// Past the click's timeout but before the server answers it.
2620+
yield* TestClock.adjust("1200 millis");
2621+
finish.resolve();
2622+
yield* Fiber.join(slow);
2623+
expect(yield* Fiber.join(queued)).toMatchObject({ _tag: "PreviewAutomationTimeoutError" });
2624+
expect(page.locator).not.toHaveBeenCalled();
2625+
}),
2626+
).pipe(Effect.provide(layer)),
2627+
);

‎apps/server/src/preview/ServerBrowser.ts‎

Lines changed: 23 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2306,9 +2306,19 @@ const make = Effect.gen(function* () {
23062306
}
23072307
};
23082308

2309-
const handleRequest = (connectionId: string, request: PreviewAutomationRequest) =>
2310-
Effect.tryPromise({
2311-
try: (signal) => runOperation(request, signal).finally(() => markUsed(request)),
2309+
const handleRequest = Effect.fn("ServerBrowser.handleRequest")(function* (
2310+
connectionId: string,
2311+
request: PreviewAutomationRequest,
2312+
) {
2313+
// Queued work expires at the request's timeout; a started run past it marks its tab busy.
2314+
const expiry = yield* Scope.make();
2315+
const signal = yield* Effect.abortSignal.pipe(Scope.provide(expiry));
2316+
yield* Effect.sleep(request.timeoutMs).pipe(
2317+
Effect.andThen(Scope.close(expiry, Exit.void)),
2318+
Effect.forkChild,
2319+
);
2320+
const outcome = yield* Effect.tryPromise({
2321+
try: () => runOperation(request, signal).finally(() => markUsed(request)),
23122322
catch: ServerBrowserPage.toOperationError,
23132323
}).pipe(
23142324
// Lets the operation report its own timeout first, yet answers before the broker's
@@ -2324,6 +2334,7 @@ const make = Effect.gen(function* () {
23242334
}),
23252335
),
23262336
}),
2337+
Effect.ensuring(Scope.close(expiry, Exit.void)),
23272338
Effect.match({
23282339
onSuccess: (result) => ({ ok: true as const, result }),
23292340
onFailure: (error) => ({
@@ -2335,16 +2346,16 @@ const make = Effect.gen(function* () {
23352346
},
23362347
}),
23372348
}),
2338-
Effect.flatMap((outcome) =>
2339-
broker.respond({
2340-
clientId: SERVER_HOST_CLIENT_ID,
2341-
connectionId,
2342-
requestId: request.requestId,
2343-
...outcome,
2344-
}),
2345-
),
2346-
Effect.ignore,
23472349
);
2350+
yield* broker
2351+
.respond({
2352+
clientId: SERVER_HOST_CLIENT_ID,
2353+
connectionId,
2354+
requestId: request.requestId,
2355+
...outcome,
2356+
})
2357+
.pipe(Effect.ignore);
2358+
});
23482359

23492360
const mirrorManagerEvent = (event: PreviewEvent) =>
23502361
Effect.promise(async () => {

0 commit comments

Comments
 (0)