Skip to content

An agent skill for creating detailed, interactive isometric illustrations of real-world machines and complex systems. Generates polished SVGs with optional WebGL shaders and 3D effects.

fix(browser-checks): clean up Chrome startup failures

Rudy Celeklicb4d6962fa47
diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml
index bf7b18d..12652e5 100644
--- a/.github/workflows/ci.yml
+++ b/.github/workflows/ci.yml
@@ -25,6 +25,9 @@ jobs:
with:
node-version: 22
+ - name: Chrome startup regressions
+ run: node --test scripts/chrome-startup.test.mjs
+
- name: Dial indicator
run: |
node examples/dial-indicator/build.mjs
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 8152ae2..678f464 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -12,6 +12,8 @@ All notable changes to Anatomy are recorded here. The format follows [Keep a Cha
### Fixed
+- Browser checks now fail promptly and remove their temporary profiles when Chrome exits before its debugging port is ready.
+
- `examples/test-rig/` drew its glass button dark in the light theme. `stretch.css` now has light values for its own colours.
### Changed
diff --git a/scripts/capture.mjs b/scripts/capture.mjs
index 755bb0a..b0b75ca 100644
--- a/scripts/capture.mjs
+++ b/scripts/capture.mjs
@@ -1,3 +1,4 @@
+import { chromePort } from "./chrome-port.mjs";
import { spawn } from "node:child_process";
import { existsSync, mkdtempSync, readdirSync, rmSync, writeFileSync } from "node:fs";
import { homedir, tmpdir } from "node:os";
@@ -110,18 +111,7 @@ async function main() {
"--ignore-gpu-blocklist",
"about:blank",
]);
- const port = await new Promise((done, fail) => {
- let text = "";
- const timer = setTimeout(() => fail(new Error("Chrome did not start")), 20000);
- chrome.stderr.on("data", (chunk) => {
- text += chunk;
- const match = text.match(/DevTools listening on ws:\/\/[^:]+:(\d+)\//);
- if (match) {
- clearTimeout(timer);
- done(Number(match[1]));
- }
- });
- });
+ const port = await chromePort(chrome, profile);
const finish = (code) => {
chrome.kill("SIGKILL");
try {
diff --git a/scripts/chrome-port.mjs b/scripts/chrome-port.mjs
new file mode 100644
index 0000000..cb8e224
--- /dev/null
+++ b/scripts/chrome-port.mjs
@@ -0,0 +1,36 @@
+import { rmSync } from "node:fs";
+
+export async function chromePort(chrome, profile) {
+ try {
+ return await new Promise((done, fail) => {
+ let text = "";
+ const clean = () => {
+ clearTimeout(timer);
+ chrome.stderr.off("data", data);
+ chrome.off("error", error);
+ chrome.off("exit", exited);
+ };
+ const error = (cause) => {
+ clean();
+ fail(cause);
+ };
+ const exited = (code, signal) => error(new Error(`Chrome exited before startup: ${code ?? signal}`));
+ const data = (chunk) => {
+ text += chunk;
+ const match = text.match(/DevTools listening on ws:\/\/[^:]+:(\d+)\//);
+ if (match) {
+ clean();
+ done(Number(match[1]));
+ }
+ };
+ const timer = setTimeout(() => error(new Error("Chrome did not start")), 20000);
+ chrome.stderr.on("data", data);
+ chrome.on("error", error);
+ chrome.on("exit", exited);
+ });
+ } catch (error) {
+ chrome.kill("SIGKILL");
+ rmSync(profile, { recursive: true, force: true });
+ throw error;
+ }
+}
diff --git a/scripts/chrome-startup.test.mjs b/scripts/chrome-startup.test.mjs
new file mode 100644
index 0000000..ef9cb5a
--- /dev/null
+++ b/scripts/chrome-startup.test.mjs
@@ -0,0 +1,38 @@
+import assert from "node:assert/strict";
+import { execFile } from "node:child_process";
+import { chmod, mkdtemp, readFile, rm, writeFile, access } from "node:fs/promises";
+import { tmpdir } from "node:os";
+import { join } from "node:path";
+import { promisify } from "node:util";
+import { fileURLToPath } from "node:url";
+import test from "node:test";
+
+const execute = promisify(execFile);
+
+for (const name of ["capture", "drive", "turn-check"]) {
+ test(`${name} removes its profile when Chrome exits before startup`, async () => {
+ const directory = await mkdtemp(join(tmpdir(), "anatomy-chrome-startup-"));
+ let profile;
+ try {
+ const binary = join(directory, "chrome");
+ const receipt = join(directory, "profile.txt");
+ await writeFile(binary, '#!/bin/sh\nfor arg in "$@"; do\n case "$arg" in --user-data-dir=*) printf "%s" "${arg#--user-data-dir=}" > "$ANATOMY_PROFILE_RECEIPT";; esac\ndone\nexit 7\n');
+ await chmod(binary, 0o755);
+ const started = performance.now();
+ let result;
+ try {
+ await execute(process.execPath, [fileURLToPath(new URL(`${name}.mjs`, import.meta.url)), join(directory, "figure.html"), ...(name === "drive" ? ["[]"] : name === "turn-check" ? ["--lines"] : [])], { timeout: 30000, env: { ...process.env, CHROME_PATH: binary, ANATOMY_PROFILE_RECEIPT: receipt } });
+ } catch (error) {
+ result = error;
+ }
+ profile = await readFile(receipt, "utf8");
+ assert.equal(result?.code, 1);
+ await assert.rejects(access(profile), { code: "ENOENT" });
+ assert.ok(performance.now() - started < 10000);
+ assert.match(result.stderr, /Chrome exited before startup.*7/);
+ } finally {
+ if (profile) await rm(profile, { recursive: true, force: true });
+ await rm(directory, { recursive: true, force: true });
+ }
+ });
+}
diff --git a/scripts/drive.mjs b/scripts/drive.mjs
index ef2c90b..122ff48 100644
--- a/scripts/drive.mjs
+++ b/scripts/drive.mjs
@@ -1,3 +1,4 @@
+import { chromePort } from "./chrome-port.mjs";
import { spawn } from "node:child_process";
import { existsSync, mkdirSync, mkdtempSync, readdirSync, readFileSync, rmSync, writeFileSync } from "node:fs";
import { homedir, tmpdir } from "node:os";
@@ -110,18 +111,7 @@ async function main() {
}
const profile = mkdtempSync(join(tmpdir(), "iso-drive-"));
const chrome = spawn(binary, ["--headless=new", "--remote-debugging-port=0", `--user-data-dir=${profile}`, "--no-first-run", "--no-default-browser-check", "--hide-scrollbars", "--allow-file-access-from-files", "--enable-unsafe-swiftshader", "--ignore-gpu-blocklist", "about:blank"]);
- const port = await new Promise((done, fail) => {
- let text = "";
- const timer = setTimeout(() => fail(new Error("Chrome did not start")), 20000);
- chrome.stderr.on("data", (chunk) => {
- text += chunk;
- const match = text.match(/DevTools listening on ws:\/\/[^:]+:(\d+)\//);
- if (match) {
- clearTimeout(timer);
- done(Number(match[1]));
- }
- });
- });
+ const port = await chromePort(chrome, profile);
const finish = (code) => {
chrome.kill("SIGKILL");
try {
diff --git a/scripts/turn-check.mjs b/scripts/turn-check.mjs
index 6dc3bdc..7df41d4 100644
--- a/scripts/turn-check.mjs
+++ b/scripts/turn-check.mjs
@@ -1,3 +1,4 @@
+import { chromePort } from "./chrome-port.mjs";
import { spawn } from "node:child_process";
import { existsSync, mkdirSync, mkdtempSync, readdirSync, readFileSync, rmSync, writeFileSync } from "node:fs";
import { homedir, tmpdir } from "node:os";
@@ -36,18 +37,7 @@ function chromePath() {
async function browser(target, { width, height, scale, throttle, noWebgl }) {
const profile = mkdtempSync(join(tmpdir(), "turn-check-"));
const chrome = spawn(chromePath(), ["--headless=new", "--remote-debugging-port=0", `--user-data-dir=${profile}`, "--no-first-run", "--no-default-browser-check", "--hide-scrollbars", "--allow-file-access-from-files", "--enable-unsafe-swiftshader", "--ignore-gpu-blocklist", "about:blank"]);
- const port = await new Promise((done, fail) => {
- let text = "";
- const timer = setTimeout(() => fail(new Error("Chrome did not start")), 20000);
- chrome.stderr.on("data", (chunk) => {
- text += chunk;
- const match = text.match(/DevTools listening on ws:\/\/[^:]+:(\d+)\//);
- if (match) {
- clearTimeout(timer);
- done(Number(match[1]));
- }
- });
- });
+ const port = await chromePort(chrome, profile);
const page = await (await fetch(`http://127.0.0.1:${port}/json/new?about:blank`, { method: "PUT" })).json();
const socket = new WebSocket(page.webSocketDebuggerUrl);
let id = 0;