UNPKG

histoire

Version:

A fast story book powered by Vite

71 lines (70 loc) • 2.87 kB
/** * Browser handles already force-killed, so calling the fallback twice for the * same run (or for a browser shared by several projects) stays a no-op. */ const killedBrowsers = new WeakSet(); /** * Reaches the OS process backing a Playwright browser handle. * * Playwright's public client API exposes no process handle, but the `playwright` * package runs its server side in the same Node process, so the client * connection can map a client object back to its server implementation * (`toImpl`) — and that implementation owns the spawned browser process. * Undefined for remote browsers (`connect`), where no local process exists. * @param browser The Playwright client `Browser` instance. */ export function getPlaywrightBrowserProcess(browser) { try { const impl = browser?._connection?.toImpl?.(browser); const child = impl?.options?.browserProcess?.process; return typeof child?.pid === 'number' ? child : undefined; } catch { // Internals are version-specific: an unexpected shape means "not reachable", // never a cleanup failure. return undefined; } } /** * Force-kills the browser process behind a Playwright browser handle. * * Last resort for a browser whose graceful `close()` is stuck: without it a * long-lived dev server would strand one headless browser per timed-out run. * Never throws and is idempotent per browser handle. * @param browser The Playwright client `Browser` instance. * @param capturedProcess Owned process captured before graceful close removed it. * @returns Whether a process was actually signalled. */ export function forceKillPlaywrightBrowser(browser, capturedProcess) { if (!browser || typeof browser !== 'object' || killedBrowsers.has(browser)) { return false; } const child = capturedProcess ?? getPlaywrightBrowserProcess(browser); // ChildProcess.killed confirms only that some signal was sent. A graceful // signal can leave a hung process alive, which still needs this SIGKILL. if (!child?.pid) { return false; } // Mark before signalling: a failed kill must not be retried on every // subsequent cleanup either. killedBrowsers.add(browser); try { if (process.platform === 'win32') { // No process groups on Windows; Chromium's job object takes the renderer // children down with the browser process. return child.kill?.('SIGKILL') ?? false; } // Playwright spawns the browser detached, so it leads its own process // group: killing the group also reaps Chromium's renderer children. process.kill(-child.pid, 'SIGKILL'); return true; } catch { try { return child.kill?.('SIGKILL') ?? false; } catch { return false; } } }