Skip to content

fix: dispose heap snapshot workers on context teardown - #2428

Merged
Lightning00Blade merged 3 commits into
ChromeDevTools:mainfrom
gunjanjaswal:fix/dispose-heap-snapshot-workers-on-teardown
Jul 29, 2026
Merged

Lightning00Blade merged 3 commits into
ChromeDevTools:mainfrom
gunjanjaswal:fix/dispose-heap-snapshot-workers-on-teardown

Conversation

@gunjanjaswal

Copy link
Copy Markdown
Contributor

Heap snapshot worker threads (Node worker threads, independent of the browser) and their parsed snapshots are only freed per file, through the close_heapsnapshot tool. On context teardown and on browser reconnect they leak.

McpContext.dispose() cleans up the service-worker console collector and each McpPage, but never touches the HeapSnapshotManager, so any snapshot loaded during the session keeps its worker alive. And getContext() in index.ts replaces the previous context on reconnect (if (context?.browser !== browser)) without ever calling dispose() on the old one, so every reconnect strands the previous context's workers and retained snapshots. Repeated reconnects accumulate the leak.

This adds HeapSnapshotManager.disposeAll() (terminates every cached worker and clears the map, mirroring the existing per-file dispose), calls it from McpContext.dispose(), and disposes the outgoing context before it is replaced on reconnect. A regression test in McpContext.test.ts loads a fixture snapshot and asserts it is freed after dispose().

Verified on Node 24 (the .nvmrc version): typecheck, eslint, and the affected test files pass, and npm run gen leaves the tree clean, so there is no generated-files drift. Reverting the source makes the new test fail.

Heap snapshot worker threads and their parsed snapshots were only ever
freed per file through the close_heapsnapshot tool. On context teardown
and on browser reconnect they leaked: McpContext.dispose() never touched
the HeapSnapshotManager, and getContext() replaced the previous context
on reconnect without disposing it at all.

Add HeapSnapshotManager.disposeAll() to terminate every cached worker
and clear the map, call it from McpContext.dispose(), and dispose the
old context before replacing it on reconnect so repeated reconnects no
longer accumulate worker threads and retained snapshots.
@OrKoN
OrKoN requested a review from Lightning00Blade July 27, 2026 13:21

@Lightning00Blade Lightning00Blade left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the PR!

I think it make sense adding the dispose on context re-recreation.
I think we should also add test for that if possible as it's the more important part of the code change.

@gunjanjaswal

Copy link
Copy Markdown
Contributor Author

Good call, added a test that covers the reconnect path directly. It loads a snapshot, disposes the context the way getContext() does before replacing it on reconnect, then asserts the cached worker thread actually gets terminated (it spies on the worker proxy dispose and calls through, so it is the real teardown rather than a mock). Reverting the disposeAll() line makes it fail, so it is pinned to the fix.

@OrKoN
OrKoN requested a review from Lightning00Blade July 28, 2026 08:08
Comment thread tests/McpContext.test.ts Resolved
Comment thread src/index.ts Resolved

@Lightning00Blade Lightning00Blade left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, will approve after the comments are fixed.

Remove the heap-snapshot-worker-on-reconnect test (it mimics the mechanism
rather than exercising it, and there is no infra to drive it here) and the
now-redundant comment on the context dispose call.
@gunjanjaswal

Copy link
Copy Markdown
Contributor Author

Both done. Dropped the reconnect worker test (fair point, without the infra it was mirroring the code rather than exercising it) and removed the comment on the dispose call. The teardown test that checks snapshots are freed stays. typecheck, eslint, and gen are clean.

@Lightning00Blade Lightning00Blade left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, thanks!

@gunjanjaswal

Copy link
Copy Markdown
Contributor Author

Thanks for the review. Quick note on the red: the macOS / node 22 job failed on one test, pages.test.js "list pages for extension service workers", hitting the 30s Puppeteer timeout. It is unrelated to this change (which only dropped a test and a comment), it is green on the other OS/node jobs here, and that macOS job has been flaky across other PRs too. Should clear on a re-run.

@Lightning00Blade
Lightning00Blade added this pull request to the merge queue Jul 29, 2026
Merged via the queue into ChromeDevTools:main with commit f245c76 Jul 29, 2026
27 of 29 checks passed
@gunjanjaswal

