Skip to content

Write HTML. Render video. Built for agents.

fix(engine): read Chrome process info from the browser target, not the page

Vance Ingalls2cf8024a7c25
diff --git a/packages/engine/src/services/frameCapture-chromeMemory.test.ts b/packages/engine/src/services/frameCapture-chromeMemory.test.ts
index b564231188..e8e33fecd0 100644
--- a/packages/engine/src/services/frameCapture-chromeMemory.test.ts
+++ b/packages/engine/src/services/frameCapture-chromeMemory.test.ts
@@ -1,5 +1,5 @@
import { describe, expect, it } from "vitest";
-import { classifyChromeProcesses } from "./frameCapture.js";
+import { classifyChromeProcesses, readChromePids } from "./frameCapture.js";
describe("classifyChromeProcesses", () => {
it("splits CDP SystemInfo.getProcessInfo rows by type and keeps the browser pid", () => {
@@ -36,3 +36,55 @@ describe("classifyChromeProcesses", () => {
).toEqual({ browser: 1, renderers: [], gpu: [] });
});
});
+
+describe("readChromePids", () => {
+ // Chrome answers SystemInfo.getProcessInfo ONLY on the browser target; a
+ // page-target session rejects with "is only supported on the browser
+ // target". A page session here yields zero samples for the whole render and
+ // the telemetry is silently always-null, so the fake refuses it the way
+ // Chrome does.
+ function fakeCdp(target: "page" | "browser") {
+ let detached = false;
+ return {
+ session: {
+ send: async (method: "SystemInfo.getProcessInfo") => {
+ if (target === "page") {
+ throw new Error(
+ `Protocol error (${method}): ${method} is only supported on the browser target`,
+ );
+ }
+ return {
+ processInfo: [
+ { type: "browser", id: 10, cpuTime: 0 },
+ { type: "renderer", id: 11, cpuTime: 0 },
+ { type: "GPU", id: 12, cpuTime: 0 },
+ ],
+ };
+ },
+ detach: async () => {
+ detached = true;
+ },
+ },
+ wasDetached: () => detached,
+ };
+ }
+
+ it("reads process info from the browser target", async () => {
+ const cdp = fakeCdp("browser");
+ await expect(readChromePids(10, async () => cdp.session)).resolves.toEqual({
+ browser: 10,
+ renderers: [11],
+ gpu: [12],
+ });
+ expect(cdp.wasDetached()).toBe(true);
+ });
+
+ it("rejects when handed a page-target session, instead of silently sampling nothing", async () => {
+ const cdp = fakeCdp("page");
+ await expect(readChromePids(10, async () => cdp.session)).rejects.toThrow(
+ "only supported on the browser target",
+ );
+ // Still detached: a failed probe must not leak a CDP session per tick.
+ expect(cdp.wasDetached()).toBe(true);
+ });
+});
diff --git a/packages/engine/src/services/frameCapture.ts b/packages/engine/src/services/frameCapture.ts
index 03678cca8a..ff8ee709d7 100644
--- a/packages/engine/src/services/frameCapture.ts
+++ b/packages/engine/src/services/frameCapture.ts
@@ -3890,6 +3890,33 @@ export function classifyChromeProcesses(
return browserPid === undefined ? { renderers, gpu } : { browser: browserPid, renderers, gpu };
}
+/** The slice of a CDP session `readChromePids` needs. */
+interface ProcessInfoCdpSession {
+ send(method: "SystemInfo.getProcessInfo"): Promise<{
+ processInfo: readonly CdpProcessInfoRow[];
+ }>;
+ detach(): Promise<void>;
+}
+
+/**
+ * Chrome pids for one browser. `SystemInfo.getProcessInfo` is served ONLY by
+ * the browser target — a page-target session rejects with "is only supported
+ * on the browser target", which the sampler would swallow into a permanently
+ * empty reading. Hence the explicit browser-session factory.
+ */
+export async function readChromePids(
+ browserPid: number | undefined,
+ createBrowserCdpSession: () => Promise<ProcessInfoCdpSession>,
+): Promise<ChromePids> {
+ const cdp = await createBrowserCdpSession();
+ try {
+ const info = await cdp.send("SystemInfo.getProcessInfo");
+ return classifyChromeProcesses(browserPid, info.processInfo);
+ } finally {
+ await cdp.detach().catch(() => {});
+ }
+}
+
const CHROME_MEMORY_SAMPLE_MS_DEFAULT = 2_000;
function resolveChromeMemorySampleMs(): number {
@@ -3905,16 +3932,10 @@ function startChromeMemorySampler(session: CaptureSession): void {
intervalMs: resolveChromeMemorySampleMs(),
onSample: session.options.onMemorySample,
sampleRss: (pids) => sampleProcessRss(pids),
- getPids: async () => {
- const browserPid = session.browser.process()?.pid;
- const cdp = await session.page.createCDPSession();
- try {
- const info = await cdp.send("SystemInfo.getProcessInfo");
- return classifyChromeProcesses(browserPid, info.processInfo);
- } finally {
- await cdp.detach().catch(() => {});
- }
- },
+ getPids: () =>
+ readChromePids(session.browser.process()?.pid, () =>
+ session.browser.target().createCDPSession(),
+ ),
});
session.chromeMemory = sampler;
sampler.start();