Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 25 additions & 16 deletions src/HeapSnapshotManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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[]> {
Expand Down
39 changes: 39 additions & 0 deletions tests/HeapSnapshotManager.test.ts
Original file line number Diff line number Diff line change
@@ -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);
});
});