Skip to content

An open source design system that's fully customizable and agent ready

TypeScript

[fix] [experimental] render: a Node crash fails with a message, and Avatar pages render on Node builds that crash in Intl.Segmenter (#10915)

Joeydfd75a6ab327
diff --git a/.changeset/render-survives-node-crash.md b/.changeset/render-survives-node-crash.md
new file mode 100644
index 0000000000..583892ce5d
--- /dev/null
+++ b/.changeset/render-survives-node-crash.md
@@ -0,0 +1,7 @@
+---
+'@astryxdesign/cli': patch
+---
+
+[fix] [experimental] `astryx experimental render` no longer dies silently with exit 139 when Node itself crashes while it renders a page. The page now renders in a separate process, so a crash (a crash signal, or a crash status on Windows) ends only that process, and the render fails with `ERR_RENDER_FAILED` and a message that names the Node version and path; a signal that stops it names them too. That process ends when the CLI ends, so a page that never finishes does not outlive a stopped CLI. Some Node builds crash in `Intl.Segmenter`, which components such as Avatar use to split text: on such a build the render runs the page once more without `Intl.Segmenter` and adds a `node-crash` warning that names the Node build and says the output may differ slightly (a joined emoji, a flag, or a letter with a separate accent mark in initials), so pages with Avatar render again.
+
+@josephfarina
diff --git a/packages/cli/api-report.md b/packages/cli/api-report.md
index 4cd38603f5..892f2a3a62 100644
--- a/packages/cli/api-report.md
+++ b/packages/cli/api-report.md
@@ -1015,7 +1015,7 @@ type RenderReverseResponse
data: { input: string; mode: "embedded" | "rebuilt"; output: string; files: string[]; exact: boolean; missing?: string[] | undefined; components?: string[] | undefine … #2d35150e
type: "render.reverse"
type RenderWarning
- code: "dropped-children" | "remote-asset" | "missing-asset" | "page-output" | "page-script" | "no-astryx-styles" | "chart-size" | "not-embedded"
+ code: "dropped-children" | "remote-asset" | "missing-asset" | "page-output" | "page-script" | "no-astryx-styles" | "chart-size" | "not-embedded" | "node-crash"
message: string
```
diff --git a/packages/cli/api/render/render.doc.mjs b/packages/cli/api/render/render.doc.mjs
index a872cf0e0f..445668ce31 100644
--- a/packages/cli/api/render/render.doc.mjs
+++ b/packages/cli/api/render/render.doc.mjs
@@ -96,7 +96,7 @@ export const doc = {
{code: 'ERR_RENDER_BROWSER_API', when: 'the page reads a browser-only API, such as window or document, while rendering'},
{code: 'ERR_RENDER_ASSET_TOO_LARGE', when: 'a local file the page uses is over 8 MB'},
{code: 'ERR_RENDER_TIMEOUT', when: 'the page does not finish rendering within 60 seconds'},
- {code: 'ERR_RENDER_FAILED', when: 'the page throws while rendering for any other reason'},
+ {code: 'ERR_RENDER_FAILED', when: 'the page throws while rendering for any other reason, or Node itself crashes (a crash signal, or a crash status on Windows) or is stopped by a signal while it renders the page (the message names the Node version and path)'},
{
code: 'ERR_RENDER_NOT_ASTRYX',
when: 'reversing a file a render did not write, with no embedded source and no Astryx markup; the suggestion is `astryx build "<what the page is>"`',
diff --git a/packages/cli/api/render/render.test.mjs b/packages/cli/api/render/render.test.mjs
index 8e5b6a6102..1972df884c 100644
--- a/packages/cli/api/render/render.test.mjs
+++ b/packages/cli/api/render/render.test.mjs
@@ -5,7 +5,7 @@
* self-contained HTML file and back, each refusal, and the warnings.
*/
-import {spawnSync} from 'node:child_process';
+import {spawn, spawnSync} from 'node:child_process';
import * as fs from 'node:fs';
import {createRequire} from 'node:module';
import * as os from 'node:os';
@@ -13,6 +13,8 @@ import * as path from 'node:path';
import {fileURLToPath} from 'node:url';
import {afterAll, beforeAll, describe, expect, it} from 'vitest';
import {render} from './render.mjs';
+import {template} from '../template/template.mjs';
+import {segmenterWarning} from '../../foundation/render/render-page.mjs';
import {runCli} from '../../test-utils/run-cli.mjs';
import {makeLinkedFixture, removeLinkedFixture} from '../../test-utils/linked-fixture.mjs';
@@ -1435,6 +1437,153 @@ describe('render(): refusals', () => {
}, 30_000);
});
+describe('render(): a crash of Node itself', () => {
+ // A signal that Node raises when it fails ends only the page's process.
+ it.skipIf(process.platform === 'win32')('fails with a message that names this Node', async () => {
+ put('crash.tsx', "process.kill(process.pid, 'SIGSEGV');\nexport default () => <p>Hi</p>;\n");
+ /** @type {any} */
+ let error;
+ try {
+ await render('crash.tsx', {cwd: dir});
+ } catch (e) {
+ error = e;
+ }
+ expect(error?.code).toBe('ERR_RENDER_FAILED');
+ expect(error.message).toContain(`Node ${process.version}`);
+ expect(error.message).toContain('crashed (SIGSEGV)');
+ expect(fs.existsSync(path.join(dir, 'crash.html'))).toBe(false);
+ }, 60_000);
+
+ // A page that loops forever must not outlive a CLI that is stopped by its
+ // process id, as a caller's own time limit stops it.
+ it.skipIf(process.platform === 'win32').each(['SIGTERM', 'SIGKILL'])('ends the page process when the CLI is stopped with %s', async signal => {
+ const name = `loop-${signal}`;
+ put(
+ `${name}.tsx`,
+ `import {writeFileSync} from 'node:fs';\nwriteFileSync(${JSON.stringify(path.join(dir, `${name}.pid`))}, String(process.pid));\nwhile (true) {}\nexport default () => <p>Hi</p>;\n`,
+ );
+ const cli = spawn(process.execPath, [path.join(CLI_DIR, 'clients/cli/bin/astryx.mjs'), 'experimental', 'render', `${name}.tsx`], {cwd: dir, stdio: 'ignore'});
+ const pidFile = path.join(dir, `${name}.pid`);
+ const until = async (/** @type {() => boolean} */ done, /** @type {number} */ ms) => {
+ for (const start = Date.now(); !done() && Date.now() - start < ms; ) await new Promise(r => setTimeout(r, 50));
+ return done();
+ };
+ expect(await until(() => fs.existsSync(pidFile) && fs.readFileSync(pidFile, 'utf8') !== '', 30_000)).toBe(true);
+ const pagePid = Number(fs.readFileSync(pidFile, 'utf8'));
+ expect(pagePid).not.toBe(cli.pid);
+ const alive = () => {
+ try {
+ process.kill(pagePid, 0);
+ return true;
+ } catch {
+ return false;
+ }
+ };
+ cli.kill(/** @type {NodeJS.Signals} */ (signal));
+ const ended = await until(() => !alive(), 10_000);
+ if (!ended) process.kill(pagePid, 'SIGKILL');
+ expect(ended).toBe(true);
+ }, 60_000);
+});
+
+describe('render(): Avatar initials without Intl.Segmenter', () => {
+ // A render on a Node build that crashes in Intl.Segmenter removes it before
+ // the page loads. A local module imported first does the same here.
+ const NAMES = [
+ ['Ada Lovelace', 'AL', 'AL'],
+ ['José Ñúñez', 'JÑ', 'JÑ'],
+ ['Zoë Kravitz', 'ZK', 'ZK'],
+ ['Åsa Öberg', 'ÅÖ', 'ÅÖ'],
+ ['李小龙', '李', '李'],
+ ['김민준', '김', '김'],
+ // The accent is a separate mark after E.
+ ['E\u0301mile Zola', 'E\u0301Z', 'EZ'],
+ ['👩‍💻 Dev Team', '👩‍💻T', '👩T'],
+ ['🇯🇵 Tanaka', '🇯🇵T', '🇯T'],
+ ];
+ const body = `import {Avatar} from '@astryxdesign/core/Avatar';
+const NAMES = ${JSON.stringify(NAMES.map(([name]) => name))};
+export default function Page() {
+ return <main>{NAMES.map((name, i) => <div key={i} data-name={i}><Avatar name={name} /></div>)}</main>;
+}
+`;
+
+ /** @param {string} name */
+ async function initialsOf(name) {
+ await render(`initials/${name}.tsx`, {cwd: dir});
+ const html = fs.readFileSync(path.join(dir, 'initials', `${name}.html`), 'utf8');
+ return [...html.matchAll(/<div data-name="\d+">([\s\S]*?)(?=<div data-name=|<\/main>)/g)].map(m => m[1].replace(/<[^>]+>/g, ''));
+ }
+
+ // On a Node build that crashes in Intl.Segmenter, both renders run without it.
+ const segmenterCrashes =
+ spawnSync(process.execPath, ['-e', "[...new Intl.Segmenter('en').segment('a b')]"], {stdio: 'ignore'}).signal !== null;
+
+ it.skipIf(segmenterCrashes)('match for ordinary names and differ only for joined emoji, flags and separate accent marks', async () => {
+ put('initials/with.tsx', body);
+ put('initials/no-segmenter.ts', 'delete (Intl as any).Segmenter;\nexport {};\n');
+ put('initials/without.tsx', `import './no-segmenter';\n${body}`);
+ expect(await initialsOf('with')).toEqual(NAMES.map(([, withSegmenter]) => withSegmenter));
+ expect(await initialsOf('without')).toEqual(NAMES.map(([, , withoutSegmenter]) => withoutSegmenter));
+ }, 60_000);
+
+ it('names the Node build in the warning and says the output may differ slightly', () => {
+ const warning = segmenterWarning();
+ expect(warning.code).toBe('node-crash');
+ expect(warning.message).toContain(`${process.version} at ${process.execPath}`);
+ expect(warning.message).toContain('may differ slightly');
+ });
+});
+
+describe('render(): every ready page template', () => {
+ /** @type {string} */
+ let home;
+
+ beforeAll(() => {
+ home = makeLinkedFixture('astryx-render-templates-', ['@astryxdesign', '@stylexjs', '@heroicons', 'lucide-react', 'react', 'react-dom', 'recharts']);
+ });
+
+ afterAll(() => {
+ removeLinkedFixture(home);
+ });
+
+ it('scaffolds and renders each one', async () => {
+ const listed = /** @type {any} */ (await template(undefined, {list: true, type: 'page', cwd: home}));
+ /** @type {string[]} */
+ const ids = listed.data.filter((/** @type {any} */ t) => t.isReady).map((/** @type {any} */ t) => t.id);
+ expect(ids.length).toBeGreaterThan(0);
+ /** @type {Set<string>} */
+ const themes = new Set();
+ for (const id of ids) {
+ await template(id, {targetPath: `templates/${id}`, type: 'page', cwd: home});
+ const source = fs.readFileSync(path.join(home, 'templates', id, 'page.tsx'), 'utf8');
+ for (const m of source.matchAll(/from '(@astryxdesign\/theme-[a-z0-9-]+)[/']/g)) themes.add(m[1]);
+ }
+ // The node tests build Core only. A template that imports a theme's build
+ // needs that build.
+ for (const name of themes) {
+ const themeDir = fs.realpathSync(path.join(home, '..', 'node_modules', name));
+ if (fs.existsSync(path.join(themeDir, 'dist'))) continue;
+ const built = spawnSync('pnpm', ['run', 'build'], {cwd: themeDir, encoding: 'utf8', shell: process.platform === 'win32'});
+ expect(built.status, `${name} build: ${built.stderr}`).toBe(0);
+ }
+ /** @type {string[]} */
+ const failed = [];
+ for (let i = 0; i < ids.length; i += 4) {
+ await Promise.all(
+ ids.slice(i, i + 4).map(async id => {
+ try {
+ await render(`templates/${id}/page.tsx`, {cwd: home});
+ } catch (e) {
+ failed.push(`${id}: ${/** @type {any} */ (e).code} ${/** @type {any} */ (e).message}`);
+ }
+ }),
+ );
+ }
+ expect(failed).toEqual([]);
+ }, 900_000);
+});
+
describe('render(): a page outside a project', () => {
/** @type {string} */
let away;
diff --git a/packages/cli/api/render/render.type.mjs b/packages/cli/api/render/render.type.mjs
index d0bb912c41..7aed3b643e 100644
--- a/packages/cli/api/render/render.type.mjs
+++ b/packages/cli/api/render/render.type.mjs
@@ -22,7 +22,7 @@
* author meant.
* @experimental May change or be removed in a patch release.
* @typedef {object} RenderWarning
- * @property {'dropped-children' | 'remote-asset' | 'missing-asset' | 'page-output' | 'page-script' | 'no-astryx-styles' | 'chart-size' | 'not-embedded'} code - What kind of problem.
+ * @property {'dropped-children' | 'remote-asset' | 'missing-asset' | 'page-output' | 'page-script' | 'no-astryx-styles' | 'chart-size' | 'not-embedded' | 'node-crash'} code - What kind of problem.
* @property {string} message - What happened and what to do, in one sentence.
*/
diff --git a/packages/cli/assets/docs/tree/render-pages.doc.mjs b/packages/cli/assets/docs/tree/render-pages.doc.mjs
index c602e3859d..b68b678c27 100644
--- a/packages/cli/assets/docs/tree/render-pages.doc.mjs
+++ b/packages/cli/assets/docs/tree/render-pages.doc.mjs
@@ -67,7 +67,7 @@ export const docs = {
content: [
{
type: 'prose',
- text: 'Each warning names something that may look different from what you meant, and what to change. The most common is a component that ignores its children: `<ListItem>Save</ListItem>` renders an empty row because ListItem takes `label="Save"`. Others flag remote images (a sandboxed viewer cannot load them; save the image next to the page and import it), a `<script>`, `<link>` or `<iframe>` the page itself renders (the file promises to run and load nothing, so remove them), missing images, and output the page printed while rendering.',
+ text: 'Each warning names something that may look different from what you meant, and what to change. The most common is a component that ignores its children: `<ListItem>Save</ListItem>` renders an empty row because ListItem takes `label="Save"`. Others flag remote images (a sandboxed viewer cannot load them; save the image next to the page and import it), a `<script>`, `<link>` or `<iframe>` the page itself renders (the file promises to run and load nothing, so remove them), missing images, and output the page printed while rendering. A `node-crash` warning names a Node build that crashes in `Intl.Segmenter` (its version and path). The page rendered without it, so the output may differ slightly: initials and character counts (such as Avatar initials) can split a joined emoji, a flag, or a letter written with a separate accent mark. Names in ordinary composed letters (`José`, `Zoë`, `李小龙`) come out the same. Render with an official Node.js build to remove the difference.',
},
],
},
diff --git a/packages/cli/clients/cli/commands/experimental-render.doc.mjs b/packages/cli/clients/cli/commands/experimental-render.doc.mjs
index 86db5dc734..1e6ab1a9b8 100644
--- a/packages/cli/clients/cli/commands/experimental-render.doc.mjs
+++ b/packages/cli/clients/cli/commands/experimental-render.doc.mjs
@@ -53,7 +53,7 @@ export const doc = {
{code: 0, when: 'the file was written (warnings included)'},
{
code: 1,
- when: 'no file, a wrong extension, a missing file, a page that does not compile, has no default export, imports a package that is not installed, reads a browser API while rendering, throws, or takes over 60 seconds; a file to write outside the current folder; with --reverse, an existing output without --overwrite, a damaged embedded source, or HTML that is not an Astryx page',
+ when: 'no file, a wrong extension, a missing file, a page that does not compile, has no default export, imports a package that is not installed, reads a browser API while rendering, throws, crashes Node, or takes over 60 seconds; a file to write outside the current folder; with --reverse, an existing output without --overwrite, a damaged embedded source, or HTML that is not an Astryx page',
},
],
related: ['template', 'build'],
diff --git a/packages/cli/foundation/doc-compiler/doc-loads.test.mjs b/packages/cli/foundation/doc-compiler/doc-loads.test.mjs
index cad3d857cd..a1e20e862a 100644
--- a/packages/cli/foundation/doc-compiler/doc-loads.test.mjs
+++ b/packages/cli/foundation/doc-compiler/doc-loads.test.mjs
@@ -357,7 +357,7 @@ const RUNNERS = {
sites: ['importDocModule', 'importNativeModule ×3', 'importTemplateModule'],
},
'foundation/render/page-worker.mjs': {
- runs: 'the page `astryx experimental render` renders, its local modules, React, and the StyleX compiler, in a worker, then React DOM and the recharts settings to draw its charts; outside a project, Node resolves the packages its folder lacks, and React and StyleX for every module (resolve hooks for ES modules), from the package set the CLI runs from',
+ runs: 'the page `astryx experimental render` renders, its local modules, React, and the StyleX compiler, in a worker thread of the render host process, then React DOM and the recharts settings to draw its charts; outside a project, Node resolves the packages its folder lacks, and React and StyleX for every module (resolve hooks for ES modules), from the package set the CLI runs from',
sites: [
'.import() ×6',
'createJiti ×2',
@@ -367,7 +367,11 @@ const RUNNERS = {
],
},
'foundation/render/render-page.mjs': {
- runs: 'the render worker, so a page that never finishes can be stopped',
+ runs: 'the render host process, so a page that never finishes can be stopped and a crash of Node ends only that process, and a one-line Intl.Segmenter check after a crash',
+ sites: ['child_process.fork', 'child_process.spawnSync'],
+ },
+ 'foundation/render/page-host.mjs': {
+ runs: 'the render worker, in a thread of the host process, so the host can exit with the CLI or at its own time limit while a page loops',
sites: ['worker_threads.Worker'],
},
'foundation/fs/import-url.mjs': {
diff --git a/packages/cli/foundation/render/page-host.mjs b/packages/cli/foundation/render/page-host.mjs
new file mode 100644
index 0000000000..3a85579f9e
--- /dev/null
+++ b/packages/cli/foundation/render/page-host.mjs
@@ -0,0 +1,45 @@
+// Copyright (c) Meta Platforms, Inc. and affiliates.
+
+/**
+ * @file Host process for `astryx experimental render`: runs page-worker.mjs
+ * in a worker thread and sends its one answer to the CLI.
+ *
+ * A crash of Node itself ends only this process, never the CLI. The page runs
+ * in a thread, so this process can still act while the page loops: it exits
+ * when the CLI goes away (the IPC channel closes) or when its own time limit
+ * passes, so a page that never finishes never outlives the CLI.
+ *
+ * @input argv[2]: JSON {entry, root, served, withoutSegmenter, timeoutMs}
+ * @output one IPC message: the worker's answer, or {hostError: message}, or
+ * {hostExit: code}
+ * @position foundation/render — started by render-page.mjs
+ */
+
+import {Worker} from 'node:worker_threads';
+
+/** @type {{entry: string, root: string, served: {dir: string, integrations: string[]} | null, withoutSegmenter: boolean, timeoutMs: number}} */
+const input = JSON.parse(process.argv[2] ?? '{}');
+
+/** @param {unknown} message */
+function answer(message) {
+ if (!process.connected) process.exit(0);
+ process.send?.(message, () => process.exit(0));
+}
+
+process.once('disconnect', () => process.exit(0));
+// A margin past the CLI's own limit, so the CLI reports the timeout first.
+setTimeout(() => process.exit(0), input.timeoutMs + 5000);
+
+const worker = new Worker(new URL('./page-worker.mjs', import.meta.url), {
+ workerData: input,
+ execArgv: [],
+ stdout: true,
+ stderr: true,
+ resourceLimits: {maxOldGenerationSizeMb: 4096},
+});
+// The page's own writes to stdout/stderr never reach the CLI's output.
+worker.stdout.resume();
+worker.stderr.resume();
+worker.once('message', answer);
+worker.once('error', (/** @type {any} */ error) => answer({hostError: String(error?.message ?? error)}));
+worker.once('exit', code => answer({hostExit: code}));
diff --git a/packages/cli/foundation/render/page-worker.mjs b/packages/cli/foundation/render/page-worker.mjs
index 64be98c0b6..a2d304777d 100644
--- a/packages/cli/foundation/render/page-worker.mjs
+++ b/packages/cli/foundation/render/page-worker.mjs
@@ -5,8 +5,10 @@
* the project (TypeScript, JSX and StyleX compiled in memory) and renders its
* default export to static markup with the project's own React.
*
- * It runs in a worker thread so a page that never finishes rendering can be
- * stopped, and so nothing the page prints reaches the CLI's own output.
+ * It runs in a worker thread of a small host process (page-host.mjs), so a
+ * page that never finishes rendering can be stopped, nothing the page prints
+ * reaches the CLI's own output, and a crash of Node itself ends only the host,
+ * never the CLI.
*
* Outside a project, the Astryx packages the page's folder lacks resolve
* from the package set the CLI runs from (served-packages.mjs), and React and
@@ -16,10 +18,10 @@
* so the server render sets them aside and draws them after it with React
* DOM in a small document (charts.mjs).
*
- * @input workerData {entry, root, served}
+ * @input workerData {entry, root, served, withoutSegmenter}
* @output one message: {ok: true, markup, css, files, texts, logs} or
* {ok: false, error: {code, message, detail?}}
- * @position foundation/render — started by render-page.mjs
+ * @position foundation/render — started by page-host.mjs
*/
import * as fs from 'node:fs';
@@ -38,9 +40,13 @@ import {CHART_GLOBAL, CHART_ROOTS, chartDocument, chartSize, markLegends} from '
/** @typedef {{level: string, text: string}} PageLog */
-/** @type {{entry: string, root: string, served: {dir: string, integrations: string[]} | null}} */
+/** @type {{entry: string, root: string, served: {dir: string, integrations: string[]} | null, withoutSegmenter?: boolean}} */
const {entry, root} = workerData;
+// Some Node builds crash in Intl.Segmenter. Without it, Core splits text by
+// code points, so a joined emoji can count as several characters.
+if (workerData.withoutSegmenter) delete (/** @type {any} */ (Intl)).Segmenter;
+
/** The package set, for a page outside a project whose folder has no Core of its own. */
const served = workerData.served && findNodeModulesPackage(path.dirname(entry), '@astryxdesign/core') === null ? workerData.served : null;
const ROOT_PREFIX = root.endsWith(path.sep) ? root : root + path.sep;
diff --git a/packages/cli/foundation/render/render-page.mjs b/packages/cli/foundation/render/render-page.mjs
index 18156d128a..a264d743dd 100644
--- a/packages/cli/foundation/render/render-page.mjs
+++ b/packages/cli/foundation/render/render-page.mjs
@@ -18,7 +18,8 @@
import * as fs from 'node:fs';
import * as path from 'node:path';
-import {Worker} from 'node:worker_threads';
+import {fork, spawnSync} from 'node:child_process';
+import {fileURLToPath} from 'node:url';
import postcss from 'postcss';
import {assetDataUri, isAssetBytes, isAssetPath, RenderAssetError} from './assets.mjs';
import {classesIn, cutCss, layerOrderOf} from './css-cut.mjs';
@@ -34,7 +35,7 @@ export {PAGE_EXTENSIONS} from './embedded-files.mjs';
/**
* @typedef {object} RenderWarning
- * @property {'dropped-children' | 'remote-asset' | 'missing-asset' | 'page-output' | 'page-script' | 'no-astryx-styles' | 'chart-size' | 'not-embedded'} code - What kind of problem.
+ * @property {'dropped-children' | 'remote-asset' | 'missing-asset' | 'page-output' | 'page-script' | 'no-astryx-styles' | 'chart-size' | 'not-embedded' | 'node-crash'} code - What kind of problem.
* @property {string} message - What happened and what to do, in one sentence.
*/
@@ -221,65 +222,158 @@ function expandImports(file, css, ctx) {
/** @typedef {{dir: string, integrations: readonly string[]}} ServedSet */
+/** Signals that Node itself or a native module raises when it fails. */
+const CRASH_SIGNALS = new Set(['SIGSEGV', 'SIGBUS', 'SIGILL', 'SIGFPE', 'SIGABRT', 'SIGTRAP']);
+
+/**
+ * How a native crash ended a process, or null when it was not one. On
+ * Windows a crash arrives as an exit status of 0xC0000000 and above (such as
+ * 0xC0000005, an access violation), not as a signal.
+ * @param {number | null} code
+ * @param {NodeJS.Signals | null} signal
+ */
+function crashOf(code, signal) {
+ if (signal) return CRASH_SIGNALS.has(signal) ? signal : null;
+ if (code !== null && code >>> 0 >= 0xc0000000) return `0x${(code >>> 0).toString(16).toUpperCase()}`;
+ return null;
+}
+
/**
- * Run the page in a worker, stopping it after `timeoutMs`.
- * @param {string} entry
- * @param {string} root
+ * Whether this Node crashes when it splits text with Intl.Segmenter. Some
+ * Node builds do, and Astryx components such as Avatar split text with it.
+ */
+function segmenterCrashes() {
+ const probe = spawnSync(
+ process.execPath,
+ ['-e', "[...new Intl.Segmenter('en', {granularity: 'grapheme'}).segment('a b')]"],
+ {stdio: 'ignore', timeout: 10_000},
+ );
+ return crashOf(probe.status, probe.signal) !== null;
+}
+
+/**
+ * The error for a page process that a crash or a signal ended. A crash comes
+ * from Node itself or a native module, never from the page's JavaScript.
+ * Every message names this Node.
+ * @param {string} how the crash (a signal name or a Windows status) or the stopping signal
+ * @param {{crashed: boolean, withoutSegmenter: boolean}} state
+ */
+function crashError(how, {crashed, withoutSegmenter}) {
+ const node = `Node ${process.version} (${process.execPath})`;
+ const detail = {signal: how, node: process.version, nodePath: process.execPath};
+ if (!crashed) {
+ const memory = how === 'SIGKILL' ? ' The system stops a process this way when the machine runs out of memory.' : '';
+ return new RenderError('ERR_RENDER_FAILED', `${node} was stopped by a signal (${how}) while it rendered the page.${memory}`, detail);
+ }
+ const cause = withoutSegmenter ? ' It crashed again with Intl.Segmenter removed, so the fault is elsewhere in this Node or in a native module.' : '';
+ return new RenderError(
+ 'ERR_RENDER_FAILED',
+ `${node} crashed (${how}) while it rendered the page. A fault in Node or in a native module causes this, not the page's code.${cause} Render with an official Node.js build of another version.`,
+ detail,
+ );
+}
+
+/**
+ * Run the page in a host process (page-host.mjs), stopping it after
+ * `timeoutMs`. The host runs the page in a worker thread, and it exits when
+ * this process goes away.
+ * @param {{entry: string, root: string, served: ServedSet | null, withoutSegmenter: boolean}} input
* @param {number} timeoutMs
- * @param {ServedSet | null} served
- * @returns {Promise<any>}
+ * @returns {Promise<any>} the page's answer, or `{crashed: how}` when a
+ * native crash ended the host
*/
-function runWorker(entry, root, timeoutMs, served) {
+function runWorker(input, timeoutMs) {
return new Promise((resolve, reject) => {
- const worker = new Worker(new URL('./page-worker.mjs', import.meta.url), {
- workerData: {entry, root, served: served ? {dir: served.dir, integrations: [...served.integrations]} : null},
+ const data = {
+ ...input,
+ served: input.served ? {dir: input.served.dir, integrations: [...input.served.integrations]} : null,
+ timeoutMs,
+ };
+ const host = fork(fileURLToPath(new URL('./page-host.mjs', import.meta.url)), [JSON.stringify(data)], {
+ // Never the CLI's own flags (such as --input-type).
execArgv: [],
- stdout: true,
- stderr: true,
- resourceLimits: {maxOldGenerationSizeMb: 4096},
+ stdio: ['ignore', 'ignore', 'ignore', 'ipc'],
+ serialization: 'advanced',
});
- // The page's own writes to stdout/stderr never reach the CLI's output.
- worker.stdout.resume();
- worker.stderr.resume();
let settled = false;
- const timer = setTimeout(() => {
+ /** @param {() => void} settle */
+ const finish = settle => {
if (settled) return;
settled = true;
- void worker.terminate();
- reject(
- new RenderError(
- 'ERR_RENDER_TIMEOUT',
- `The page did not finish rendering within ${timeoutMs / 1000} seconds, so it was stopped. Look for a loop that never ends or a promise the page waits on.`,
- {seconds: timeoutMs / 1000},
+ clearTimeout(timer);
+ host.kill('SIGKILL');
+ settle();
+ };
+ const timer = setTimeout(() => {
+ finish(() =>
+ reject(
+ new RenderError(
+ 'ERR_RENDER_TIMEOUT',
+ `The page did not finish rendering within ${timeoutMs / 1000} seconds, so it was stopped. Look for a loop that never ends or a promise the page waits on.`,
+ {seconds: timeoutMs / 1000},
+ ),
),
);
}, timeoutMs);
- worker.once('message', message => {
- if (settled) return;
- settled = true;
- clearTimeout(timer);
- void worker.terminate();
- resolve(message);
+ host.once('message', (/** @type {any} */ message) => {
+ finish(() => {
+ if (message?.hostError !== undefined) {
+ const text = String(message.hostError);
+ reject(
+ /ERR_WORKER_OUT_OF_MEMORY|heap out of memory/.test(text)
+ ? new RenderError('ERR_RENDER_FAILED', 'The page ran out of memory while rendering.')
+ : new RenderError('ERR_RENDER_FAILED', `The page could not be rendered: ${text.split('\n')[0]}`),
+ );
+ } else if (message?.hostExit !== undefined) {
+ reject(new RenderError('ERR_RENDER_FAILED', `The page stopped rendering without an answer (exit ${message.hostExit}).`));
+ } else {
+ resolve(message);
+ }
+ });
});
- worker.once('error', (/** @type {any} */ error) => {
- if (settled) return;
- settled = true;
- clearTimeout(timer);
- const out =
- /ERR_WORKER_OUT_OF_MEMORY|heap out of memory/.test(String(error?.message ?? error))
- ? new RenderError('ERR_RENDER_FAILED', 'The page ran out of memory while rendering.')
- : new RenderError('ERR_RENDER_FAILED', `The page could not be rendered: ${String(error?.message ?? error).split('\n')[0]}`);
- reject(out);
+ host.once('error', error => {
+ finish(() => reject(new RenderError('ERR_RENDER_FAILED', `The page could not be rendered: ${String(error?.message ?? error).split('\n')[0]}`)));
});
- worker.once('exit', code => {
- if (settled) return;
- settled = true;
- clearTimeout(timer);
- reject(new RenderError('ERR_RENDER_FAILED', `The page stopped rendering without an answer (exit ${code}).`));
+ host.once('exit', (code, signal) => {
+ finish(() => {
+ const crash = crashOf(code, signal);
+ if (crash) resolve({crashed: crash});
+ else if (signal) reject(crashError(signal, {crashed: false, withoutSegmenter: input.withoutSegmenter}));
+ else reject(new RenderError('ERR_RENDER_FAILED', `The page stopped rendering without an answer (exit ${code}).`));
+ });
});
});
}
+/**
+ * Run the page. When this Node crashes in Intl.Segmenter, run it once more
+ * without Intl.Segmenter and say so in a warning.
+ * @param {{entry: string, root: string, served: ServedSet | null}} input
+ * @param {number} timeoutMs
+ * @param {RenderWarning[]} warnings
+ */
+async function runPage(input, timeoutMs, warnings) {
+ const started = Date.now();
+ const first = await runWorker({...input, withoutSegmenter: false}, timeoutMs);
+ if (!first.crashed) return first;
+ if (!segmenterCrashes()) throw crashError(first.crashed, {crashed: true, withoutSegmenter: false});
+ const again = await runWorker({...input, withoutSegmenter: true}, Math.max(1000, timeoutMs - (Date.now() - started)));
+ if (again.crashed) throw crashError(again.crashed, {crashed: true, withoutSegmenter: true});
+ warnings.push(segmenterWarning());
+ return again;
+}
+
+/**
+ * The warning for a page rendered without Intl.Segmenter.
+ * @returns {RenderWarning}
+ */
+export function segmenterWarning() {
+ return {
+ code: 'node-crash',
+ message: `This Node build (${process.version} at ${process.execPath}) crashes in Intl.Segmenter, so the page rendered without it, and the output may differ slightly: initials and character counts (such as Avatar initials) can split a joined emoji, a flag, or a letter written with a separate accent mark. Render with an official Node.js build to remove the difference.`,
+ };
+}
+
/**
* Inline local images (src, srcset, poster and CSS url()) as data: URIs. A
* link (`href`) loads nothing, so it is left alone.
@@ -371,9 +465,9 @@ export async function renderPage(entry, options = {}) {
const dir = path.dirname(entry);
const root = projectRootOf(dir);
const served = options.served ?? null;
- const message = await runWorker(entry, root, options.timeoutMs ?? RENDER_TIMEOUT_MS, served);
/** @type {RenderWarning[]} */
const warnings = [];
+ const message = await runPage({entry, root, served}, options.timeoutMs ?? RENDER_TIMEOUT_MS, warnings);
if (!message.ok) {
throw new RenderError(message.error.code, message.error.message, message.error.detail);
}
diff --git a/packages/cli/foundation/response/error-codes.doc.mjs b/packages/cli/foundation/response/error-codes.doc.mjs
index 317a7112d6..903df89389 100644
--- a/packages/cli/foundation/response/error-codes.doc.mjs
+++ b/packages/cli/foundation/response/error-codes.doc.mjs
@@ -332,7 +332,7 @@ export const doc = {
value: 'ERR_RENDER_FAILED',
stability: 'experimental',
description:
- 'The page threw while rendering. Nothing was written. Raised only by the experimental render command.',
+ 'The page threw while rendering, or Node crashed while it rendered the page. Nothing was written. Raised only by the experimental render command.',
},
{
value: 'ERR_RENDER_NOT_ASTRYX',