Skip to content

fix: load source maps on demand instead of for every parsed script - #2463

Closed
Dmitriusan wants to merge 1 commit into
ChromeDevTools:mainfrom
Dmitriusan:fix/lazy-devtools-universe
Closed

Dmitriusan wants to merge 1 commit into
ChromeDevTools:mainfrom
Dmitriusan:fix/lazy-devtools-universe

Conversation

@Dmitriusan

@Dmitriusan Dmitriusan commented Aug 3, 2026 •

Copy link
Copy Markdown
Contributor

Replaces the original contents of this PR. That version made the DevTools universe lazy, which @OrKoN pointed out collides with #1777 and, as it turns out, treated the symptom anyway: the cost is per page whether or not the universe is created eagerly. This version fixes the cost itself and has no bearing on how many pages get a universe.

Root cause and measurements are in #2431 (comment).

Problem

DebuggerModel.parsedScriptSource() calls attachSourceMap() for every script that carries a sourceMapURL, because SourceMapManager defaults to enabled. Each map is downloaded, VLQ decoded, sorted, reverse mapped, and the decoded mappings are retained for as long as the model lives. On one Jira issue page that is 147 maps and 2427 MB retained, measured with a full GC forced every second. createPagesSnapshot() builds an McpPage for every page in the browser, so this is paid per open tab.

Change

The universe only needs source maps to symbolicate the frames of a stack trace it is about to format, and createStackTrace() already walks exactly those scripts. So:

  • Developer resource loads are refused by default. Both channels have to be gated: PageResourceLoader.dispatchLoad() falls back from the CDP loader to the host bindings, so gating one alone reroutes the download instead of preventing it. That is PuppeteerDevToolsConnection.send and McpHostBindingAdapter.loadNetworkResource.
  • createStackTrace() allows the URL for a script it needs, then does detachSourceMap() + attachSourceMap() on that one script, which makes the manager run the load it skipped.
  • It then awaits sourceMap.waitForScopeInfo(). Without that the frames resolve to positions but the function names come back empty, which is easy to miss.
  • A map that already failed to load is not retried.

SourceMapManager.setEnabled(false) is not usable for this: setEnabled(true) re-attaches every registered client at once, so toggling it around a symbolication brings the whole avalanche back.

Results

Fixture is a rollup bundle with a real source map back to src/app.js; the large one has a 2.8 MB map. Memory is measured against a real browser with 18 ordinary tabs open (Teams, Jira, Gmail, a few SPAs), one list_pages call and then no tool calls at all.

main this branch
RSS OOM at 4.9 GB in 65 s 210.1 -> 210.3 MB over 120 s
stack trace deliberatelyFail (app.js:2:9) … byte-identical
get_console_message, 2.8 MB map, 3 runs 1893 / 1882 / 2340 ms 133 / 139 / 142 ms
npm run test baseline no new failures

Two things worth calling out. The memory result holds with every one of the 18 pages still getting a universe, so this does not depend on the lazy-universe idea at all. And symbolication came out roughly 14x faster rather than slower, which I did not expect; I have not dug into why the eager path is slow, so treat that number as an observation rather than a claim about the mechanism.

Notes for review

  • The allowlist in sourceMapGate.ts is module state. Per-context would be tidier if you would rather have it on McpContext, I kept it standalone so the two gate points do not need a context reference.
  • I first put the fallback gate in McpContext.loadResource and it broke eight tests, correctly: that method is a public API with its own allowlist, blocklist, file: and path-validation semantics. It lives in the host binding adapter instead.
  • Inline data: source maps never reach the loader and are unaffected.
  • Separately, and not fixed here: detaching a CDP session while a source map is loading throws an uncaught Error: Unknown session <id> out of PuppeteerDevToolsConnection.send and kills the process. send() is declared to return a promise but throws synchronously, so the generated agent code never catches it. Happy to send that as its own PR.

@google-cla

google-cla Bot commented Aug 3, 2026

Copy link
Copy Markdown

Thanks for your pull request! It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA).

View this failed invocation of the CLA check for more information.

For the most up to date status, view the checks section at the bottom of the pull request.

Comment thread src/McpContext.ts Outdated
// Start the DevTools universe for the page we are about to work with, so
// that stack traces can be symbolicated by the time a response is
// formatted. Pages that are never selected never pay for it.
void newPage.ensureDevToolsUniverse();

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.

