Skip to content

Chrome DevTools for coding agents

fix: improve daemon lifecycle (#2360)

Alex Rudenko3ee6a2710029
diff --git a/src/daemon/client.ts b/src/daemon/client.ts
index 8aa511e7..281d767a 100644
--- a/src/daemon/client.ts
+++ b/src/daemon/client.ts
@@ -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({
diff --git a/src/daemon/daemon.ts b/src/daemon/daemon.ts
index 0be23d99..ecfb29f4 100644
--- a/src/daemon/daemon.ts
+++ b/src/daemon/daemon.ts
@@ -262,7 +262,7 @@ async function startSocketServer() {
});
}
-async function cleanup() {
+async function cleanup(exitCode = 0) {
console.log('Cleaning up daemon...');
try {
@@ -291,7 +291,7 @@ async function cleanup() {
if (fs.existsSync(pidFilePath)) {
fs.unlinkSync(pidFilePath);
}
- process.exit(0);
+ process.exit(exitCode);
}
// Handle shutdown signals
@@ -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);
});
diff --git a/src/daemon/utils.ts b/src/daemon/utils.ts
index 7f70343e..8bf7bb66 100644
--- a/src/daemon/utils.ts
+++ b/src/daemon/utils.ts
@@ -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)