Repository navigation
fix: load source maps on demand instead of for every parsed script - #2463
Dmitriusan wants to merge 1 commit into
Conversation
|
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. |
| // 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(); |
There was a problem hiding this comment.
@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
07bff11 to
8b44a31
Compare
|
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 |
…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.
|
lazy loading is implemented on the upstream and the mcp is lazy loading by default now. Thanks for the suggestion! @Dmitriusan |
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()callsattachSourceMap()for every script that carries asourceMapURL, becauseSourceMapManagerdefaults 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 anMcpPagefor 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:PageResourceLoader.dispatchLoad()falls back from the CDP loader to the host bindings, so gating one alone reroutes the download instead of preventing it. That isPuppeteerDevToolsConnection.sendandMcpHostBindingAdapter.loadNetworkResource.createStackTrace()allows the URL for a script it needs, then doesdetachSourceMap()+attachSourceMap()on that one script, which makes the manager run the load it skipped.sourceMap.waitForScopeInfo(). Without that the frames resolve to positions but the function names come back empty, which is easy to miss.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), onelist_pagescall and then no tool calls at all.maindeliberatelyFail (app.js:2:9)…get_console_message, 2.8 MB map, 3 runsnpm run testTwo 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
sourceMapGate.tsis module state. Per-context would be tidier if you would rather have it onMcpContext, I kept it standalone so the two gate points do not need a context reference.McpContext.loadResourceand 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.data:source maps never reach the loader and are unaffected.Error: Unknown session <id>out ofPuppeteerDevToolsConnection.sendand 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.