Skip to content

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

chore(dashboard): report daemon PID via reveal ack (#40906)

Simon Knottde13224ffb2a
diff --git a/packages/playwright-core/src/tools/cli-client/program.ts b/packages/playwright-core/src/tools/cli-client/program.ts
index 1eb3e7c4a6..86a3db49e6 100644
--- a/packages/playwright-core/src/tools/cli-client/program.ts
+++ b/packages/playwright-core/src/tools/cli-client/program.ts
@@ -244,13 +244,17 @@ export async function program(options?: { embedderVersion?: string}) {
}
const timer = setTimeout(() => child.stdin!.destroy(), 60_000);
child.unref();
+ let daemonPid: number;
try {
await new Promise<void>((resolve, reject) => {
let outLog = '';
child.stdout!.on('data', data => {
outLog += data.toString();
- if (outLog.includes('Dashboard is running'))
+ const match = outLog.match(/Dashboard is running pid=(\d+)/);
+ if (match) {
+ daemonPid = Number(match[1]);
resolve();
+ }
});
child.once('exit', (code, signal) => reject(new Error(`Dashboard daemon exited (code=${code}, signal=${signal}) before signaling READY${outLog ? '\n' + outLog : ''}`)));
});
@@ -260,7 +264,7 @@ export async function program(options?: { embedderVersion?: string}) {
child.stdin!.destroy();
child.stdout!.destroy();
}
- output.show(sessionName, child.pid);
+ output.show(sessionName, daemonPid!);
return;
}
default: {
diff --git a/packages/playwright-core/src/tools/dashboard/dashboardApp.ts b/packages/playwright-core/src/tools/dashboard/dashboardApp.ts
index 892b9e9df3..7515e7b38a 100644
--- a/packages/playwright-core/src/tools/dashboard/dashboardApp.ts
+++ b/packages/playwright-core/src/tools/dashboard/dashboardApp.ts
@@ -253,25 +253,38 @@ function parseOpenArgs(): DashboardOptions {
};
}
-async function acquireSingleton(options: DashboardOptions): Promise<net.Server> {
+type AcquireResult =
+ | { role: 'winner', server: net.Server }
+ | { role: 'loser', daemonPid: number };
+
+async function acquireSingleton(options: DashboardOptions): Promise<AcquireResult> {
const socketPath = dashboardSocketPath();
if (process.platform !== 'win32')
await fs.promises.mkdir(path.dirname(socketPath), { recursive: true });
return await new Promise((resolve, reject) => {
const server = net.createServer();
- server.listen(socketPath, () => resolve(server));
+ server.listen(socketPath, () => resolve({ role: 'winner', server }));
server.on('error', (err: NodeJS.ErrnoException) => {
if (err.code !== 'EADDRINUSE')
return reject(err);
+ let ackBuffer = '';
const client = net.connect(socketPath, () => {
client.write(JSON.stringify(options) + '\n');
});
- client.on('end', () => reject(new Error('already running')));
+ client.on('data', chunk => { ackBuffer += chunk.toString(); });
+ client.on('end', () => {
+ try {
+ const { pid } = JSON.parse(ackBuffer.trim());
+ resolve({ role: 'loser', daemonPid: pid });
+ } catch (e) {
+ reject(e);
+ }
+ });
client.on('error', () => {
if (process.platform !== 'win32')
fs.unlinkSync(socketPath);
- server.listen(socketPath, () => resolve(server));
+ server.listen(socketPath, () => resolve({ role: 'winner', server }));
});
});
});
@@ -304,24 +317,23 @@ export async function openDashboardApp() {
// Self-destruct if the parent CLI dies before we signal READY. Unregistered
// before we signal so the daemon outlives the parent.
const stopSelfDestruct = selfDestructOnParentGone();
- let server: net.Server;
- try {
- server = await acquireSingleton(options);
- } catch {
+ const acquired = await acquireSingleton(options);
+ if (acquired.role === 'loser') {
// Another daemon is already running, signal success.
stopSelfDestruct();
// eslint-disable-next-line no-console
- console.log('Dashboard is running');
+ console.log(`Dashboard is running pid=${acquired.daemonPid}`);
// eslint-disable-next-line no-restricted-properties
await new Promise(f => process.stdout.write('', f)); // Make sure stdout is flushed.
return;
}
+ const { server } = acquired;
process.on('exit', () => server.close());
try {
await startApp(server, options);
stopSelfDestruct();
// eslint-disable-next-line no-console
- console.log('Dashboard is running');
+ console.log(`Dashboard is running pid=${process.pid}`);
} catch (error) {
// eslint-disable-next-line no-console
console.log(error);
@@ -366,7 +378,7 @@ async function startApp(server: net.Server, options: DashboardOptions) {
try {
await page?.bringToFront();
await dashboard.reveal(parsed);
- socket.end();
+ socket.end(JSON.stringify({ pid: process.pid }) + '\n');
} catch (e) {
socket.end(e);
}
diff --git a/tests/mcp/dashboard.spec.ts b/tests/mcp/dashboard.spec.ts
index b10a2ec4df..eda77af07c 100644
--- a/tests/mcp/dashboard.spec.ts
+++ b/tests/mcp/dashboard.spec.ts
@@ -178,3 +178,10 @@ test('save recording streams WebM bytes to the chosen file', async ({ cli, serve
// WebM files start with the EBML magic bytes.
expect(bytes.subarray(0, 4)).toEqual(Buffer.from([0x1a, 0x45, 0xdf, 0xa3]));
});
+
+test('two concurrent cli show invocations both succeed', async ({ cli }) => {
+ const bindTitle = `--playwright-internal--${crypto.randomUUID()}`;
+ const [first, second] = await Promise.all([cli('show', { bindTitle }), cli('show', { bindTitle })]);
+ expect(first.dashboardPid).toBe(second.dashboardPid);
+ await cli('show', '--kill');
+});