|
| 1 | +/** |
| 2 | + * WASM runtime flags — workaround for the V8 turboshaft WASM Zone OOM. |
| 3 | + * |
| 4 | + * tree-sitter grammars are large WebAssembly modules. On Node >= 22 the V8 |
| 5 | + * "turboshaft" optimizing WASM compiler can exhaust its per-compilation Zone |
| 6 | + * arena while compiling these grammars on a background thread, aborting the |
| 7 | + * whole process with `Fatal process out of memory: Zone` — even with tens of |
| 8 | + * GB of system memory free, because the Zone is a V8-internal arena, not the |
| 9 | + * JS heap. Reproduced on Node 22 and 24; Node 25 is already hard-blocked for |
| 10 | + * the same crash (see ../bin/node-version-check.ts). See issues #293 and #298. |
| 11 | + * |
| 12 | + * `--liftoff-only` forces every WASM module to the Liftoff baseline compiler |
| 13 | + * and never runs turboshaft, which eliminates the crash. Parsing stays fully |
| 14 | + * correct; we only forgo the (marginal, and for grammars rarely reached) |
| 15 | + * optimized-tier speedup. |
| 16 | + * |
| 17 | + * This flag MUST be on node's command line — it is read by V8 at engine init, |
| 18 | + * before any of our JS runs. Empirically (Node 24) none of these work: |
| 19 | + * - `v8.setFlagsFromString('--liftoff-only')` at runtime — too late. |
| 20 | + * - Worker `execArgv: ['--liftoff-only']` — rejected (ERR_WORKER_INVALID_EXEC_ARGV). |
| 21 | + * - `NODE_OPTIONS=--liftoff-only` — not on Node's NODE_OPTIONS allowlist. |
| 22 | + * Also empirically, `--no-wasm-tier-up` / `--no-wasm-dynamic-tiering` do NOT |
| 23 | + * prevent the crash — only disabling the optimizing tier entirely does. |
| 24 | + * |
| 25 | + * Delivery: the bundled launcher passes the flag directly (see |
| 26 | + * scripts/build-bundle.sh and scripts/npm-shim.js); for any other launch path |
| 27 | + * (running dist directly, from source, etc.) the CLI re-execs itself once with |
| 28 | + * the flag via {@link relaunchWithWasmRuntimeFlagsIfNeeded}. V8 flags are |
| 29 | + * PROCESS-global, and the parse worker is created with default (inherited) |
| 30 | + * execArgv, so flagging the main process governs the worker's WASM compilation |
| 31 | + * too. |
| 32 | + */ |
| 33 | +import { spawnSync } from 'child_process'; |
| 34 | + |
| 35 | +/** |
| 36 | + * The V8 flag(s) that keep tree-sitter grammar compilation off the turboshaft |
| 37 | + * optimizing tier. Single source of truth: the relaunch guard and the test |
| 38 | + * suite both read this (a test asserts each is a real flag on the running |
| 39 | + * runtime, so a rename can't silently regress the fix). |
| 40 | + */ |
| 41 | +export const WASM_RUNTIME_FLAGS: readonly string[] = ['--liftoff-only']; |
| 42 | + |
| 43 | +/** |
| 44 | + * Env var set on the relaunched child so a detection slip can never cause an |
| 45 | + * infinite re-exec loop. Also lets users force-disable the relaunch. |
| 46 | + */ |
| 47 | +const RELAUNCH_GUARD_ENV = 'CODEGRAPH_WASM_RELAUNCHED'; |
| 48 | + |
| 49 | +/** True when every required WASM runtime flag is already present in `execArgv`. */ |
| 50 | +export function processHasWasmRuntimeFlags( |
| 51 | + execArgv: readonly string[] = process.execArgv |
| 52 | +): boolean { |
| 53 | + return WASM_RUNTIME_FLAGS.every((flag) => execArgv.includes(flag)); |
| 54 | +} |
| 55 | + |
| 56 | +/** |
| 57 | + * Build the argv for re-execing node with the WASM runtime flags: our flags |
| 58 | + * first, then any node flags already in `execArgv` (deduped), then the script |
| 59 | + * and its args. Pure — exported for unit testing. |
| 60 | + */ |
| 61 | +export function buildRelaunchArgv( |
| 62 | + scriptPath: string, |
| 63 | + scriptArgs: readonly string[], |
| 64 | + execArgv: readonly string[] = process.execArgv |
| 65 | +): string[] { |
| 66 | + const preserved = execArgv.filter((arg) => !WASM_RUNTIME_FLAGS.includes(arg)); |
| 67 | + return [...WASM_RUNTIME_FLAGS, ...preserved, scriptPath, ...scriptArgs]; |
| 68 | +} |
| 69 | + |
| 70 | +/** |
| 71 | + * If the current process is missing the WASM runtime flags, re-exec it once |
| 72 | + * with them and exit with the child's status. No-op when the flags are already |
| 73 | + * present (the normal bundled-launcher path), when already relaunched, or when |
| 74 | + * disabled via CODEGRAPH_NO_RELAUNCH. |
| 75 | + * |
| 76 | + * On spawn failure, returns so the caller runs in-process anyway — risking the |
| 77 | + * OOM is still better than refusing to start. |
| 78 | + */ |
| 79 | +export function relaunchWithWasmRuntimeFlagsIfNeeded(scriptPath: string): void { |
| 80 | + if (processHasWasmRuntimeFlags()) return; |
| 81 | + if (process.env[RELAUNCH_GUARD_ENV]) return; |
| 82 | + if (process.env.CODEGRAPH_NO_RELAUNCH) return; |
| 83 | + |
| 84 | + const argv = buildRelaunchArgv(scriptPath, process.argv.slice(2)); |
| 85 | + const result = spawnSync(process.execPath, argv, { |
| 86 | + stdio: 'inherit', |
| 87 | + env: { ...process.env, [RELAUNCH_GUARD_ENV]: '1' }, |
| 88 | + }); |
| 89 | + |
| 90 | + if (result.error) { |
| 91 | + // Couldn't relaunch (e.g. execPath unavailable) — fall through and run in |
| 92 | + // this process. Degraded (may OOM on huge repos) but not broken. |
| 93 | + return; |
| 94 | + } |
| 95 | + process.exit(result.status ?? (result.signal ? 1 : 0)); |
| 96 | +} |
0 commit comments