Chrome DevTools for coding agents
fix(memory): dispose the heap-snapshot worker when loading fails (#2449)
bassem chagra744738deaaa9
diff --git a/src/HeapSnapshotManager.ts b/src/HeapSnapshotManager.tsindex 0d01cf88..ae017ed1 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<DevTools.HeapSnapshotModel.HeapSnapshotProxy.HeapSnapshotProxy>();+ try {+ const {promise: snapshotPromise, resolve: resolveSnapshot} =+ Promise.withResolvers<DevTools.HeapSnapshotModel.HeapSnapshotProxy.HeapSnapshotProxy>();- 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<DuplicateStringGroup[]> {diff --git a/tests/HeapSnapshotManager.test.ts b/tests/HeapSnapshotManager.test.tsnew file mode 100644index 00000000..ee5cb259--- /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);+ });+});