Skip to content

fix: prune destroyed extension service workers on reload (#2978) - #3116

Closed
trukhinyuri wants to merge 1 commit into
ChromeDevTools:mainfrom
trukhinyuri:fix/2978-extensions-prune-destroyed-service-workers
Closed

trukhinyuri wants to merge 1 commit into
ChromeDevTools:mainfrom
trukhinyuri:fix/2978-extensions-prune-destroyed-service-workers

Conversation

@trukhinyuri

Copy link
Copy Markdown
Contributor

Fixes #2978

Summary

  • Query authoritative Target.getTargets from the browser CDP session in src/McpContext.ts to prune destroyed extension service worker targets that do not emit target destruction events on reload.
  • Add regression coverage in tests/tools/extensions.test.ts, tests/tools/script.test.ts, and tests/tools/console.test.ts.

@OrKoN
OrKoN requested a review from samiyac October 14, 2026 08:10
@OrKoN

OrKoN commented Oct 14, 2026

Copy link
Copy Markdown
Collaborator

@samiyac, please review this PR (automatically distributed)

@OrKoN OrKoN 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.

Please check the following:

  1. Fix the bug in Puppeteer (ChromeTargetManager) so browser.targets() drops the old extension service worker after browser.installExtension() re-installs the same extension, and emits targetdestroyed for it; then roll the Puppeteer version here. AGENTS.md requires upstream fixes first, and createWorkersSnapshot() on main already prunes against browser.targets() (#2800), so a correct browser.targets() fixes #2978 with no change in this repository.

  2. Remove the Target.getTargets round trip and the writes to Puppeteer internals (browser._connection, browser._targetManager().getAvailableTargets().delete(...), browser.emit('targetdestroyed', ...), target._targetId) from #pruneDestroyedServiceWorkers(). The repository rules forbid @ts-expect-error, as unknown as, and Puppeteer internals outside src/third_party/index.ts; mutating TargetManager state from outside also races with Puppeteer's own targetDestroyed handling.

  3. Keep createWorkersSnapshot() synchronous. Making it async adds a CDP round trip to every McpResponse.handle() that includes workers and changes the signature for every caller (#init, collectors, tests) for a workaround that request 1 makes unnecessary.

  4. In tests/tools/extensions.test.ts, replace the fixed setTimeout(…, 2000) with a poll on context.createWorkersSnapshot().length === 1, and put the real-browser test in a file that already launches a browser (for example tests/McpContext.test.ts or tests/tools/script.test.ts) instead of adding withMcpContext to the mock-based extensions.test.ts. A fixed sleep is flaky, and mock-based tool-handler test files stay browser-free.

  5. Rebase onto main; the PR is DIRTY. McpContext.ts changed in #2881, #3156, #3161, and #3162 around createWorkersSnapshot() and the collector initialization.

@trukhinyuri

Copy link
Copy Markdown
Contributor Author

@OrKoN Makes total sense, thank you! I opened upstream puppeteer/puppeteer#15614 so CdpBrowser.installExtension() destroys any pre-existing service_worker target for that extension ID (matching uninstallExtension()) and removes it from browser.targets().

Once puppeteer/puppeteer#15614 lands and Puppeteer rolls here, I will rebase this PR onto main, drop #pruneDestroyedServiceWorkers() so createWorkersSnapshot() stays synchronous, and move the integration test to tests/McpContext.test.ts polling createWorkersSnapshot() instead of using a fixed timeout.

@trukhinyuri

Copy link
Copy Markdown
Contributor Author

Closing this PR for now until the next Puppeteer release rolls into this repository with the upstream fix from puppeteer/puppeteer. I will reopen with the integration test once the roll lands.

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.

List_pages keeps listing destroyed extension service workers after reload_extension, and their ids hang evaluate_script

2 participants