Repository navigation
v3.1.4 — the 40 GB bug: quadratic pair explosion in co-change mining
The 40 GB bug is fixed
If you have seen cxpak serve --mcp grow to many GB — #51, #47, reports of 40+ GB and 57 GB — this is the release that fixes it. Two previous attempts targeted the watcher; the actual cause was somewhere else entirely.
Measured, cxpak serve --mcp on a 12 000-file repository:
| peak RSS | initial index | |
|---|---|---|
| 3.1.3 | 5 036 MB | never ready (still building after 242 s) |
| 3.1.4 | 506 MB | ready in 14 s |
All 12 001 files still indexed. On a normal repository the new bounds never fire and behaviour is unchanged.
Root cause — quadratic pair explosion in co-change mining (#53)
build_co_changes expanded every commit into all pairs of its changed files — F*(F-1)/2 pairs for F files, cloning both paths into a HashMap<(String, String), _> for each pair, allocated in full before any threshold pruning ran.
One mass-touch commit is enough to consume the machine: an initial import, a formatter sweep, a generated-code or vendored-dependency refresh, a squash-merge of a long branch. A 12 000-file commit expands to 71 994 000 pairs.
That single fact explains everything that made this bug hard to pin down:
- Why growth looked spiky rather than monotonic — it is a large transient that is then freed. A long-lived server was caught going 18 MB → 1190 MB in 3 seconds, decaying over ~2 minutes, then re-spiking.
- Why it tracked repo size and not activity — it could not be reproduced on a 302-file repo at any event rate, including 997 full re-index batches in 150 seconds.
- Why the earlier fixes did not hold — #47 correctly identified "two full index deep clones per watcher batch" and #48 removed that multiplier, but not the base cost of the thing being multiplied. 3.1.3 genuinely contains #48's fix, and the bug came back anyway.
The fix is two bounds plus interning:
MAX_FILES_PER_COMMIT(200) skips mass-touch commits. This improves the signal as well as the cost — a commit touching thousands of files links every file to every other file, which is not evidence that any particular pair is coupled.MAX_TRACKED_PAIRS(2 000 000) stops a long history rebuilding the blowup one at-cap commit at a time.- Paths are interned to
u32, so the map holds two integers per entry instead of two heapStrings. Ordering still compares paths, so emitted(file_a, file_b)ordering is byte-identical to before.
Neither bound truncates silently — both counts are reported on stderr. A silent cap reads as "mined everything" when it did not.
Watcher (#50)
- Bounded channel, noise pre-filter, windowed debounce. Under identical churn this cut full re-index batches from 997 to 71. Paths under
target/,.git/,node_modules/,.cxpak/and similar are dropped at the notify callback before they can queue. Note the trade: under sustained churn the index is now up to ~2 s stale where it was ~50 ms. - The overflow rebuild no longer holds two indexes at once. It publishes
Buildingand releases the old index first. Overflow means events were dropped, so the index has just been declared lossy — "indexing in progress, retry" is an honest answer to a question that can no longer be answered correctly, and it is the state clients already retry on. A failed rebuild now publishesFailedrather than stranding the cell onBuilding. - The watch root is canonicalized. The pre-filter only runs when
strip_prefix(root)succeeds, so a relative or symlinked root (on macOS/var→/private/var) would make every strip miss and silently degrade the filter to a no-op — passing exactly the churn it exists to drop. Not previously reachable in production, since both callers canonicalize; the guarantee now lives inFileWatcherrather than depending on caller discipline.
Watcher work by @cefothe.
Known issue — jemalloc tuning is inert on macOS (#54)
jemalloc is now the global allocator, but on macOS its malloc_conf decay tuning does not take effect: jemalloc reads _rjem_malloc_conf there, not the unprefixed name, so the configured values are ignored and macOS runs jemalloc at its defaults.
This is documented rather than fixed in 3.1.4, deliberately. Every number above was measured in exactly this configuration, so the shipped artifact is the one that was validated. Turning the tuning on affects every allocation in the process and has never been measured; enabling it as a side effect of a release would repeat the mistake that made #48 look fixed.
The related stderr noise is fixed — background_threads is now a Linux-only cargo feature, so macOS no longer prints <jemalloc>: option background_thread currently supports pthread only on every start. That matters for a stdio MCP server, where it landed in client logs on every launch.
Upgrading
brew upgrade cxpak
# or
cargo install cxpak@3.1.4
# or
docker pull ghcr.io/barnett-studios/cxpak:3.1.4
No interface change. If you have long-lived cxpak serve --mcp processes, restart them — the fix is in the indexing path, so existing processes keep the old behaviour.
Full changelog: v3.1.3...v3.1.4