codegraph icon indicating copy to clipboard operation
codegraph copied to clipboard

CLI worker pools default to cpus-1 each with no user-facing cap — index builds can saturate many-core machines

Open milesbuckton opened this issue 1 month ago • 1 comments

Title: CLI worker pools default to cpus-1 each with no user-facing cap — index builds can saturate many-core machines

Type: enhancement (missing resource-control knob) + documentation gap

Environment

  • macOS, Apple Silicon (darwin-arm64), 18 logical CPUs, 47 GB RAM
  • @colbymchenry/codegraph v1.6.1, global npm install (~/.codegraph/versions/v1.6.1, node CLI)

Observed behavior

  • A co-installed VS Code extension's native engine saturated my whole machine during a full index (load average 38 on 18 cores, ~1700 %CPU in ps). I reported that to its own maintainers separately — but on inspecting the CLI's source I found the same unbounded-pool pattern here, and throttled proactively. I have not isolated a CLI-only runaway reproduction; this report is about the configuration gap, not a claim of a CLI crash.

Findings (from the installed v1.6.1 source)

Three independently-sized worker pools, each defaulting to cpus - 1:

  1. lib/dist/extraction/parse-pool.js — resolveParsePoolSize(envVal, cpuCount) returns Math.max(1, Math.min(cpuCount - 1, DEFAULT_PARSE_POOL_CAP)) → 17 parse workers on an 18-core machine.
  2. lib/dist/mcp/query-pool.js — resolvePoolSize(envVal, cpuCount) returns Math.max(1, Math.min(cpuCount - 1, MAX_POOL_SIZE)) → 17 query workers.
  3. lib/dist/resolution/resolver-pool.js — ResolverPool.resolvePoolSize sizes its own pool from os.availableParallelism().

Because these run concurrently during index / init, the pools can collectively occupy every core, leaving no headroom — the machine is unresponsive for the duration of the build.

Working but undiscoverable knobs — these exist in code but are not documented anywhere I could find:

Env var Effect Where
CODEGRAPH_PARSE_POOL_SIZE caps the parse pool extraction/parse-pool.js
CODEGRAPH_QUERY_POOL_SIZE caps the query pool (0 = disable, serve reads in-process) mcp/query-pool.js
CODEGRAPH_PARALLEL_RESOLVE_MIN minimum refs before the resolver pool engages resolution/resolver-pool.js
CODEGRAPH_NO_PARALLEL_RESOLVE=1 kill switch, sequential resolution resolution/resolver-pool.js

There is no equivalent in user-facing config: codegraph index --help and codegraph init --help expose no thread/worker option, and codegraph.json (project config) has no thread setting.

Requests

  1. Document the env knobs above in the README — they are the only cap that exists today, and I only found them by grepping lib/dist.
  2. Add a first-class limit — e.g. maxThreads in codegraph.json and/or a --threads flag on index/init/sync — applied across all pools together (parse + query + resolve + embedding), so one number bounds total engine CPU.
  3. Default to leaving headroom. cpus - 1 per pool is reasonable for one pool; with three pools running concurrently the sum can hit the full core count. Either size pools from a budget (total ≤ ½ or ¾ of cores) or cap the aggregate.
  4. --verbose already prints worker lifecycle/memory info — please also print the resolved pool sizes at startup so users can confirm the cap took effect.

Current workaround (verified working) — export CODEGRAPH_PARSE_POOL_SIZE=5 and CODEGRAPH_QUERY_POOL_SIZE=4 (rayon/tokio env vars do not apply to this Node CLI; those are only for the native Rust engine build).

milesbuckton avatar Oct 12 '26 18:10 milesbuckton

Real-incident datapoint from Windows — the same cpus-derived default is a memory hazard on RAM-constrained boxes, not just a CPU-saturation one. RAM-aware sizing belongs in the same fix.

Environment: Windows 10 Pro 22H2 (build 19045), 12 logical CPUs, 15.3 GB RAM, @colbymchenry/codegraph 1.6.2.

What the 1.6.2 source does (lib/dist/extraction/parse-pool.js): resolveParsePoolSize is Math.max(1, Math.min(cpuCount - 1, 8)) — clamp(cores−1, 1, 8), with no os.totalmem/os.freemem input anywhere in the file. The file's own comments state the risk this leaves unbounded: each worker holds its own tree-sitter WASM linear memory that "grows but never shrinks" between recycles (every 250 parses), and "peak scales with pool size (≈ size × a worker's pre-recycle heap)". The cap of 8 is justified in the comments as bounding cold-start concurrency, not memory against physical RAM.

Observed on this box (12 cores → 8 workers): codegraph init -y on a ~9.5k-file TypeScript monorepo clone peaked a single node process at 4.6 GB working set ≈ 30% of physical RAM, with Memory Compression at ~0.9 GB and pagefile at 2.4 GB concurrently — the system was swap-stressed for the duration. Two concurrent index sweeps would roughly double the process peak. Mitigations today (CODEGRAPH_PARSE_WORKERS, external watchdogs) must again be discovered by grepping lib/dist, per the OP's documentation-gap point.

Concrete amendment to request #3 ("default to leaving headroom") — size from memory as well as cores. node exposes os.totalmem()/os.freemem() on all platforms, e.g.:

  • floor a RAM-derived cap alongside the core heuristic: workers = min(clamp(cores-1, 1, 8), floor(availablePhysicalRAM / ~450MB)), or
  • default pool = min(cores-1, 4, ceil(totalRAM_GB / 3)) on memory-limited machines, and/or
  • print a warning at init start when pool_size × est_heap > 50% of free RAM.

The existing CODEGRAPH_PARSE_WORKERS override stays unchanged as the escape hatch. On the box above, any of these would have sized the parse pool at 2–4 workers instead of 8 and kept the peak well inside physical memory.

Bearmancer avatar Nov 05 '26 05:11 Bearmancer