Playwright is a framework for Web Testing and Automation. It allows testing Chromium, Firefox and WebKit with a single API.
TypeScript automationchromechromiume2e-testingelectronend-to-end-testingfirefoxjavascriptplaywrighttesttest-automationtestingtesting-toolswebwebkit
fix(mcp): do not close the browser twice when the client disconnects (#43174)
Pavel Feldmand6158cdf637b
diff --git a/packages/playwright-core/src/tools/dashboard/dashboardApp.ts b/packages/playwright-core/src/tools/dashboard/dashboardApp.tsindex 5faa603223..08bb90c4c6 100644--- a/packages/playwright-core/src/tools/dashboard/dashboardApp.ts+++ b/packages/playwright-core/src/tools/dashboard/dashboardApp.ts@@ -397,6 +397,9 @@ async function runKillClient(): Promise<void> {async function runAnnotateClient(options: DashboardOptions): Promise<void> {selfDestructOnParentGone();+ // stdin is a pipe from the process that started this client. It emits 'close' only after it+ // has been read to the end, so read it. Unref it, so that it does not keep the process alive.+ process.stdin.resume().unref();const socketPath = dashboardSocketPath();const tryConnect = () => new Promise<net.Socket | undefined>(resolve => {diff --git a/packages/playwright-core/src/tools/utils/mcp/server.ts b/packages/playwright-core/src/tools/utils/mcp/server.tsindex 8be86c1206..28317f27d4 100644--- a/packages/playwright-core/src/tools/utils/mcp/server.ts+++ b/packages/playwright-core/src/tools/utils/mcp/server.ts@@ -188,9 +188,8 @@ function addServerListener(server: ServerType, event: 'close' | 'initialized', lexport async function start(serverBackendFactory: ServerBackendFactory, options: { host?: string; port?: number, allowedHosts?: string[], socketPath?: string } = {}) {if (options.port === undefined) {const transport = new StdioServerTransport();- // The SDK's StdioServerTransport doesn't detect peer disconnect — it never listens for stdin- // end-of-stream. Wire it up so callTool requests can be cancelled when the client goes away.- process.stdin.on('end', () => void transport.close());+ // Do not close the transport on stdin end. The exit watchdog closes the browsers on stdin+ // close, and closing a browser that is already closing force-kills it.await connect(serverBackendFactory, transport, Promise.resolve(), false);return;}diff --git a/tests/mcp/launch.spec.ts b/tests/mcp/launch.spec.tsindex 0b4e9599db..1678f43cb5 100644--- a/tests/mcp/launch.spec.ts+++ b/tests/mcp/launch.spec.ts@@ -317,3 +317,23 @@ exit 1error: expect.not.stringContaining(`Browser is already in use`),});});++test('should gracefully close the browser once on stdin end', {+ annotation: { type: 'issue', description: 'https://github.com/microsoft/playwright/issues/43098' }+}, async ({ startClient, server }, testInfo) => {+ const { client, stderr } = await startClient({+ args: [`--user-data-dir=${testInfo.outputPath('user-data-dir')}`],+ env: { DEBUG: 'pw:browser' },+ });+ await client.callTool({+ name: 'browser_navigate',+ arguments: { url: server.HELLO_WORLD },+ });++ await client.close();++ await expect.poll(() => stderr()).toContain('<gracefully close end>');+ expect(stderr()).toContain('<gracefully close start>');+ expect(stderr()).not.toContain('<forcefully close>');+ expect(stderr()).not.toContain('<will force kill>');+});