Skip to content

Playwright is a framework for Web Testing and Automation. It allows testing Chromium, Firefox and WebKit with a single API.

fix(dashboard): fix Windows named pipe race on daemon shutdown (#40642)

Simon Knottdd468b4ec61c
diff --git a/packages/playwright-core/src/tools/dashboard/dashboardApp.ts b/packages/playwright-core/src/tools/dashboard/dashboardApp.ts
index b4a8a1a962..34b5bd4cfa 100644
--- a/packages/playwright-core/src/tools/dashboard/dashboardApp.ts
+++ b/packages/playwright-core/src/tools/dashboard/dashboardApp.ts
@@ -22,6 +22,7 @@ import http from 'http';
import { HttpServer } from '@utils/httpServer';
import { makeSocketPath } from '@utils/fileUtils';
import { gracefullyProcessExitDoNotHang } from '@utils/processLauncher';
+import { monotonicTime } from '@isomorphic/time';
import { libPath } from '../../package';
import { playwright } from '../../inprocess';
import { findChromiumChannelBestEffort, registryDirectory } from '../../server/registry/index';
@@ -277,9 +278,7 @@ async function acquireSingleton(options: DashboardOptions): Promise<net.Server>
const server = net.createServer();
server.listen(socketPath, () => resolve(server));
server.on('error', (err: NodeJS.ErrnoException) => {
- const isInUse = err.code === 'EADDRINUSE'
- || (process.platform === 'win32' && err.code === 'EBUSY');
- if (!isInUse)
+ if (err.code !== 'EADDRINUSE')
return reject(err);
const client = net.connect(socketPath, () => {
client.write(JSON.stringify(options) + '\n');
@@ -342,16 +341,21 @@ export async function openDashboardApp() {
socket.end();
return;
}
+ if (parsed.kill) {
+ // Write our PID so the kill client can wait for the process to fully exit,
+ // which guarantees the named pipe is released (especially on Windows).
+ // Start graceful shutdown only after the socket data has been flushed, so the
+ // kill client is guaranteed to receive the PID before we begin tearing down.
+ server?.close();
+ socket.end(JSON.stringify({ pid: process.pid }) + '\n', () => gracefullyProcessExitDoNotHang(0));
+ return;
+ }
void statePromise.then(({ page, server: dashboard }) => {
if (parsed.annotate) {
page?.bringToFront().catch(() => {});
dashboard.reveal(parsed);
dashboard.triggerAnnotate();
dashboard.registerAnnotateWaiter(socket);
- } else if (parsed.kill) {
- server?.close();
- socket.end();
- gracefullyProcessExitDoNotHang(0);
} else {
page?.bringToFront().catch(() => {});
dashboard.reveal(parsed);
@@ -381,14 +385,37 @@ export async function openDashboardForContext(context: api.BrowserContext): Prom
async function runKillClient(): Promise<void> {
const socketPath = dashboardSocketPath();
- await new Promise<void>(resolve => {
+ const pid = await new Promise<number | undefined>((resolve, reject) => {
const client = net.connect(socketPath);
+ let data = '';
client.once('connect', () => {
client.write(JSON.stringify({ kill: true }) + '\n');
});
- client.once('end', () => resolve());
- client.once('error', () => resolve());
+ client.on('data', chunk => { data += chunk; });
+ client.once('end', () => {
+ let pid: number | undefined;
+ try { pid = JSON.parse(data.trim()).pid; } catch { }
+ if (pid === undefined)
+ reject(new Error('Dashboard did not return its PID'));
+ else
+ resolve(pid);
+ });
+ client.once('error', () => resolve(undefined));
});
+ if (pid === undefined)
+ return;
+ // Poll until the daemon process exits — at that point the OS has released all
+ // its handles, including the named pipe, so the next acquisition won't see a stale pipe.
+ const deadline = monotonicTime() + 35000;
+ while (monotonicTime() < deadline) {
+ try {
+ process.kill(pid, 0);
+ } catch {
+ return;
+ }
+ await new Promise(r => setTimeout(r, 50));
+ }
+ throw new Error(`Dashboard process ${pid} did not exit within the deadline`);
}
async function runAnnotateClient(options: DashboardOptions): Promise<void> {
diff --git a/tests/mcp/cli-fixtures.ts b/tests/mcp/cli-fixtures.ts
index 4c7413f228..1a5f1e79fa 100644
--- a/tests/mcp/cli-fixtures.ts
+++ b/tests/mcp/cli-fixtures.ts
@@ -60,7 +60,7 @@ export const test = baseTest.extend<{
return page;
});
},
- connectToDashboard: async ({ cli, playwright }, use) => {
+ connectToDashboard: [async ({ cli, playwright }, use) => {
await use(async (bindTitle: string) => {
let endpoint = '';
await expect(async () => {
@@ -72,7 +72,7 @@ export const test = baseTest.extend<{
return await playwright.chromium.connect(endpoint);
});
await cli('show', '--kill');
- },
+ }, { timeout: 60000 }],
cli: async ({ mcpBrowser, mcpHeadless, childProcess }, use) => {
await fs.promises.mkdir(test.info().outputPath('.playwright'), { recursive: true });