fix: improve daemon lifecycle (#2360)

See b/534287578
This commit is contained in:
Alex Rudenko
2026-07-14 10:58:05 +02:00
committed by GitHub
parent f621d0052f
commit 3ee6a27100
3 changed files with 13 additions and 4 deletions
+5
View File
@@ -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({
+5 -3
View File
@@ -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);
});
+3 -1
View File
@@ -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)