From 97dbe5c3e81a3a6cc3168bffae20c550c0182248 Mon Sep 17 00:00:00 2001 From: Yury Semikhatsky Date: Wed, 28 Oct 2026 13:12:53 -0700 Subject: [PATCH 1/3] feat(cli): add PLAYWRIGHT_SOCKETS_DIR to choose the socket directory --- docs/src/getting-started-cli.md | 6 ++++++ packages/utils/fileUtils.ts | 6 +++--- tests/extension/extension-fixtures.ts | 2 +- tests/mcp/cli-fixtures.ts | 2 +- tests/mcp/cli-session.spec.ts | 2 +- 5 files changed, 12 insertions(+), 6 deletions(-) diff --git a/docs/src/getting-started-cli.md b/docs/src/getting-started-cli.md index 4f1e54f6cc7ad..6dd6efce243dd 100644 --- a/docs/src/getting-started-cli.md +++ b/docs/src/getting-started-cli.md @@ -255,6 +255,12 @@ playwright-cli -s=name delete-data # delete user data for a named session A headless session shuts itself down after an hour without commands, so a session an agent forgot to close does not keep a browser running. Headed and attached browsers are never closed automatically. Change the timeout with `open --idle-timeout=` or `timeouts.idle` in the config file, and pass `0` to disable it. +Sessions communicate through sockets in the system temporary directory. To keep the sockets in another directory, for example on a Linux machine shared with other users, set the `PLAYWRIGHT_SOCKETS_DIR` environment variable for all `playwright-cli` commands: + +```bash +export PLAYWRIGHT_SOCKETS_DIR=~/.cache/playwright-sockets +``` + ## Monitoring Use `playwright-cli show` to open a visual dashboard for observing and controlling all running browser sessions: diff --git a/packages/utils/fileUtils.ts b/packages/utils/fileUtils.ts index 213b546c14c58..16c7cbf0c5a16 100644 --- a/packages/utils/fileUtils.ts +++ b/packages/utils/fileUtils.ts @@ -149,17 +149,17 @@ const UNIX_SOCKET_PATH_MAX = 103; export function makeSocketPath(domain: string, name: string): string { const userNameHash = calculateSha1(process.env.USERNAME || process.env.USER || 'default').slice(0, 8); if (process.platform === 'win32') { - const socketsDir = process.env.PWTEST_SOCKETS_DIR; + const socketsDir = process.env.PLAYWRIGHT_SOCKETS_DIR; const suffix = socketsDir ? `-${calculateSha1(socketsDir).slice(0, 8)}` : ''; return `\\\\.\\pipe\\pw-${userNameHash}-${domain}-${name}${suffix}`; } - const baseDir = process.env.PWTEST_SOCKETS_DIR || path.join(os.tmpdir(), `pw-${userNameHash}`); + const baseDir = process.env.PLAYWRIGHT_SOCKETS_DIR || path.join(os.tmpdir(), `pw-${userNameHash}`); const dir = path.join(baseDir, domain); let result = path.join(dir, sanitizeForFilePath(name) + '.sock'); if (Buffer.byteLength(result) > UNIX_SOCKET_PATH_MAX) result = path.join(dir, calculateSha1(name).slice(0, 16) + '.sock'); if (Buffer.byteLength(result) > UNIX_SOCKET_PATH_MAX) - throw new Error(`Socket directory path is too long (${Buffer.byteLength(dir)} bytes); set PWTEST_SOCKETS_DIR to a shorter location.`); + throw new Error(`Socket directory path is too long (${Buffer.byteLength(dir)} bytes); set PLAYWRIGHT_SOCKETS_DIR to a shorter location.`); fs.mkdirSync(dir, { recursive: true }); return result; } diff --git a/tests/extension/extension-fixtures.ts b/tests/extension/extension-fixtures.ts index 760c78fdaf8f3..3c1d1aba85c2a 100644 --- a/tests/extension/extension-fixtures.ts +++ b/tests/extension/extension-fixtures.ts @@ -131,7 +131,7 @@ function cliEnv() { PWTEST_DAEMON_SESSION_DIR: test.info().outputPath('daemon'), // Short path because macOS caps unix socket paths at 104 chars; the // long `project.outputDir` path overflows and causes EADDRINUSE. - PWTEST_SOCKETS_DIR: path.join(os.tmpdir(), 'pwmcp-sock', String(test.info().parallelIndex)), + PLAYWRIGHT_SOCKETS_DIR: path.join(os.tmpdir(), 'pwmcp-sock', String(test.info().parallelIndex)), }; } diff --git a/tests/mcp/cli-fixtures.ts b/tests/mcp/cli-fixtures.ts index f19627745a1bf..07d9aedba7413 100644 --- a/tests/mcp/cli-fixtures.ts +++ b/tests/mcp/cli-fixtures.ts @@ -126,7 +126,7 @@ function cliEnv() { PWTEST_SERVER_REGISTRY: test.info().outputPath('registry'), PWTEST_DASHBOARD_SETTINGS_FILE: test.info().outputPath('dashboard.settings.json'), PWTEST_DAEMON_SESSION_DIR: test.info().outputPath('daemon'), - PWTEST_SOCKETS_DIR: path.join(os.tmpdir(), 'ds-' + crypto.createHash('sha1').update(test.info().outputDir).digest('hex').slice(0, 16)), + PLAYWRIGHT_SOCKETS_DIR: path.join(os.tmpdir(), 'ds-' + crypto.createHash('sha1').update(test.info().outputDir).digest('hex').slice(0, 16)), PWTEST_CLI_CHANNEL_SCAN_DISABLED_FOR_TEST: '1', }; } diff --git a/tests/mcp/cli-session.spec.ts b/tests/mcp/cli-session.spec.ts index d260743f57fba..82fc7a591f0eb 100644 --- a/tests/mcp/cli-session.spec.ts +++ b/tests/mcp/cli-session.spec.ts @@ -153,7 +153,7 @@ test('session stops when temporary socket directory disappears', async ({ cli, s try { const { daemonPid } = await cli('open', server.HELLO_WORLD, { env: { - PWTEST_SOCKETS_DIR: '', + PLAYWRIGHT_SOCKETS_DIR: '', TMPDIR: tempDir, }, }); From 0e93141d9b3033e65f82511dc49e34d173c7b581 Mon Sep 17 00:00:00 2001 From: Yury Semikhatsky Date: Wed, 28 Oct 2026 13:34:29 -0700 Subject: [PATCH 2/3] feat(cli): keep sockets in XDG_RUNTIME_DIR when it is set --- docs/src/getting-started-cli.md | 2 +- packages/utils/fileUtils.ts | 16 ++++++++++- tests/mcp/cli-session.spec.ts | 49 +++++++++++++++++---------------- 3 files changed, 42 insertions(+), 25 deletions(-) diff --git a/docs/src/getting-started-cli.md b/docs/src/getting-started-cli.md index 6dd6efce243dd..8f8df5243c826 100644 --- a/docs/src/getting-started-cli.md +++ b/docs/src/getting-started-cli.md @@ -255,7 +255,7 @@ playwright-cli -s=name delete-data # delete user data for a named session A headless session shuts itself down after an hour without commands, so a session an agent forgot to close does not keep a browser running. Headed and attached browsers are never closed automatically. Change the timeout with `open --idle-timeout=` or `timeouts.idle` in the config file, and pass `0` to disable it. -Sessions communicate through sockets in the system temporary directory. To keep the sockets in another directory, for example on a Linux machine shared with other users, set the `PLAYWRIGHT_SOCKETS_DIR` environment variable for all `playwright-cli` commands: +Sessions communicate through sockets in the user runtime directory (`$XDG_RUNTIME_DIR`), or in the system temporary directory when it is not set. To keep the sockets in another directory, set the `PLAYWRIGHT_SOCKETS_DIR` environment variable for all `playwright-cli` commands: ```bash export PLAYWRIGHT_SOCKETS_DIR=~/.cache/playwright-sockets diff --git a/packages/utils/fileUtils.ts b/packages/utils/fileUtils.ts index 16c7cbf0c5a16..746e496e7dfe9 100644 --- a/packages/utils/fileUtils.ts +++ b/packages/utils/fileUtils.ts @@ -153,7 +153,21 @@ export function makeSocketPath(domain: string, name: string): string { const suffix = socketsDir ? `-${calculateSha1(socketsDir).slice(0, 8)}` : ''; return `\\\\.\\pipe\\pw-${userNameHash}-${domain}-${name}${suffix}`; } - const baseDir = process.env.PLAYWRIGHT_SOCKETS_DIR || path.join(os.tmpdir(), `pw-${userNameHash}`); + if (process.env.PLAYWRIGHT_SOCKETS_DIR) + return makeSocketPathIn(process.env.PLAYWRIGHT_SOCKETS_DIR, domain, name); + // Unlike a shared /tmp, the runtime directory is private to the user. + const runtimeDir = process.env.XDG_RUNTIME_DIR; + if (runtimeDir && path.isAbsolute(runtimeDir)) { + try { + return makeSocketPathIn(path.join(runtimeDir, 'playwright'), domain, name); + } catch { + // The variable can point to a directory that does not exist or belongs to another user. + } + } + return makeSocketPathIn(path.join(os.tmpdir(), `pw-${userNameHash}`), domain, name); +} + +function makeSocketPathIn(baseDir: string, domain: string, name: string): string { const dir = path.join(baseDir, domain); let result = path.join(dir, sanitizeForFilePath(name) + '.sock'); if (Buffer.byteLength(result) > UNIX_SOCKET_PATH_MAX) diff --git a/tests/mcp/cli-session.spec.ts b/tests/mcp/cli-session.spec.ts index 82fc7a591f0eb..f2c7993f7becf 100644 --- a/tests/mcp/cli-session.spec.ts +++ b/tests/mcp/cli-session.spec.ts @@ -144,29 +144,32 @@ test('session stops when browser exits', async ({ cli, server }) => { expect(listAfter).toContain('(no browsers)'); }); -test('session stops when temporary socket directory disappears', async ({ cli, server }) => { - test.skip(process.platform === 'win32'); - - // Keep this short because macOS limits Unix socket paths to 103 bytes. - const tempDir = await fs.promises.mkdtemp(path.join(os.tmpdir(), 'p-')); - const removedTempDir = tempDir + '.removed'; - try { - const { daemonPid } = await cli('open', server.HELLO_WORLD, { - env: { - PLAYWRIGHT_SOCKETS_DIR: '', - TMPDIR: tempDir, - }, - }); - expect(daemonPid).toBeDefined(); - expect(isProcessAlive(daemonPid)).toBe(true); - - await fs.promises.rename(tempDir, removedTempDir); - await expect.poll(() => isProcessAlive(daemonPid)).toBe(false); - } finally { - await fs.promises.rm(tempDir, { force: true, recursive: true }); - await fs.promises.rm(removedTempDir, { force: true, recursive: true }); - } -}); +for (const envName of ['XDG_RUNTIME_DIR', 'TMPDIR']) { + test(`session stops when socket directory in ${envName} disappears`, async ({ cli, server }) => { + test.skip(process.platform === 'win32'); + + // Keep this short because macOS limits Unix socket paths to 103 bytes. + const tempDir = await fs.promises.mkdtemp(path.join(os.tmpdir(), 'p-')); + const removedTempDir = tempDir + '.removed'; + try { + const { daemonPid } = await cli('open', server.HELLO_WORLD, { + env: { + PLAYWRIGHT_SOCKETS_DIR: '', + XDG_RUNTIME_DIR: '', + [envName]: tempDir, + }, + }); + expect(daemonPid).toBeDefined(); + expect(isProcessAlive(daemonPid)).toBe(true); + + await fs.promises.rename(tempDir, removedTempDir); + await expect.poll(() => isProcessAlive(daemonPid)).toBe(false); + } finally { + await fs.promises.rm(tempDir, { force: true, recursive: true }); + await fs.promises.rm(removedTempDir, { force: true, recursive: true }); + } + }); +} test('session reopen with different config', async ({ cli, server }, testInfo) => { const config = { browser: { contextOptions: { viewport: { width: 700, height: 500 } } } }; From 72923bdf6ba6f9c74214946ff45fa7af54fe7789 Mon Sep 17 00:00:00 2001 From: Yury Semikhatsky Date: Wed, 28 Oct 2026 13:39:00 -0700 Subject: [PATCH 3/3] chore: drop PLAYWRIGHT_SOCKETS_DIR, keep the internal test variable --- docs/src/getting-started-cli.md | 6 ------ packages/utils/fileUtils.ts | 8 ++++---- tests/extension/extension-fixtures.ts | 2 +- tests/mcp/cli-fixtures.ts | 2 +- tests/mcp/cli-session.spec.ts | 2 +- 5 files changed, 7 insertions(+), 13 deletions(-) diff --git a/docs/src/getting-started-cli.md b/docs/src/getting-started-cli.md index 8f8df5243c826..4f1e54f6cc7ad 100644 --- a/docs/src/getting-started-cli.md +++ b/docs/src/getting-started-cli.md @@ -255,12 +255,6 @@ playwright-cli -s=name delete-data # delete user data for a named session A headless session shuts itself down after an hour without commands, so a session an agent forgot to close does not keep a browser running. Headed and attached browsers are never closed automatically. Change the timeout with `open --idle-timeout=` or `timeouts.idle` in the config file, and pass `0` to disable it. -Sessions communicate through sockets in the user runtime directory (`$XDG_RUNTIME_DIR`), or in the system temporary directory when it is not set. To keep the sockets in another directory, set the `PLAYWRIGHT_SOCKETS_DIR` environment variable for all `playwright-cli` commands: - -```bash -export PLAYWRIGHT_SOCKETS_DIR=~/.cache/playwright-sockets -``` - ## Monitoring Use `playwright-cli show` to open a visual dashboard for observing and controlling all running browser sessions: diff --git a/packages/utils/fileUtils.ts b/packages/utils/fileUtils.ts index 746e496e7dfe9..5a10350aeb0fd 100644 --- a/packages/utils/fileUtils.ts +++ b/packages/utils/fileUtils.ts @@ -149,12 +149,12 @@ const UNIX_SOCKET_PATH_MAX = 103; export function makeSocketPath(domain: string, name: string): string { const userNameHash = calculateSha1(process.env.USERNAME || process.env.USER || 'default').slice(0, 8); if (process.platform === 'win32') { - const socketsDir = process.env.PLAYWRIGHT_SOCKETS_DIR; + const socketsDir = process.env.PWTEST_SOCKETS_DIR; const suffix = socketsDir ? `-${calculateSha1(socketsDir).slice(0, 8)}` : ''; return `\\\\.\\pipe\\pw-${userNameHash}-${domain}-${name}${suffix}`; } - if (process.env.PLAYWRIGHT_SOCKETS_DIR) - return makeSocketPathIn(process.env.PLAYWRIGHT_SOCKETS_DIR, domain, name); + if (process.env.PWTEST_SOCKETS_DIR) + return makeSocketPathIn(process.env.PWTEST_SOCKETS_DIR, domain, name); // Unlike a shared /tmp, the runtime directory is private to the user. const runtimeDir = process.env.XDG_RUNTIME_DIR; if (runtimeDir && path.isAbsolute(runtimeDir)) { @@ -173,7 +173,7 @@ function makeSocketPathIn(baseDir: string, domain: string, name: string): string if (Buffer.byteLength(result) > UNIX_SOCKET_PATH_MAX) result = path.join(dir, calculateSha1(name).slice(0, 16) + '.sock'); if (Buffer.byteLength(result) > UNIX_SOCKET_PATH_MAX) - throw new Error(`Socket directory path is too long (${Buffer.byteLength(dir)} bytes); set PLAYWRIGHT_SOCKETS_DIR to a shorter location.`); + throw new Error(`Socket directory path is too long (${Buffer.byteLength(dir)} bytes); set PWTEST_SOCKETS_DIR to a shorter location.`); fs.mkdirSync(dir, { recursive: true }); return result; } diff --git a/tests/extension/extension-fixtures.ts b/tests/extension/extension-fixtures.ts index 3c1d1aba85c2a..760c78fdaf8f3 100644 --- a/tests/extension/extension-fixtures.ts +++ b/tests/extension/extension-fixtures.ts @@ -131,7 +131,7 @@ function cliEnv() { PWTEST_DAEMON_SESSION_DIR: test.info().outputPath('daemon'), // Short path because macOS caps unix socket paths at 104 chars; the // long `project.outputDir` path overflows and causes EADDRINUSE. - PLAYWRIGHT_SOCKETS_DIR: path.join(os.tmpdir(), 'pwmcp-sock', String(test.info().parallelIndex)), + PWTEST_SOCKETS_DIR: path.join(os.tmpdir(), 'pwmcp-sock', String(test.info().parallelIndex)), }; } diff --git a/tests/mcp/cli-fixtures.ts b/tests/mcp/cli-fixtures.ts index 07d9aedba7413..f19627745a1bf 100644 --- a/tests/mcp/cli-fixtures.ts +++ b/tests/mcp/cli-fixtures.ts @@ -126,7 +126,7 @@ function cliEnv() { PWTEST_SERVER_REGISTRY: test.info().outputPath('registry'), PWTEST_DASHBOARD_SETTINGS_FILE: test.info().outputPath('dashboard.settings.json'), PWTEST_DAEMON_SESSION_DIR: test.info().outputPath('daemon'), - PLAYWRIGHT_SOCKETS_DIR: path.join(os.tmpdir(), 'ds-' + crypto.createHash('sha1').update(test.info().outputDir).digest('hex').slice(0, 16)), + PWTEST_SOCKETS_DIR: path.join(os.tmpdir(), 'ds-' + crypto.createHash('sha1').update(test.info().outputDir).digest('hex').slice(0, 16)), PWTEST_CLI_CHANNEL_SCAN_DISABLED_FOR_TEST: '1', }; } diff --git a/tests/mcp/cli-session.spec.ts b/tests/mcp/cli-session.spec.ts index f2c7993f7becf..284acb61501e7 100644 --- a/tests/mcp/cli-session.spec.ts +++ b/tests/mcp/cli-session.spec.ts @@ -154,7 +154,7 @@ for (const envName of ['XDG_RUNTIME_DIR', 'TMPDIR']) { try { const { daemonPid } = await cli('open', server.HELLO_WORLD, { env: { - PLAYWRIGHT_SOCKETS_DIR: '', + PWTEST_SOCKETS_DIR: '', XDG_RUNTIME_DIR: '', [envName]: tempDir, },