Copy link
Copy Markdown
Contributor Author

Thanks for the review and merge, @Lightning00Blade. Good call trimming that reconnect test down to what the infra can actually exercise.

@gunjanjaswal
gunjanjaswal deleted the fix/dispose-heap-snapshot-workers-on-teardown branch July 29, 2026 11:59
pull Bot pushed a commit to CrazyForks/chrome-devtools-mcp that referenced this pull request Aug 7, 2026
…omeDevTools#2449)

Fixes ChromeDevTools#2448

## Problem

`HeapSnapshotManager.#loadSnapshot` creates a `HeapSnapshotWorkerProxy`
(a live worker) before reading the file, but the manager only disposes
workers tracked in the `#snapshots` map. `getSnapshot` adds the entry
only on success, so a failed load — e.g. a missing or invalid
`.heapsnapshot` path passed to any `get_heapsnapshot_*` tool — throws
before the entry exists, and neither `dispose()` nor `disposeAll()`
(added in ChromeDevTools#2428) can reach the worker. It leaks for the life of the
process, one per failed load.

## Fix

Wrap the load body in `try/catch` and `workerProxy.dispose()` before
rethrowing, so a failed load cleans up its own worker.

## Testing

- Added `tests/HeapSnapshotManager.test.ts`: spies on
`HeapSnapshotWorkerProxy.prototype.dispose`, forces a load failure via a
nonexistent path, and asserts the worker was disposed. Fails on `main`
(`dispose` called 0 times) and passes with this change.
- `npm run test tests/HeapSnapshotManager.test.ts
tests/tools/memory.test.ts` — all pass.
- ESLint + Prettier clean.
OrKoN pushed a commit to LuHuangMSFT/chrome-devtools-mcp that referenced this pull request Aug 10, 2026
🤖 I have created a release *beep* *boop*
---


##
[1.7.0](ChromeDevTools/chrome-devtools-mcp@chrome-devtools-mcp-v1.6.0...chrome-devtools-mcp-v1.7.0)
(2026-08-10)


### 🎉 Features

* add a utility function to check for localhost.
([ChromeDevTools#2454](ChromeDevTools#2454))
([c5ebf9e](ChromeDevTools@c5ebf9e))
* Add get_heapsnapshot_object_details MCP tool
([ChromeDevTools#2374](ChromeDevTools#2374))
([8432cb9](ChromeDevTools@8432cb9))
* Emit native contexts in snapshot summary
([ChromeDevTools#2375](ChromeDevTools#2375))
([f78a911](ChromeDevTools@f78a911))
* Filter heap snapshot objects by native context
([ChromeDevTools#2377](ChromeDevTools#2377))
([76fd242](ChromeDevTools@76fd242))
* **telemetry:** log devtools data.
([ChromeDevTools#2460](ChromeDevTools#2460))
([df2c753](ChromeDevTools@df2c753))
* **telemetry:** log is_devtools_open with each tool call.
([ChromeDevTools#2445](ChromeDevTools#2445))
([6f60eab](ChromeDevTools@6f60eab))
* **telemetry:** report whether tool call is made on localhost.
([ChromeDevTools#2455](ChromeDevTools#2455))
([6398b7e](ChromeDevTools@6398b7e))
* update lighthouse to 13.4.1
([ChromeDevTools#2398](ChromeDevTools#2398))
([745ffe7](ChromeDevTools@745ffe7))


### 🛠️ Fixes

* bound per-navigation network request retention
([ChromeDevTools#2435](ChromeDevTools#2435))
([b08c73c](ChromeDevTools@b08c73c))
* **cli:** validate session ids
([ChromeDevTools#2475](ChromeDevTools#2475))
([5ca9121](ChromeDevTools@5ca9121))
* **cli:** warn about version mismatch between cli and daemon
([ChromeDevTools#2461](ChromeDevTools#2461))
([3afc44d](ChromeDevTools@3afc44d))
* **daemon:** preserve hyphenated browser flags in arg serialization and
lazy daemon startup
([ChromeDevTools#2405](ChromeDevTools#2405))
([d79f3ba](ChromeDevTools@d79f3ba))
* dispose heap snapshot workers on context teardown
([ChromeDevTools#2428](ChromeDevTools#2428))
([f245c76](ChromeDevTools@f245c76))
* do not throw synchronously when a CDP session is gone
([ChromeDevTools#2466](ChromeDevTools#2466))
([ba4fe3e](ChromeDevTools@ba4fe3e))
* don't throw if Dialog was handled
([ChromeDevTools#2437](ChromeDevTools#2437))
([574c320](ChromeDevTools@574c320))
* downscale viewport screenshots when no viewport is emulated
([ChromeDevTools#2380](ChromeDevTools#2380))
([39c4140](ChromeDevTools@39c4140))
* improve daemon lifecycle
([ChromeDevTools#2360](ChromeDevTools#2360))
([3ee6a27](ChromeDevTools@3ee6a27))
* improve file writing
([ChromeDevTools#2447](ChromeDevTools#2447))
([1da4bb0](ChromeDevTools@1da4bb0))
* include the tab id in `get_tab_id`'s text response
([ChromeDevTools#2381](ChromeDevTools#2381))
([dcbaf49](ChromeDevTools@dcbaf49))
* **memory:** dispose the heap-snapshot worker when loading fails
([ChromeDevTools#2449](ChromeDevTools#2449))
([744738d](ChromeDevTools@744738d))
* **performance:** reset trace-running flag when start_trace setup fails
([ChromeDevTools#2420](ChromeDevTools#2420))
([40240c0](ChromeDevTools@40240c0))
* regression after the
[ChromeDevTools#2443](ChromeDevTools#2443)
([ChromeDevTools#2462](ChromeDevTools#2462))
([4c80ce4](ChromeDevTools@4c80ce4))
* rename maxRetainedSize column
([ChromeDevTools#2402](ChromeDevTools#2402))
([75c7048](ChromeDevTools@75c7048))
* **screenshot:** dispose element handle after take_screenshot
([ChromeDevTools#2422](ChromeDevTools#2422))
([fffb40e](ChromeDevTools@fffb40e))
* toggle lazy loading for source maps.
([ChromeDevTools#2486](ChromeDevTools#2486))
([8028bfe](ChromeDevTools@8028bfe))
* **wait:** avoid 180s mutex stall when a dialog opens during an action
([ChromeDevTools#2427](ChromeDevTools#2427))
([b4e8f74](ChromeDevTools@b4e8f74))


### 📄 Documentation

* Add devin cli install instruction to README
([ChromeDevTools#2361](ChromeDevTools#2361))
([45262c0](ChromeDevTools@45262c0))
* correct the user data directory documentation
([ChromeDevTools#2473](ChromeDevTools#2473))
([5ddbffd](ChromeDevTools@5ddbffd))
* **skills:** update memory leak debugging skill to use native MCP tools
([ChromeDevTools#2436](ChromeDevTools#2436))
([99bd90a](ChromeDevTools@99bd90a))
* Update Chrome requirement for categoryExperimentalWebmcp
([ChromeDevTools#2163](ChromeDevTools#2163))
([354458e](ChromeDevTools@354458e))
* update security.md
([ChromeDevTools#2362](ChromeDevTools#2362))
([5012b07](ChromeDevTools@5012b07))


### 🏗️ Refactor

* clean up McpResponse.handle
([ChromeDevTools#2392](ChromeDevTools#2392))
([348975d](ChromeDevTools@348975d))
* introduce Explicit resouce managent (using)
([ChromeDevTools#2443](ChromeDevTools#2443))
([5b25370](ChromeDevTools@5b25370))
* move tool groups
([ChromeDevTools#2365](ChromeDevTools#2365))
([2d944f9](ChromeDevTools@2d944f9))
* re-use Puppteer Mutex
([ChromeDevTools#2444](ChromeDevTools#2444))
([775ea2d](ChromeDevTools@775ea2d))
* rename methods for clarity
([ChromeDevTools#2442](ChromeDevTools#2442))
([dc23bcd](ChromeDevTools@dc23bcd))
* use a destructor to shorten the calls
([ChromeDevTools#2376](ChromeDevTools#2376))
([77e1d3f](ChromeDevTools@77e1d3f))

---
This PR was generated with [Release
Please](https://github.com/googleapis/release-please). See
[documentation](https://github.com/googleapis/release-please#release-please).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants