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
5 changes: 5 additions & 0 deletions src/daemon/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,11 @@ export async function sendCommand(
sessionId: string,
timeout = SEND_COMMAND_TIMEOUT,
): Promise<DaemonResponse> {
// Before connecting and sending, verify the daemon is still alive.
if (!isDaemonRunning(sessionId)) {
throw new Error('Daemon is not running.');
}

const socketPath = getSocketPath(sessionId);

const socket = net.createConnection({
Expand Down
8 changes: 5 additions & 3 deletions src/daemon/daemon.ts
Original file line number Diff line number Diff line change
Expand Up @@ -262,7 +262,7 @@ async function startSocketServer() {
});
}

async function cleanup() {
async function cleanup(exitCode = 0) {
console.log('Cleaning up daemon...');

try {
Expand Down Expand Up @@ -291,7 +291,7 @@ async function cleanup() {
if (fs.existsSync(pidFilePath)) {
fs.unlinkSync(pidFilePath);
}
process.exit(0);
process.exit(exitCode);
}

// Handle shutdown signals
Expand All @@ -308,13 +308,15 @@ process.on('SIGHUP', () => {
// Handle uncaught errors
process.on('uncaughtException', error => {
logger?.('Uncaught exception:', error);
void cleanup(1);
});
process.on('unhandledRejection', error => {
logger?.('Unhandled rejection:', error);
void cleanup(1);
});

// Start the server
const started = startSocketServer().catch(error => {
logger?.('Failed to start daemon server:', error);
process.exit(1);
void cleanup(1);
});
4 changes: 3 additions & 1 deletion src/daemon/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,15 @@ export const DAEMON_CLIENT_NAME = 'chrome-devtools-cli-daemon';
// Using these paths due to strict limits on the POSIX socket path length.
export function getSocketPath(sessionId: string): string {
const uid = os.userInfo().uid;
const username = os.userInfo().username;
const suffix = sessionId ? `-${sessionId}` : '';
const appName = APP_NAME + suffix;

if (IS_WINDOWS) {
// Windows uses Named Pipes, not file paths.
// This format is required for server.listen()
return path.join('\\\\.\\pipe', appName, 'server.sock');
// Append username to prevent cross-user named pipe squatting
return path.join('\\\\.\\pipe', `${appName}-${username}`, 'server.sock');
}

// 1. Try XDG_RUNTIME_DIR (Linux standard, sometimes macOS)
Expand Down