with #1777 we plan to allow agents to concurrently work on different pages, so I think we would need to find a different solution cc @yulunz

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

@OrKoN fyi I've changed the approach, so I hope this issue now has a chance to be resolved

`DebuggerModel.parsedScriptSource()` attaches a source map to every
script that has one, and the decoded mappings are retained for the life
of the model. On a script-heavy app that is hundreds of downloads and
gigabytes of retained mappings per page, and `createPagesSnapshot()`
builds an `McpPage` for every page in the browser, so the cost is paid
per open tab.

The universe only needs source maps to symbolicate the frames of a stack
trace it is about to format, and `createStackTrace()` already walks
exactly those scripts. So refuse developer resource loads by default and
attach the map for a single script when it is needed.

Both load channels have to be gated. `PageResourceLoader.dispatchLoad()`
falls back from the CDP loader to the host bindings, so gating only one
of them reroutes the download rather than preventing it.

`SourceMapManager.setEnabled(false)` is not usable here, because
`setEnabled(true)` re-attaches every registered client at once.

Measured against a browser with 18 ordinary tabs open, one `list_pages`
call and then no tool calls at all:

  RSS                          OOM at 4.9 GB in 65 s -> 210.1..210.3 MB
  stack trace                  byte-identical
  get_console_message, 2.8 MB map, 3 runs
                               1893/1882/2340 ms -> 133/139/142 ms

Fixes ChromeDevTools#2431
@Dmitriusan
Dmitriusan force-pushed the fix/lazy-devtools-universe branch from 07bff11 to 8b44a31 Compare August 3, 2026 21:11
@Dmitriusan Dmitriusan changed the title fix: create the DevTools universe on demand instead of per page fix: load source maps on demand instead of for every parsed script Aug 3, 2026
@OrKoN
OrKoN requested a review from szuend August 4, 2026 05:27
@szuend

szuend commented Aug 4, 2026

Copy link
Copy Markdown
Contributor

I'd like to push back on the approach taken in this PR. Sniffing on the CDP traffic and then massaging certain results opens pandoras' box. Most of the test coverage of DevTools/Universe related code lives in the devtools-frontend repository and this PR changes fundamental behaviors that have zero test coverage.

I agree that the MCP server needs lazy loading of source maps (in contrast to DevTools, which needs them eagerly), but the right solution is to fix this upstream by adding a config option to the Universe constructor and wire that to the appropriate places.

pull Bot pushed a commit to Spencerx/chrome-devtools-mcp that referenced this pull request Aug 4, 2026
…Tools#2466)

`PuppeteerDevToolsConnection.send()` is declared to return a promise of
a result or an error, but it throws synchronously when the session id no
longer resolves. The generated agent code does not catch that, so it
escapes as an uncaught exception and ends the process.

A session going away while commands for it are in flight is normal
rather than exceptional. The way I kept hitting it is closing a page
while its source maps are still loading:

```
Error: Unknown session 5C39E3164EE6C2C4AB1D3788F5AE03DA
    at PuppeteerDevToolsConnection.send (build/src/devtools/DevToolsConnectionAdapter.js:31:19)
    at AgentPrototype.invoke [as invoke_close]
    at IOModel.close
    at PageResourceLoader.loadFromTarget
    at async PageResourceLoader.dispatchLoad
    at async PageResourceLoader.loadResource
    at async loadSourceMap
```

Source maps are just the most likely way to be holding a load open long
enough to notice; any in-flight command to a session that goes away does
the same thing. In the server this means closing a tab at the wrong
moment can take everything down.

This returns an error response instead, which is how a failed command is
already reported a few lines below, so callers see it through the path
they already handle.

The other synchronous throw in that method, for `sessionId ===
undefined`, is left alone: it guards an invariant the callers are
supposed to uphold, so failing loudly there seems right. Happy to change
that too if you would rather have both consistent.

The test fails on `main` with `Error: Unknown session
session-that-went-away` and passes with the change.

Found while working on ChromeDevTools#2431, but unrelated to it and independent of
ChromeDevTools#2463.
@Lightning00Blade

Copy link
Copy Markdown
Collaborator

@yulunz with #2486 can we close this one?

@yulunz

yulunz commented Aug 11, 2026

Copy link
Copy Markdown
Collaborator

lazy loading is implemented on the upstream and the mcp is lazy loading by default now. Thanks for the suggestion! @Dmitriusan

@yulunz yulunz closed this Aug 11, 2026
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.

5 participants