cultmesh-ts: add a steady-state memory test for the nextEvent leak

Calls the runtime's private `nextEvent` directly (0 ms timeout, no
listener/connection open, so each round trip returns immediately with
status 0) 200,000 times after a warm-up, then asserts RSS growth
across the run stays under 5 MiB with `global.gc()` forced before and
after. The old per-call `koffi.alloc` code allocates a ~64 B event
struct plus a 4 B int32 every iteration and never frees either, so it
clears this bound by roughly 2x; the fixed code, which allocates
nothing per call, stays flat. Skips when no native bridge is built or
when Node wasn't started with `--expose-gc`; `npm test` now passes
`--expose-gc` so the check runs by default instead of always skipping.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
This commit is contained in:
Meta Vens 2026-09-23 19:11:41 +02:00
commit 9a7e20a8d2
2 changed files with 62 additions and 1 deletions

View file

@ -18,7 +18,7 @@
"build": "node ../../node_modules/typescript/bin/tsc -p tsconfig.json",
"prepack": "node ../../node_modules/typescript/bin/tsc -p tsconfig.json",
"typecheck": "node ../../node_modules/typescript/bin/tsc -p tsconfig.json --pretty false",
"test": "node ../../node_modules/typescript/bin/tsc -p tsconfig.json && node ../../node_modules/typescript/bin/tsc -p tsconfig.test.json && node --test dist-test/test/*.test.js"
"test": "node ../../node_modules/typescript/bin/tsc -p tsconfig.json && node ../../node_modules/typescript/bin/tsc -p tsconfig.test.json && node --expose-gc --test dist-test/test/*.test.js"
},
"keywords": [
"cultmesh",

View file

@ -205,3 +205,64 @@ test("negative grep: no koffi callbacks, .async only on nextEvent", () => {
}
assert.doesNotMatch(source, /koffi\.register/, "no koffi callbacks");
});
test(
"memory: sustained nextEvent calls do not leak koffi-allocated out-parameter buffers",
{ timeout: 60_000 },
async (t) => {
if (!nativeBridgeAvailable()) {
t.skip("CULTMESH_QUIC_NATIVE_DIR is not set to a built native bridge.");
return;
}
if (typeof global.gc !== "function") {
t.skip("run with --expose-gc to measure RSS growth deterministically.");
return;
}
const runtime = await CultMeshQuicNativeRuntime.open();
try {
// `nextEvent` is private: the pump loop is the only production caller,
// and it never has more than one call in flight at a time, which is
// exactly the property this test needs to call it directly in a tight
// loop instead of waiting on the pump's 250 ms idle poll. A 0 ms
// timeout with no listener/connection open returns immediately with
// status 0 (no event), so each iteration is a fast round trip through
// the real native ABI, not a synthetic stand-in.
const nextEvent = (
runtime as unknown as {
nextEvent(timeoutMs: number, payload: Uint8Array | null, payloadCapacity: number): Promise<unknown>;
}
).nextEvent.bind(runtime);
// Warm up: let koffi's own one-time bookkeeping (module init, JIT,
// thread pool spin-up) happen before the baseline measurement.
for (let i = 0; i < 2_000; i += 1) {
await nextEvent(0, null, 0);
}
global.gc();
const before = process.memoryUsage().rss;
// Sized so the old per-call `koffi.alloc` code (~64 B event struct +
// ~4 B int32, plus allocator bookkeeping, well over 53 B/call) clearly
// exceeds the bound below, while the fixed code, which allocates
// nothing per call, stays flat.
const ITERATIONS = 200_000;
for (let i = 0; i < ITERATIONS; i += 1) {
await nextEvent(0, null, 0);
}
global.gc();
const after = process.memoryUsage().rss;
const growthBytes = after - before;
const BOUND_BYTES = 5 * 1024 * 1024;
assert.ok(
growthBytes < BOUND_BYTES,
`RSS grew by ${growthBytes} bytes (${(growthBytes / 1024 / 1024).toFixed(2)} MiB) over ` +
`${ITERATIONS} nextEvent calls; expected it to stay under ${BOUND_BYTES / 1024 / 1024} MiB ` +
"once out-parameter buffers are reused instead of allocated per call.",
);
} finally {
await runtime.release();
}
},
);