From fcacd94fd708a3de3c3a0c101a2d2f2064ed7493 Mon Sep 17 00:00:00 2001 From: Ruben Bridgewater Date: Thu, 19 Nov 2026 18:21:49 +0100 Subject: [PATCH] test(heap): verify OOM profiles without requiring a crash V8 can reach the heap limit again during profile capture. The callback can grant temporary headroom even when the caller requests zero bytes. The fixture exits when it sees the limit rise, so requiring a fatal error fails even when the profile contains the expected stack. Give the child a nonzero extension and require the allocation stack and a successful exit. Read the output after the child closes its stdio. --- ts/test/test-heap-profiler.ts | 29 ++++++++++++++++------------- 1 file changed, 16 insertions(+), 13 deletions(-) diff --git a/ts/test/test-heap-profiler.ts b/ts/test/test-heap-profiler.ts index 743302f9..79361c53 100644 --- a/ts/test/test-heap-profiler.ts +++ b/ts/test/test-heap-profiler.ts @@ -14,6 +14,8 @@ * limitations under the License. */ +import {once} from 'node:events'; + import * as sinon from 'sinon'; import * as heapProfiler from '../src/heap-profiler'; @@ -456,17 +458,21 @@ describe('foreign heap sampler', () => { }); describe('OOMMonitoring', () => { + const CONFIGURED_HEAP_EXTENSION = String(64 * 1024 * 1024); + it('should dump an OOM profile after an allocation collection', async function () { - if (Number(process.versions.node.split('.')[0]) < 26) this.skip(); + if (Number(process.versions.node.split('.')[0]) < 26) { + this.skip(); + } this.timeout(30000); - const {output} = await runOomFixture( + const {code, output} = await runOomFixture( 'oom-heap-limit-extension.js', - '0', + CONFIGURED_HEAP_EXTENSION, 'allocations', ); - assert.ok(output.includes('FATAL ERROR:'), output); assert.ok(output.includes('NearHeapLimit(count=1)'), output); assert.ok(output.includes(':leak:'), output); + assert.strictEqual(code, 0, output); }); async function runOomFixture( @@ -491,14 +497,11 @@ describe('OOMMonitoring', () => { output += chunk; }); - return new Promise<{code: number | null; output: string}>( - (resolve, reject) => { - proc.on('error', reject); - proc.on('exit', code => { - resolve({code, output}); - }); - }, - ); + const [code] = (await once(proc, 'close')) as [ + number | null, + NodeJS.Signals | null, + ]; + return {code, output}; } async function assertHeapLimitIsRestored(heapLimitExtensionSize: string) { @@ -520,7 +523,7 @@ describe('OOMMonitoring', () => { it('should restore a configured heap limit extension', async function () { this.timeout(30000); - await assertHeapLimitIsRestored(String(64 * 1024 * 1024)); + await assertHeapLimitIsRestored(CONFIGURED_HEAP_EXTENSION); }); // The fixture runs under --max-old-space-size=64, and v8 reports