mirror of
https://github.com/heygen-com/hyperframes.git
synced 2026-10-30 09:16:55 +00:00
fix(cli): orphaned Chrome helpers no longer keep a finished snapshot from exiting (#6545)
* fix(cli): a browser's orphaned helpers no longer keep a finished snapshot from exiting The CDP pipe sockets stayed open after Chrome exited whenever a helper process outlived it, so the CLI never reached natural exit. Destroy them when the browser process exits. * refactor(cli): name the pipe close so launch.ts carries no new comment * fix(cli): a browser stand-in without stdio no longer throws from the exit listener * fix(cli): close every pipe of an exited browser, not only the CDP pair
This commit is contained in:
@@ -0,0 +1,24 @@
|
||||
import { spawn } from "node:child_process";
|
||||
import type { LaunchOptions, PuppeteerNode } from "puppeteer-core";
|
||||
import { expect, it } from "vitest";
|
||||
import { launchManagedBrowser } from "./launch.js";
|
||||
|
||||
// A helper that outlives the browser inherits its pipes, as Chrome's crashpad handler does.
|
||||
const SPAWN_ORPHAN = `
|
||||
require("node:child_process")
|
||||
.spawn(process.execPath, ["-e", "setTimeout(() => {}, 4000)"], { detached: true, stdio: ["ignore", "inherit", "inherit", 3, 4] })
|
||||
.unref();
|
||||
`;
|
||||
|
||||
it("closes the browser's pipes when it exits, so an orphaned helper cannot hold the CLI open", async () => {
|
||||
const child = spawn(process.execPath, ["-e", SPAWN_ORPHAN], {
|
||||
stdio: ["pipe", "pipe", "pipe", "pipe", "pipe"],
|
||||
});
|
||||
const puppeteer = {
|
||||
launch: async (_options: LaunchOptions) => ({ process: () => child }),
|
||||
} as unknown as PuppeteerNode;
|
||||
await launchManagedBrowser(puppeteer, { headless: true }, { diagnose: false });
|
||||
await expect
|
||||
.poll(() => child.stdio.every((pipe) => pipe?.destroyed), { timeout: 3000 })
|
||||
.toBe(true);
|
||||
});
|
||||
@@ -1,3 +1,4 @@
|
||||
import type { ChildProcess } from "node:child_process";
|
||||
import { explainChromeLaunchFailure } from "@hyperframes/engine/chrome-startup";
|
||||
import type { Browser, LaunchOptions, PuppeteerNode } from "puppeteer-core";
|
||||
import { withHyperframesBrowserMarker } from "@hyperframes/core/browser-marker";
|
||||
@@ -151,6 +152,10 @@ function withRemediation(error: unknown): unknown {
|
||||
return remediation ? new Error(remediation, { cause: error }) : error;
|
||||
}
|
||||
|
||||
function destroyBrowserPipes(child: ChildProcess): void {
|
||||
for (const pipe of child.stdio ?? []) pipe?.destroy();
|
||||
}
|
||||
|
||||
async function launchOwnedBrowser(
|
||||
puppeteer: PuppeteerNode,
|
||||
options: LaunchOptions,
|
||||
@@ -188,7 +193,10 @@ async function launchOwnedBrowser(
|
||||
browsers.add(browser);
|
||||
const watchdog = watchChromeTree(child, launchedAt);
|
||||
if (watchdog) watchdogs.set(browser, watchdog);
|
||||
child.once("exit", () => browsers.delete(browser));
|
||||
child.once("exit", () => {
|
||||
browsers.delete(browser);
|
||||
destroyBrowserPipes(child);
|
||||
});
|
||||
return browser;
|
||||
} finally {
|
||||
clearTimeout(timer);
|
||||
|
||||
Reference in New Issue
Block a user