Skip to content

Commit e5d6330

Browse files
colbymchenryclaude
andauthored
fix: prevent V8 turboshaft WASM Zone OOM during indexing (#298, #293) (#322)
Large multi-language indexes crashed with `Fatal process out of memory: Zone` on Node 22/24 (including the bundled runtime) — V8's turboshaft optimizing WASM compiler exhausts its per-compilation Zone arena while compiling tree-sitter grammars on a background thread, even with tens of GB free (the Zone is a V8-internal arena, not the JS heap). Run node with V8 `--liftoff-only`, which keeps grammar compilation on the Liftoff baseline and never reaches the optimizing tier. Delivered via the bundled launcher + a one-shot CLI re-exec guard for all other launch paths. Empirically only `--liftoff-only` stops it (`--no-wasm-tier-up` / `--no-wasm-dynamic-tiering` do not), and it must be on node's command line (setFlagsFromString / worker execArgv / NODE_OPTIONS all fail). Reproduced the exact crash with the real indexer on Node 24.16 against a 2,880-file / 18-language repo and confirmed the fix eliminates it; full suite + 7 new tests pass. Bumps to 0.9.4. Co-authored-by: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
1 parent bf73f4d commit e5d6330

8 files changed

Lines changed: 231 additions & 6 deletions

File tree

CHANGELOG.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,26 @@ a [GitHub Release](https://github.com/colbymchenry/codegraph/releases) tagged
77
This project follows [Keep a Changelog](https://keepachangelog.com/en/1.1.0/)
88
and adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
99

10+
## [0.9.4] - 2026-05-22
11+
12+
### Fixed
13+
- **`Fatal process out of memory: Zone` crash while indexing large projects.**
14+
On Node.js 22 and 24 — including CodeGraph's own bundled runtime — running
15+
`codegraph index` / `codegraph init` on a large multi-language repo could
16+
abort the entire process partway through parsing with
17+
`Fatal process out of memory: Zone`, even with tens of GB of RAM free (the
18+
failure is in a V8-internal compilation arena, not the JS heap). The cause is
19+
V8's "turboshaft" optimizing WASM compiler exhausting its Zone budget while
20+
compiling tree-sitter's large WebAssembly grammars on a background thread.
21+
CodeGraph now runs with V8's `--liftoff-only`, which keeps grammar compilation
22+
on the baseline compiler and never reaches the optimizing tier, eliminating
23+
the crash; indexing output is otherwise unchanged. The bundled launcher passes
24+
the flag directly, and any other launch path (from source, `npx`, a globally
25+
linked dev build) re-execs once with it automatically. Resolves
26+
[#298](https://github.com/colbymchenry/codegraph/issues/298) and
27+
[#293](https://github.com/colbymchenry/codegraph/issues/293). (Node 25 stays
28+
blocked — its variant of this V8 bug is not resolved by `--liftoff-only`.)
29+
1030
## [0.9.3] - 2026-05-22
1131

1232
### Added
@@ -116,6 +136,7 @@ and adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
116136
find its bundle. The release pipeline now verifies every package reached the
117137
registry (and is idempotent), so a release can't pass green-but-broken again.
118138

139+
[0.9.4]: https://github.com/colbymchenry/codegraph/releases/tag/v0.9.4
119140
[0.9.3]: https://github.com/colbymchenry/codegraph/releases/tag/v0.9.3
120141
[0.9.2]: https://github.com/colbymchenry/codegraph/releases/tag/v0.9.2
121142
[0.9.1]: https://github.com/colbymchenry/codegraph/releases/tag/v0.9.1
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
/**
2+
* WASM runtime flags — the workaround for the V8 turboshaft WASM Zone OOM
3+
* (`Fatal process out of memory: Zone`) that crashed `codegraph index` on large
4+
* polyglot repos under Node >= 22. See issues #293 and #298.
5+
*
6+
* The crash was reproduced with the real indexer on the bundled Node 24 runtime;
7+
* empirically only `--liftoff-only` prevents it (`--no-wasm-tier-up` /
8+
* `--no-wasm-dynamic-tiering` do not), and the flag must be on node's command
9+
* line — `setFlagsFromString`, worker `execArgv`, and `NODE_OPTIONS` all fail.
10+
* These tests pin that contract so it can't silently regress.
11+
*/
12+
import { describe, it, expect } from 'vitest';
13+
import { spawnSync } from 'child_process';
14+
import * as fs from 'fs';
15+
import * as os from 'os';
16+
import * as path from 'path';
17+
import {
18+
WASM_RUNTIME_FLAGS,
19+
processHasWasmRuntimeFlags,
20+
buildRelaunchArgv,
21+
} from '../src/extraction/wasm-runtime-flags';
22+
23+
describe('WASM_RUNTIME_FLAGS', () => {
24+
it('pins --liftoff-only (the only flag shown to stop the turboshaft Zone OOM)', () => {
25+
// On Node 24, --no-wasm-tier-up and --no-wasm-dynamic-tiering both still
26+
// crash; only --liftoff-only forces grammars onto the Liftoff baseline and
27+
// off the optimizing tier. Pin it so it can't be swapped for an ineffective
28+
// flag.
29+
expect(WASM_RUNTIME_FLAGS).toContain('--liftoff-only');
30+
});
31+
32+
it('every flag is a real, accepted flag on the running Node/V8 runtime', () => {
33+
// node rejects unknown CLI flags at startup, so a renamed/removed flag would
34+
// break the bundled launcher and make the relaunch guard a silent no-op.
35+
// Prove each flag actually launches node here.
36+
const res = spawnSync(
37+
process.execPath,
38+
[...WASM_RUNTIME_FLAGS, '-e', 'process.exit(0)'],
39+
{ encoding: 'utf8' }
40+
);
41+
expect(res.status, `node rejected ${WASM_RUNTIME_FLAGS.join(' ')}:\n${res.stderr}`).toBe(0);
42+
});
43+
});
44+
45+
describe('processHasWasmRuntimeFlags', () => {
46+
it('is true only when every required flag is present', () => {
47+
expect(processHasWasmRuntimeFlags(['--liftoff-only'])).toBe(true);
48+
expect(processHasWasmRuntimeFlags(['--liftoff-only', '--enable-source-maps'])).toBe(true);
49+
});
50+
51+
it('is false when the flags are absent', () => {
52+
expect(processHasWasmRuntimeFlags([])).toBe(false);
53+
expect(processHasWasmRuntimeFlags(['--max-old-space-size=4096'])).toBe(false);
54+
});
55+
});
56+
57+
describe('buildRelaunchArgv', () => {
58+
it('places the wasm flags first, then the script and its args', () => {
59+
expect(buildRelaunchArgv('/x/codegraph.js', ['index', '/repo'], [])).toEqual([
60+
'--liftoff-only',
61+
'/x/codegraph.js',
62+
'index',
63+
'/repo',
64+
]);
65+
});
66+
67+
it('preserves other existing node flags without duplicating ours', () => {
68+
expect(
69+
buildRelaunchArgv('/x/codegraph.js', ['status'], ['--liftoff-only', '--enable-source-maps'])
70+
).toEqual(['--liftoff-only', '--enable-source-maps', '/x/codegraph.js', 'status']);
71+
});
72+
73+
it('produces an argv that actually launches node WITH the flag applied', () => {
74+
// End-to-end proof of the delivery mechanism without needing the crash:
75+
// run the constructed argv and confirm the child sees the flag in execArgv.
76+
const dir = fs.mkdtempSync(path.join(os.tmpdir(), 'cg-relaunch-'));
77+
try {
78+
const harness = path.join(dir, 'harness.cjs');
79+
fs.writeFileSync(harness, 'process.stdout.write(JSON.stringify(process.execArgv));');
80+
const res = spawnSync(process.execPath, buildRelaunchArgv(harness, []), { encoding: 'utf8' });
81+
expect(res.status, res.stderr).toBe(0);
82+
expect(JSON.parse(res.stdout)).toContain('--liftoff-only');
83+
} finally {
84+
fs.rmSync(dir, { recursive: true, force: true });
85+
}
86+
});
87+
});

package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@colbymchenry/codegraph",
3-
"version": "0.9.3",
3+
"version": "0.9.4",
44
"description": "Supercharge Claude Code with semantic code intelligence. 94% fewer tool calls • 77% faster exploration • 100% local.",
55
"main": "dist/index.js",
66
"types": "dist/index.d.ts",

scripts/build-bundle.sh

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,9 +70,18 @@ rm -f "$STAGE/lib/package-lock.json"
7070

7171
# 4. Vendored Node + launcher (the launcher uses the bundled Node by relative
7272
# path, so no system Node is ever needed).
73+
#
74+
# `--liftoff-only`: keep tree-sitter's large WASM grammars on V8's Liftoff
75+
# baseline compiler so they never reach the turboshaft optimizing tier, whose
76+
# per-compilation Zone arena OOMs the whole process (`Fatal process out of
77+
# memory: Zone`) on Node >= 22 — even with tens of GB free. The flag is read at
78+
# V8 engine init so it must be on node's command line; the parse worker inherits
79+
# it. See issues #293/#298 and src/extraction/wasm-runtime-flags.ts. (The CLI
80+
# also self-relaunches with this flag when launched without it, so non-bundled
81+
# runs are covered too; passing it here avoids that extra spawn.)
7382
if [ "$OSFAM" = "win32" ]; then
7483
cp "$NODE_BIN" "$STAGE/node.exe"
75-
printf '@"%%~dp0..\\node.exe" "%%~dp0..\\lib\\dist\\bin\\codegraph.js" %%*\r\n' \
84+
printf '@"%%~dp0..\\node.exe" --liftoff-only "%%~dp0..\\lib\\dist\\bin\\codegraph.js" %%*\r\n' \
7685
> "$STAGE/bin/codegraph.cmd"
7786
else
7887
cp "$NODE_BIN" "$STAGE/node"
@@ -89,7 +98,8 @@ while [ -L "$SELF" ]; do
8998
esac
9099
done
91100
DIR="$(cd "$(dirname "$SELF")/.." && pwd)"
92-
exec "$DIR/node" "$DIR/lib/dist/bin/codegraph.js" "$@"
101+
# --liftoff-only: avoid the V8 turboshaft WASM Zone OOM (issues #293/#298).
102+
exec "$DIR/node" --liftoff-only "$DIR/lib/dist/bin/codegraph.js" "$@"
93103
LAUNCH
94104
chmod +x "$STAGE/bin/codegraph"
95105
fi

scripts/npm-shim.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,10 @@ try {
3131
if (isWindows) {
3232
command = require.resolve(pkg + '/node.exe');
3333
var entry = require.resolve(pkg + '/lib/dist/bin/codegraph.js');
34-
args = [entry].concat(process.argv.slice(2));
34+
// --liftoff-only: keep tree-sitter's WASM grammars off V8's turboshaft tier
35+
// to avoid the Zone OOM on Node >= 22 (issues #293/#298). The unix launcher
36+
// passes this too; on Windows we invoke node.exe directly so add it here.
37+
args = ['--liftoff-only', entry].concat(process.argv.slice(2));
3538
} else {
3639
command = require.resolve(pkg + '/bin/codegraph');
3740
args = process.argv.slice(2);

src/bin/codegraph.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ import { createShimmerProgress } from '../ui/shimmer-progress';
2727
import { getGlyphs } from '../ui/glyphs';
2828

2929
import { buildNode25BlockBanner, buildNodeTooOldBanner, MIN_NODE_MAJOR } from './node-version-check';
30+
import { relaunchWithWasmRuntimeFlagsIfNeeded } from '../extraction/wasm-runtime-flags';
3031

3132
// Lazy-load heavy modules (CodeGraph, runInstaller) to keep CLI startup fast.
3233
async function loadCodeGraph(): Promise<typeof import('../index')> {
@@ -75,6 +76,13 @@ if (nodeMajor < MIN_NODE_MAJOR) {
7576
// Override active — banner shown for visibility, continuing.
7677
}
7778

79+
// Re-exec with V8's `--liftoff-only` if it isn't already set, so tree-sitter's
80+
// large WASM grammars never hit the turboshaft Zone OOM (`Fatal process out of
81+
// memory: Zone`) on Node >= 22. No-op under the bundled launcher, which already
82+
// passes the flag. Must run before any grammar (in the parse worker, which
83+
// inherits this process's flags) is compiled. See ../extraction/wasm-runtime-flags.
84+
relaunchWithWasmRuntimeFlagsIfNeeded(__filename);
85+
7886
// Check if running with no arguments - run installer
7987
if (process.argv.length === 2) {
8088
import('../installer').then(({ runInstaller }) =>
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
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

Comments
 (0)