wasm-runtime-flags.ts 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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. * The V8 flag(s) that keep tree-sitter grammar compilation off the turboshaft
  36. * optimizing tier. Single source of truth: the relaunch guard and the test
  37. * suite both read this (a test asserts each is a real flag on the running
  38. * runtime, so a rename can't silently regress the fix).
  39. */
  40. export const WASM_RUNTIME_FLAGS: readonly string[] = ['--liftoff-only'];
  41. /**
  42. * Env var set on the relaunched child so a detection slip can never cause an
  43. * infinite re-exec loop. Also lets users force-disable the relaunch.
  44. */
  45. const RELAUNCH_GUARD_ENV = 'CODEGRAPH_WASM_RELAUNCHED';
  46. /**
  47. * Env var carrying the *host* PID (the relauncher's own parent) across the
  48. * re-exec. Without `--liftoff-only` the CLI re-execs itself once, inserting an
  49. * intermediate process between the MCP host and the server. That intermediate
  50. * stays alive (blocked in spawnSync) even after the host is killed, so the
  51. * server's PPID watchdog can't detect the host's death by watching its own
  52. * `process.ppid`. Passing the host PID through lets the watchdog poll it
  53. * directly. Unset on the no-re-exec path (bundled launcher / flag already
  54. * present), where the server is already a direct child of the host. See
  55. * src/mcp/index.ts (#277).
  56. */
  57. export const HOST_PPID_ENV = 'CODEGRAPH_HOST_PPID';
  58. /** True when every required WASM runtime flag is already present in `execArgv`. */
  59. export function processHasWasmRuntimeFlags(
  60. execArgv: readonly string[] = process.execArgv
  61. ): boolean {
  62. return WASM_RUNTIME_FLAGS.every((flag) => execArgv.includes(flag));
  63. }
  64. /**
  65. * Build the argv for re-execing node with the WASM runtime flags: our flags
  66. * first, then any node flags already in `execArgv` (deduped), then the script
  67. * and its args. Pure — exported for unit testing.
  68. */
  69. export function buildRelaunchArgv(
  70. scriptPath: string,
  71. scriptArgs: readonly string[],
  72. execArgv: readonly string[] = process.execArgv
  73. ): string[] {
  74. const preserved = execArgv.filter((arg) => !WASM_RUNTIME_FLAGS.includes(arg));
  75. return [...WASM_RUNTIME_FLAGS, ...preserved, scriptPath, ...scriptArgs];
  76. }
  77. /**
  78. * If the current process is missing the WASM runtime flags, re-exec it once
  79. * with them and exit with the child's status. No-op when the flags are already
  80. * present (the normal bundled-launcher path), when already relaunched, or when
  81. * disabled via CODEGRAPH_NO_RELAUNCH.
  82. *
  83. * On spawn failure, returns so the caller runs in-process anyway — risking the
  84. * OOM is still better than refusing to start.
  85. */
  86. export function relaunchWithWasmRuntimeFlagsIfNeeded(scriptPath: string): void {
  87. if (processHasWasmRuntimeFlags()) return;
  88. if (process.env[RELAUNCH_GUARD_ENV]) return;
  89. if (process.env.CODEGRAPH_NO_RELAUNCH) return;
  90. const argv = buildRelaunchArgv(scriptPath, process.argv.slice(2));
  91. const result = spawnSync(process.execPath, argv, {
  92. stdio: 'inherit',
  93. env: { ...process.env, [RELAUNCH_GUARD_ENV]: '1', [HOST_PPID_ENV]: String(process.ppid) },
  94. });
  95. if (result.error) {
  96. // Couldn't relaunch (e.g. execPath unavailable) — fall through and run in
  97. // this process. Degraded (may OOM on huge repos) but not broken.
  98. return;
  99. }
  100. process.exit(result.status ?? (result.signal ? 1 : 0));
  101. }