Skip to content

Commit ace4fd8

Browse files
fix(server): bound the desktop viewport read in preview status
A busy desktop renderer could hold the status viewport measurement for its full 1 s timeout (longer when a measurement was already queued), using up the broker's 1 s grace after a navigate that spent its own budget. Status now waits at most 250 ms for each renderer read and falls back to the last measured viewport; the measurement still finishes in the background and refreshes it. Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent 6d1ca80 commit ace4fd8

2 files changed

Lines changed: 50 additions & 12 deletions

File tree

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

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -754,6 +754,40 @@ it.effect.each([
754754
).pipe(Effect.ensuring(Effect.sync(() => vi.useRealTimers())), Effect.provide(layer)),
755755
);
756756

757+
it.effect("a desktop status answers with the last viewport while the renderer cannot measure", () =>
758+
Effect.scoped(
759+
Effect.gen(function* () {
760+
const { broker } = yield* ready;
761+
desktopRendersNext = true;
762+
const opened = yield* broker.invoke<PreviewAutomationStatus>({
763+
scope,
764+
operation: "open",
765+
input: { url: "https://example.com/", reuseExistingTab: false, show: false },
766+
});
767+
const measuring = Promise.withResolvers<void>();
768+
desktopSizeAnswer = () => {
769+
measuring.resolve();
770+
return new Promise(() => {});
771+
};
772+
773+
vi.useFakeTimers({ toFake: ["setTimeout", "clearTimeout"] });
774+
const result = yield* broker
775+
.invoke<PreviewAutomationStatus>({
776+
scope,
777+
tabId: opened.tabId!,
778+
operation: "status",
779+
input: {},
780+
})
781+
.pipe(Effect.forkScoped);
782+
yield* Effect.promise(() => measuring.promise);
783+
yield* Effect.promise(() => vi.advanceTimersByTimeAsync(250));
784+
const status = yield* Fiber.join(result);
785+
786+
expect(status.viewport).toEqual({ width: 1920, height: 1080 });
787+
}),
788+
).pipe(Effect.ensuring(Effect.sync(() => vi.useRealTimers())), Effect.provide(layer)),
789+
);
790+
757791
it.live("navigate fails when the page never commits within its timeout", () =>
758792
Effect.scoped(
759793
Effect.gen(function* () {

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

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -156,8 +156,8 @@ const EDITABLE_AT_POINT_SCRIPT = `(x, y) => {
156156
}`;
157157
const UNATTACHED_FILL_VIEWPORT = { width: 1280, height: 800 } as const;
158158
const NAVIGATION_TIMEOUT_MS = 15_000;
159-
// page.title() runs in the renderer; a busy page must not hold a status past the broker's grace.
160-
const TITLE_READ_MS = 250;
159+
// Status reads run in the renderer; a busy page must not hold a status past the broker's grace.
160+
const STATUS_READ_MS = 250;
161161
const VIEWER_NAVIGATION_OPTIONS = { waitUntil: "commit", timeout: NAVIGATION_TIMEOUT_MS } as const;
162162
const ACTION_TIMELINE_LIMIT = 50;
163163

@@ -1790,15 +1790,12 @@ const make = Effect.gen(function* () {
17901790
}
17911791
};
17921792

1793-
const readTitle = async (tab: ServerTab) => {
1793+
const withinStatusDeadline = <A>(read: Promise<A>, late: () => A): Promise<A> => {
17941794
let timer: ReturnType<typeof setTimeout> | undefined;
1795-
const late = new Promise<string>((resolve) => {
1796-
timer = setTimeout(() => resolve(""), TITLE_READ_MS);
1795+
const deadline = new Promise<A>((resolve) => {
1796+
timer = setTimeout(() => resolve(late()), STATUS_READ_MS);
17971797
});
1798-
const title = await Promise.race([tab.page.title().catch(() => ""), late]).finally(() =>
1799-
clearTimeout(timer),
1800-
);
1801-
return title || null;
1798+
return Promise.race([read, deadline]).finally(() => clearTimeout(timer));
18021799
};
18031800

18041801
const statusWithTitle = async (
@@ -1819,10 +1816,17 @@ const make = Effect.gen(function* () {
18191816
const url = ServerBrowserPage.withoutDataPayload(tab.page.url());
18201817
const loadError = loadErrorOf(tab);
18211818
const statusUrl = loadError?.url ?? (url === "about:blank" ? null : url);
1822-
// Both reads need the renderer; overlapping them keeps a busy page within the broker's grace.
18231819
const [viewport, title] = await Promise.all([
1824-
tab.desktop ? layoutSize(tab) : tab.page.viewportSize(),
1825-
statusUrl === null || tab.dialog ? null : readTitle(tab),
1820+
// A late measurement still lands in desktopViewport for the next status.
1821+
tab.desktop
1822+
? withinStatusDeadline(layoutSize(tab), () => tab.desktopViewport)
1823+
: tab.page.viewportSize(),
1824+
statusUrl === null || tab.dialog
1825+
? null
1826+
: withinStatusDeadline(
1827+
tab.page.title().catch(() => ""),
1828+
() => "",
1829+
).then((title) => title || null),
18261830
]);
18271831
return {
18281832
available: true,

0 commit comments

Comments
 (0)