Repository navigation
fix(backend-native): Bump neon to 1.1.2 to stop leaking N-API async work, thanks @cavi21 - #12461
Conversation
neon 1.1.1 called napi_delete_async_work inside debug_assert_eq!, so release builds never freed the async work created by every TaskBuilder::promise. In Cube this leaks on every Cube Store result parsed by parse_cubestore_result_message, and the refresh worker grows until it is OOM-killed. Fixed upstream in neon-bindings/neon#1145 (neon 1.1.2). neon 1.1.2 also stops handles from escaping execute_scoped, so get_cubestore_result now uses compute_scoped to return the row and the result array from their scopes. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
|
Thanks for the thorough investigation! I reproduced the leak locally and can confirm both the root cause and the fix. ReproductionRelease builds on darwin-arm64 with Node 24. The test calls
All leaked objects are The remaining ~270 B per call comes from the The leak covers more than
|
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## master #12461 +/- ##
==========================================
- Coverage 86.60% 85.68% -0.93%
==========================================
Files 271 271
Lines 100660 100660
==========================================
- Hits 87173 86246 -927
- Misses 13487 14414 +927
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
|
Thank you @cavi21 for your contribution! 🍰 👍 |
|
@ovr thank you all for all the work done on it :)! |
…ork, thanks @cavi21 (backport #12461 to v1.6) neon 1.1.1 frees N-API async work inside `debug_assert_eq!`, so release builds never call `napi_delete_async_work`. neon 1.1.2 fixes it upstream (neon-bindings/neon#1145). On v1.6 the shipped addon (packages/cubejs-backend-native/Cargo.lock) is still on neon 1.0.0, which is not affected: the bump to 1.1.1 came with #12170 (first released in v1.8.3). This backport only moves the rust/cube workspace lockfile off 1.1.1 and switches `get_cubestore_result` to `compute_scoped`, which neon 1.0.0 also provides. (cherry picked from commit 62dbc3f) Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Check List
Tests for the changes have been added if not covered yetAlready covered bypackages/cubejs-backend-native/test/cubestore-result.test.ts, which exercises bothparseCubestoreResultMessageandgetCubestoreResult. A regression test for the leak itself is not practical: it only happens in release builds, wheredebug_assert_eq!is compiled out. The test suite uses a debug build, where the async work is freed even with neon 1.1.1.Docs have been added / updated if requiredNot required: dependency bump plus an internal change in the native module, with no user-facing configuration or behavior changes.Description of Changes Made
This bumps
neon(andneon-macros) from 1.1.1 to 1.1.2 inpackages/cubejs-backend-native/Cargo.lockandrust/cube/Cargo.lock. The upgrade fixes a native memory leak that makes the refresh worker grow until it is OOM-killed.Root cause
In neon 1.1.1,
call_completeincrates/neon/src/sys/async_work.rsfrees the N-API async work inside a debug assertion:Release builds compile
debug_assert_eq!out, sonapi_delete_async_worknever runs. EveryTaskBuilder::promisecall then leaks itsnapi_async_workand theAsyncResourceattached to it, which holds a V8 global handle. The same pattern also skippednapi_release_threadsafe_functionintsfn.rs.This was fixed upstream in neon-bindings/neon#1145 and released in neon 1.1.2. PostHog found the same leak in production and described it in PostHog/posthog#110220.
In Cube, the hot path is
parse_cubestore_result_message, which runs once for every Cube Store result. The refresh worker queries Cube Store constantly, so it leaks the fastest. API instances leak too, more slowly.Code change in
orchestrator.rsneon 1.1.2 also tightens the lifetime of
Context::execute_scoped:Under the new signature, a handle created inside the scope can no longer be returned out of it.
get_cubestore_resultdid exactly that: it returned the row object and the result array out ofexecute_scoped, and used them after theHandleScopehad already been closed. With neon 1.1.2 that is a compile error:Both calls now use
compute_scoped, which opens anEscapableHandleScopeand escapes the returned value into the parent scope. The output is unchanged.How we found it
Setup: a self-hosted Cube v1.8.10 (official image) refresh worker with 8 scheduled refresh contexts, running in a container with a 5 GB memory limit.
Symptom. Resident memory grew by about 0.25–0.3 GB/h. Every ~17 h the kernel killed the process (
Memory cgroup out of memory,CONSTRAINT_MEMCG, anon-rss ≈ 5.2 GB). There was never aJavaScript heap out of memory.Heap limit. Lowering
--max-old-space-sizefrom 4096 to 3072 changed nothing: the process was still killed at 5.2 GB every ~17 h./proc/<pid>/smapsat 10 h uptime. Total RSS was 3.35 GB. The glibc main arena ([heap]) held 2.49 GB, and all anonymous mappings together, V8 heap included, held 0.71 GB.process.memoryUsage()every 15 minutes, first 4 h.rsswent from 438 to 1442 MB whileheapTotalwent from 232 to 387 MB andexternalstayed at 5–6 MB. The growth was almost entirely native.jemalloc. Running with
LD_PRELOAD=libjemalloc.so.2gave the same growth rate, which rules out glibc fragmentation.jemalloc heap profiling. With
MALLOC_CONF=prof:true,lg_prof_interval:34, we compared a profile taken shortly after start with one taken 3 h later usingjeprof --base. 520 MB of the 644 MB of native growth (81%) was allocated undernapi_create_async_work. We resolved the callers againstindex.nodefrom the official image:The share was the same in both halves of the window: 273 of 327 MB, then 247 of 318 MB.
All Cube releases up to v1.8.14 and current
masterstill lock neon 1.1.1, so they are all affected. TheCargo.tomlfiles already requireneon = "=1", so only the lockfiles change.Testing
Run locally with the pinned Rust toolchain (1.98.1) and Node 24:
cargo check --lockedinpackages/cubejs-backend-native. With only the lockfile bump it fails with the two errors above. With thecompute_scopedchange it passes.cargo metadata --lockedpasses for both workspaces. The lockfile diff touches onlyneonandneon-macros.cargo fmt --checkpasses.cargo clippy --locked --workspace --all-targets --keep-going -- -D warningsinpackages/cubejs-backend-nativepasses.jestinpackages/cubejs-backend-nativeagainst a debug build (yarn native:build-debug):pythonfeature.cubestore-result.test.ts, which covers bothparseCubestoreResultMessageandgetCubestoreResult, passes all 15 tests. These include the legacy and Arrow result sets, error and truncated messages, and 64 concurrent parses of the same buffer.Not run locally:
pythonfeature build and its test suites.