Skip to content

Commit bae3292

Browse files
authored
fix(cli): stop daemon when socket disappears (#42454)
detached browser daemons stay alive after losing their control socket monitor the socket identity and exit gracefully when it disappears or is replaced fixes <#42428>
1 parent a89041e commit bae3292

2 files changed

Lines changed: 58 additions & 0 deletions

File tree

‎packages/playwright-core/src/tools/cli-daemon/daemon.ts‎

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,29 @@ async function socketExists(socketPath: string): Promise<boolean> {
4545
return false;
4646
}
4747

48+
async function monitorSocketPath(socketPath: string): Promise<void> {
49+
if (process.platform === 'win32')
50+
return;
51+
52+
const socketStat = await fs.promises.stat(socketPath);
53+
54+
async function checkSocketPath() {
55+
const currentStat = await fs.promises.stat(socketPath).catch(() => undefined);
56+
if (!currentStat || !currentStat.isSocket() || currentStat.dev !== socketStat.dev || currentStat.ino !== socketStat.ino) {
57+
gracefullyProcessExitDoNotHang(0);
58+
return;
59+
}
60+
scheduleSocketCheck();
61+
}
62+
63+
function scheduleSocketCheck() {
64+
const timer = setTimeout(() => void checkSocketPath(), 1000);
65+
timer.unref();
66+
}
67+
68+
scheduleSocketCheck();
69+
}
70+
4871
export async function startCliDaemonServer(
4972
sessionName: string,
5073
browserContext: playwright.BrowserContext,
@@ -118,6 +141,7 @@ export async function startCliDaemonServer(
118141
});
119142

120143
await saveSessionFile(clientInfo, sessionConfig);
144+
await monitorSocketPath(socketPath);
121145
return socketPath;
122146
}
123147

‎tests/mcp/cli-session.spec.ts‎

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,21 @@
1515
*/
1616

1717
import fs from 'fs';
18+
import os from 'os';
1819
import path from 'path';
1920
import { test, expect, daemonFolder } from './cli-fixtures';
2021
import { killProcessGroup } from '../config/commonFixtures';
2122
import playwright from '../../packages/playwright-core';
2223

24+
function isProcessAlive(pid: number): boolean {
25+
try {
26+
process.kill(pid, 0);
27+
return true;
28+
} catch {
29+
return false;
30+
}
31+
}
32+
2333
test('list', async ({ cli, server }) => {
2434
const { output: emptyOutput } = await cli('list');
2535
expect(emptyOutput).toContain('(no browsers)');
@@ -110,6 +120,30 @@ test('session stops when browser exits', async ({ cli, server }) => {
110120
expect(listAfter).toContain('(no browsers)');
111121
});
112122

123+
test('session stops when temporary socket directory disappears', async ({ cli, server }) => {
124+
test.skip(process.platform === 'win32');
125+
126+
// Keep this short because macOS limits Unix socket paths to 103 bytes.
127+
const tempDir = await fs.promises.mkdtemp(path.join(os.tmpdir(), 'p-'));
128+
const removedTempDir = tempDir + '.removed';
129+
try {
130+
const { daemonPid } = await cli('open', server.HELLO_WORLD, {
131+
env: {
132+
PWTEST_SOCKETS_DIR: '',
133+
TMPDIR: tempDir,
134+
},
135+
});
136+
expect(daemonPid).toBeDefined();
137+
expect(isProcessAlive(daemonPid)).toBe(true);
138+
139+
await fs.promises.rename(tempDir, removedTempDir);
140+
await expect.poll(() => isProcessAlive(daemonPid)).toBe(false);
141+
} finally {
142+
await fs.promises.rm(tempDir, { force: true, recursive: true });
143+
await fs.promises.rm(removedTempDir, { force: true, recursive: true });
144+
}
145+
});
146+
113147
test('session reopen with different config', async ({ cli, server }, testInfo) => {
114148
const config = { browser: { contextOptions: { viewport: { width: 700, height: 500 } } } };
115149
const configPath = testInfo.outputPath('config.json');

0 commit comments

Comments
 (0)