fix(cli): studio thumbnails and Ctrl-C cleanup under a restricted page network (#7941)

* fix(cli): studio thumbnails and Ctrl-C cleanup under a restricted page network

Studio's thumbnail browser now gets the preview's server as its page server, so
with PRODUCER_RESTRICT_PAGE_NETWORK on the proxy no longer refuses the Studio's
own preview. A restricted CLI browser's proxy and temp profile are released by
the CLI's stop-signal handler, which waits for them before exiting.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* test(cli): every composition Chrome launch goes through the page-network fence

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* fix(cli): keep the follow-up inside the package's comment share

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

---------

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
James Russo
2026-11-16 10:52:44 +00:00
committed by GitHub
co-authored by Claude Opus 5.5
parent e8f1fff5fd
commit 707e20f185
6 changed files with 208 additions and 9 deletions
@@ -0,0 +1,26 @@
// fallow-ignore-file unused-file
import { EventEmitter } from "node:events";
import { writeFileSync } from "node:fs";
import type { Browser } from "puppeteer-core";
import { registerRootExitRequester } from "../utils/commandResult.js";
import { onStopSignal } from "./launch.js";
import { launchWithPageNetwork } from "./pageNetwork.js";
const [readyPath] = process.argv.slice(2);
if (!readyPath) throw new Error("Missing fixture arguments");
process.env.PRODUCER_RESTRICT_PAGE_NETWORK = "true";
registerRootExitRequester((exitCode) => process.exit(exitCode));
// A command's own Chrome launch installs the CLI's signal handling; this stands in for it.
onStopSignal(() => {});
const events = new EventEmitter();
const connectedUntilStopped = {
connected: true,
once: (event: string, listener: () => void) => events.once(event, listener),
} as unknown as Browser;
await launchWithPageNetwork("http://127.0.0.1:4000/", async (network) => {
writeFileSync(readyPath, JSON.stringify({ userDataDir: network.userDataDir }));
return connectedUntilStopped;
});
@@ -0,0 +1,37 @@
import { spawn } from "node:child_process";
import { existsSync, mkdtempSync, readFileSync, rmSync } from "node:fs";
import { tmpdir } from "node:os";
import { dirname, join } from "node:path";
import { fileURLToPath } from "node:url";
import { expect, it } from "vitest";
import { waitForTestCondition } from "../utils/processTestUtils.js";
const fixturePath = join(dirname(fileURLToPath(import.meta.url)), "pageNetwork.signal.fixture.ts");
// fails without the fix: the CLI's Ctrl-C handler exits without waiting for the profile's removal, so its
// hf-cli-profile- folder stays in the temp folder.
it.skipIf(process.platform === "win32")(
"removes a restricted browser's temp profile when the CLI stops on Ctrl-C",
async () => {
const testDir = mkdtempSync(join(tmpdir(), "hf-page-network-signal-"));
const readyPath = join(testDir, "ready.json");
const child = spawn(process.execPath, ["--import", "tsx", fixturePath, readyPath], {
stdio: "ignore",
});
try {
await waitForTestCondition(() => existsSync(readyPath), 20_000);
const { userDataDir } = JSON.parse(readFileSync(readyPath, "utf8")) as {
userDataDir: string;
};
expect(existsSync(userDataDir)).toBe(true);
const exited = new Promise<number | null>((resolve) => child.once("exit", resolve));
child.kill("SIGINT");
expect(await exited).toBe(130);
expect(existsSync(userDataDir)).toBe(false);
} finally {
child.kill("SIGKILL");
rmSync(testDir, { recursive: true, force: true });
}
},
30_000,
);
+10 -4
View File
@@ -1,4 +1,5 @@
import type { Browser } from "puppeteer-core";
import { registerBrowserSessionCleanup } from "./launch.js";
export interface PageNetworkLaunch {
args: string[];
@@ -20,11 +21,16 @@ export async function launchWithPageNetwork(
const { rm } = await import("node:fs/promises");
const proxy = await startCaptureProxy(null, logRefusal);
let userDataDir: string | undefined;
const release = async () => {
await proxy.close();
if (userDataDir) await rm(userDataDir, { recursive: true, force: true }).catch(() => {});
};
let released: Promise<void> | undefined;
let stopWatching = () => {};
const release = () =>
(released ??= (async () => {
stopWatching();
await proxy.close();
if (userDataDir) await rm(userDataDir, { recursive: true, force: true }).catch(() => {});
})());
try {
stopWatching = registerBrowserSessionCleanup(release);
userDataDir = await createPublicOnlyProfile("hf-cli-profile-");
const browser = await launch({
args: browserProxyArgs(proxy.url, new URL(pageUrl).host),
@@ -0,0 +1,91 @@
import { existsSync, readdirSync, readFileSync } from "node:fs";
import { join, relative, sep } from "node:path";
import { fileURLToPath } from "node:url";
import ts from "typescript";
import { describe, expect, it } from "vitest";
const SRC = fileURLToPath(new URL("..", import.meta.url));
// Chrome launches that never open a composition page, so PRODUCER_RESTRICT_PAGE_NETWORK's fence doesn't apply.
const OPENS_NO_COMPOSITION: Record<string, string> = {
"browser/launch.ts": "defines the launch, and its GPU probe opens a blank page",
"capture/launchCaptureBrowser.ts": "`capture` opens a website, not a composition",
};
function sources(dir: string): string[] {
return readdirSync(dir, { withFileTypes: true }).flatMap((entry) => {
const at = join(dir, entry.name);
if (entry.isDirectory()) return sources(at);
return /\.tsx?$/.test(entry.name) && !/\.(test|fixture|d)\.tsx?$/.test(entry.name) ? [at] : [];
});
}
const callee = (call: ts.CallExpression) => call.expression.getText();
/**
* The Chrome launches in `text` that skip the page-network fence: a `launchManagedBrowser` or puppeteer `.launch`
* not inside a `launchWithPageNetwork` call, or an engine `acquireBrowser` given no `pageServerUrl`.
*/
function unfencedLaunches(name: string, text: string): string[] {
const file = ts.createSourceFile(name, text, ts.ScriptTarget.Latest, true);
const found: string[] = [];
const at = (node: ts.Node) =>
`${name}:${file.getLineAndCharacterOfPosition(node.getStart()).line + 1}`;
const fenced = (node: ts.Node): boolean =>
!!node.parent &&
((ts.isCallExpression(node.parent) && callee(node.parent) === "launchWithPageNetwork") ||
fenced(node.parent));
const visit = (node: ts.Node) => {
if (ts.isCallExpression(node)) {
const name = callee(node);
if ((name === "launchManagedBrowser" || /puppeteer.*\.launch$/i.test(name)) && !fenced(node))
found.push(at(node));
if (
name === "acquireBrowser" &&
!node.arguments.some(
(arg) =>
ts.isObjectLiteralExpression(arg) &&
arg.properties.some((property) => property.name?.getText() === "pageServerUrl"),
)
)
found.push(at(node));
}
ts.forEachChild(node, visit);
};
visit(file);
return found;
}
describe("the page-network fence", () => {
it("is found missing on a bare launch, and found on a fenced one", () => {
expect(
unfencedLaunches(
"bare.ts",
`const a = await launchManagedBrowser(puppeteer.default, { args });
const b = await puppeteer.default.launch({ args });
const c = await acquireBrowser(args, { forceScreenshot: true });`,
),
).toEqual(["bare.ts:1", "bare.ts:2", "bare.ts:3"]);
expect(
unfencedLaunches(
"fenced.ts",
`const a = await launchWithPageNetwork(url, (network) =>
launchManagedBrowser(puppeteer.default, { args: [...args, ...network.args] }),
);
const c = await acquireBrowser(args, { forceScreenshot: true, pageServerUrl: url });`,
),
).toEqual([]);
});
// fails without the fix: layout, validate and the motion shot launched Chrome with launchManagedBrowser alone, and
// the Studio thumbnail browser called acquireBrowser with no page server; only the shared page-open path was tested.
it("covers every Chrome launch in the CLI that opens a composition", () => {
for (const file of Object.keys(OPENS_NO_COMPOSITION))
expect(existsSync(join(SRC, file))).toBe(true);
const unfenced = sources(SRC)
.map((file) => relative(SRC, file).split(sep).join("/"))
.filter((file) => !(file in OPENS_NO_COMPOSITION))
.flatMap((file) => unfencedLaunches(file, readFileSync(join(SRC, file), "utf8")));
expect(unfenced).toEqual([]);
});
});
@@ -747,6 +747,25 @@ describe("Studio thumbnail capture", () => {
expect(screenshot).toHaveBeenCalledTimes(1);
});
// fails without the fix: the thumbnail browser launched with no page server, so with
// PRODUCER_RESTRICT_PAGE_NETWORK on its proxy refused the Studio's own preview and every thumbnail failed.
it("gives the thumbnail browser the preview's server, and a new browser for a preview on another host", async () => {
fakePageBrowser();
const launch = vi.spyOn(engineState, "acquireBrowser");
const dir = tmpProject();
server = createStudioServer({ projectDir: dir });
const at = (previewUrl: string) => ({ ...opts(dir), previewUrl });
await server.adapter.generateThumbnail?.(at("http://127.0.0.1:4100/api/projects/demo/preview"));
await server.adapter.generateThumbnail?.(at("http://127.0.0.1:4100/api/projects/demo/preview"));
await server.adapter.generateThumbnail?.(at("http://localhost:4200/api/projects/demo/preview"));
expect(
launch.mock.calls.map((call) => (call[1] as { pageServerUrl?: string }).pageServerUrl),
).toEqual([
"http://127.0.0.1:4100/api/projects/demo/preview",
"http://localhost:4200/api/projects/demo/preview",
]);
});
it("undoes a row's isolation after its screenshot, since the page is reused", async () => {
const { screenshot, evaluate } = fakePageBrowser();
const dir = tmpProject();
+25 -5
View File
@@ -267,6 +267,7 @@ let _thumbnailBrowserModes: {
requested: BrowserGpuMode;
resolved: ResolvedBrowserGpuMode;
softwareWebGpu: boolean;
pageServerHost: string;
} | null = null;
interface ThumbnailBrowserSession {
@@ -274,27 +275,35 @@ interface ThumbnailBrowserSession {
requestedGpuMode: BrowserGpuMode;
resolvedGpuMode: ResolvedBrowserGpuMode;
softwareWebGpu: boolean;
pageServerHost: string;
}
// The browser gets the preview's server as its page server: with PRODUCER_RESTRICT_PAGE_NETWORK on, that is the
// one host it reaches directly, so a preview on another host needs a new browser.
async function getThumbnailBrowser(
requestedGpuMode: BrowserGpuMode,
previewUrl: string,
isShuttingDown: () => boolean,
): Promise<ThumbnailBrowserSession | null> {
if (isShuttingDown()) return null;
const pageServerHost = new URL(previewUrl).host;
if (
_thumbnailBrowserLease?.browser.connected &&
_thumbnailBrowserModes?.requested === requestedGpuMode
_thumbnailBrowserModes?.requested === requestedGpuMode &&
_thumbnailBrowserModes.pageServerHost === pageServerHost
) {
return {
browser: _thumbnailBrowserLease.browser,
requestedGpuMode: _thumbnailBrowserModes.requested,
resolvedGpuMode: _thumbnailBrowserModes.resolved,
softwareWebGpu: _thumbnailBrowserModes.softwareWebGpu,
pageServerHost,
};
}
if (_thumbnailBrowserInitializing) {
const session = await _thumbnailBrowserInitializing;
if (session?.requestedGpuMode === requestedGpuMode) return session;
if (session?.requestedGpuMode === requestedGpuMode && session.pageServerHost === pageServerHost)
return session;
}
if (_thumbnailBrowserLease) await closeThumbnailBrowser();
@@ -327,13 +336,14 @@ async function getThumbnailBrowser(
},
gpuConfig,
),
{ forceScreenshot: true },
{ forceScreenshot: true, pageServerUrl: previewUrl },
);
_thumbnailBrowserLease = acquired;
_thumbnailBrowserModes = {
requested: requestedGpuMode,
resolved: resolvedGpuMode,
softwareWebGpu,
pageServerHost,
};
acquired.browser.on("disconnected", () => {
if (_thumbnailBrowserLease !== acquired) return;
@@ -341,7 +351,13 @@ async function getThumbnailBrowser(
_thumbnailBrowserModes = null;
_thumbnailBrowserInitializing = null;
});
return { browser: acquired.browser, requestedGpuMode, resolvedGpuMode, softwareWebGpu };
return {
browser: acquired.browser,
requestedGpuMode,
resolvedGpuMode,
softwareWebGpu,
pageServerHost,
};
} catch (err) {
console.warn(
"[Studio] Failed to launch thumbnail browser:",
@@ -791,7 +807,11 @@ export function createStudioServer(options: StudioServerOptions): StudioServer {
},
async generateThumbnail(opts): Promise<Buffer | null> {
const session = await getThumbnailBrowser(browserGpuMode, () => shuttingDown);
const session = await getThumbnailBrowser(
browserGpuMode,
opts.previewUrl,
() => shuttingDown,
);
if (!session) {
if (!shuttingDown) {
console.warn("[Studio] Thumbnail: no browser available — Chrome may not be installed");