fix: actually terminate remote browser session in browser_close for CDP-connected browsers#56
Open
jonnyparris wants to merge 2 commits into
Open
fix: actually terminate remote browser session in browser_close for CDP-connected browsers#56jonnyparris wants to merge 2 commits into
jonnyparris wants to merge 2 commits into
Conversation
…ted browsers When connected to a remote Chromium via CDP, Playwright's browser.close() only disconnects the local WebSocket — the remote browser keeps running until the remote service's keep-alive timeout expires. Override the close path in CdpContextFactory to first send the CDP Browser.close command (which terminates the remote instance) before disconnecting. Also update the browser_close tool description from 'Close the page' to reflect what it actually does.
cfa3212 to
90d2217
Compare
ruifigueira
requested changes
Apr 22, 2026
…lose Address review feedback: keep browser_close behaviour unchanged (matches upstream Microsoft semantics — 'close the page', tear down the MCP context) and add a separate browser_terminate tool that explicitly sends CDP Browser.close to release remote CDP-backed sessions (e.g. Cloudflare Browser Rendering) instead of waiting for the keep-alive to expire. The CDP Browser.close path now lives on an optional terminate() hook exposed by CreateContextResult. Only CdpContextFactory implements it; for every other transport browser_terminate falls back to the normal close path, so there is no behaviour change for persistent, isolated, remote, or browser-server modes.
Member
Author
|
Good point — revised with option A.
Implementation:
Test in |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
When this MCP is connected to a remote Chromium via CDP (via the
cdpEndpointconfig), callingbrowser_closedoesn't actually close the remote browser — it just disconnects the MCP-side WebSocket. The remote browser keeps running until the underlying service's keep-alive timeout expires.Playwright's own docs confirm this:
So
browser_closeis effectively a no-op against the remote in CDP mode. Today it's also described as "Close the page" which further undersells what the tool is supposed to do.Fix
Override the close path in
CdpContextFactoryso it:browser.newBrowserCDPSession()Browser.closeCDP command, which terminates the remote Chromium instancebrowserContext.close()+browser.close()disconnectOnly
CdpContextFactoryis affected —IsolatedContextFactory,PersistentContextFactory,RemoteContextFactory, andBrowserServerContextFactorykeep their existing behaviour because theirbrowser.close()semantics are already correct for their respective transports.Also updated the
browser_closetool description from"Close the page"to describe what the tool actually does.Tests
Added a test in
tests/cdp.spec.tsthat:--cdp-endpointbrowser_closedisconnectedevent within 5s (i.e. the process is actually gone, not just our WS dropped)Passes on
chromeandchromium. Skipped on firefox/webkit (CDP is Chromium-only). All existing CDP and core tests continue to pass.Note
This is a standalone bug fix — the tool's observable behaviour should match its name and description. Whether the remote endpoint honours the CDP
Browser.closecommand is an orthogonal concern of whatever serves the CDP endpoint.