Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 15 additions & 1 deletion packages/utils/fileUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.PWTEST_SOCKETS_DIR || path.join(os.tmpdir(), `pw-${userNameHash}`);
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)) {
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)
Expand Down
49 changes: 26 additions & 23 deletions tests/mcp/cli-session.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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: {
PWTEST_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: {
PWTEST_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 } } } };
Expand Down
Loading