From 2e8e71028798debc558fb5d34abe45a4ae64276b Mon Sep 17 00:00:00 2001 From: bassem chagra Date: Thu, 30 Jul 2026 21:19:47 +0200 Subject: [PATCH] fix(memory): dispose the heap-snapshot worker when loading fails HeapSnapshotManager.#loadSnapshot creates a HeapSnapshotWorkerProxy (a live worker) before reading the file, but only ever disposes workers tracked in the #snapshots map. A failed load - e.g. a missing or invalid .heapsnapshot path passed to get_heapsnapshot_* - throws before the entry is added, so neither dispose() nor disposeAll() can reach it and the worker leaks for the life of the process. Wrap the load body in try/catch and dispose the worker before rethrowing. Adds a regression test that fails on the previous behavior. Fixes #2448 --- src/HeapSnapshotManager.ts | 41 +++++++++++++++++++------------ tests/HeapSnapshotManager.test.ts | 39 +++++++++++++++++++++++++++++ 2 files changed, 64 insertions(+), 16 deletions(-) create mode 100644 tests/HeapSnapshotManager.test.ts diff --git a/src/HeapSnapshotManager.ts b/src/HeapSnapshotManager.ts index 0d01cf884c..ae017ed1e8 100644 --- a/src/HeapSnapshotManager.ts +++ b/src/HeapSnapshotManager.ts @@ -362,26 +362,35 @@ export class HeapSnapshotManager { import.meta.resolve('./third_party/devtools-heap-snapshot-worker.js'), ); - const {promise: snapshotPromise, resolve: resolveSnapshot} = - Promise.withResolvers(); + try { + const {promise: snapshotPromise, resolve: resolveSnapshot} = + Promise.withResolvers(); - const loaderProxy = workerProxy.createLoader(uid, snapshotProxy => { - resolveSnapshot(snapshotProxy); - }); - - const fileStream = fsSync.createReadStream(absolutePath, { - encoding: 'utf-8', - highWaterMark: 1024 * 1024, - }); + const loaderProxy = workerProxy.createLoader(uid, snapshotProxy => { + resolveSnapshot(snapshotProxy); + }); - for await (const chunk of fileStream) { - await loaderProxy.write(chunk); - } + const fileStream = fsSync.createReadStream(absolutePath, { + encoding: 'utf-8', + highWaterMark: 1024 * 1024, + }); - await loaderProxy.close(); + for await (const chunk of fileStream) { + await loaderProxy.write(chunk); + } - const snapshot = await snapshotPromise; - return {snapshot, worker: workerProxy}; + await loaderProxy.close(); + + const snapshot = await snapshotPromise; + return {snapshot, worker: workerProxy}; + } catch (error) { + // The worker is created before the read, and a failed load never reaches + // the #snapshots map, so dispose()/disposeAll() can never clean it up. + // Dispose it here to avoid leaking a worker on every failed load (e.g. a + // missing or invalid .heapsnapshot path). + workerProxy.dispose(); + throw error; + } } async getDuplicateStrings(filePath: string): Promise { diff --git a/tests/HeapSnapshotManager.test.ts b/tests/HeapSnapshotManager.test.ts new file mode 100644 index 0000000000..ee5cb259eb --- /dev/null +++ b/tests/HeapSnapshotManager.test.ts @@ -0,0 +1,39 @@ +/** + * @license + * Copyright 2026 Google LLC + * SPDX-License-Identifier: Apache-2.0 + */ + +import assert from 'node:assert'; +import {describe, it, afterEach} from 'node:test'; + +import sinon from 'sinon'; + +import {HeapSnapshotManager} from '../src/HeapSnapshotManager.js'; +import {DevTools} from '../src/third_party/index.js'; + +describe('HeapSnapshotManager', () => { + afterEach(() => { + sinon.restore(); + }); + + it('disposes the worker when snapshot loading fails', async () => { + const disposeSpy = sinon.spy( + DevTools.HeapSnapshotModel.HeapSnapshotProxy.HeapSnapshotWorkerProxy + .prototype, + 'dispose', + ); + + const manager = new HeapSnapshotManager(); + + // A path that passes into #loadSnapshot but fails on read. The worker is + // created before the read, so a failed load must still dispose it, + // otherwise it leaks for the life of the process (it is never added to the + // #snapshots map, so dispose()/disposeAll() cannot reach it). + await assert.rejects( + manager.getSnapshot('/nonexistent/does-not-exist.heapsnapshot'), + ); + + sinon.assert.calledOnce(disposeSpy); + }); +});