Skip to content

Chrome DevTools for coding agents

fix: allow stopping the CLI if command fails (#3088)

Nikolay Vitkov77e238e6aba6
diff --git a/src/daemon/client.ts b/src/daemon/client.ts
index ad5de0c0..e996b539 100644
--- a/src/daemon/client.ts
+++ b/src/daemon/client.ts
@@ -37,25 +37,10 @@ function delay(ms: number): Promise<void> {
});
}
-/**
- * Waits for a file to be created and populated (removed = false) or removed (removed = true).
- */
-async function waitForFile(filePath: string, removed = false): Promise<void> {
- const check = (): boolean => {
- const exists = fs.existsSync(filePath);
- if (removed) {
- return !exists;
- }
- if (!exists) {
- return false;
- }
- try {
- return fs.statSync(filePath).size > 0;
- } catch {
- return false;
- }
- };
-
+async function waitFor(
+ check: () => boolean,
+ timeoutMessage: string,
+): Promise<void> {
const deadline = Date.now() + FILE_TIMEOUT;
while (Date.now() < deadline) {
if (check()) {
@@ -71,9 +56,23 @@ async function waitForFile(filePath: string, removed = false): Promise<void> {
return;
}
- throw new Error(
- `Timeout: file ${filePath} ${removed ? 'not removed' : 'not found'} within ${FILE_TIMEOUT}ms`,
- );
+ throw new Error(timeoutMessage);
+}
+
+/**
+ * Waits for a file to be created and populated.
+ */
+async function waitForFile(filePath: string): Promise<void> {
+ await waitFor(() => {
+ if (!fs.existsSync(filePath)) {
+ return false;
+ }
+ try {
+ return fs.statSync(filePath).size > 0;
+ } catch {
+ return false;
+ }
+ }, `Timeout: file ${filePath} not found within ${FILE_TIMEOUT}ms`);
}
async function waitForDaemonReady(sessionId: string): Promise<void> {
@@ -206,10 +205,25 @@ export async function stopDaemon(sessionId: string): Promise<void> {
const pid = getDaemonPid(sessionId);
const pidFilePath = getPidFilePath(sessionId);
- await sendCommand({method: 'stop'}, sessionId);
+ try {
+ await sendCommand({method: 'stop'}, sessionId);
+ } catch (error) {
+ logger?.('Failed to stop daemon via socket, sending SIGTERM:', error);
+ if (pid) {
+ try {
+ process.kill(pid, 'SIGTERM');
+ } catch {
+ // Ignore if process already exited.
+ }
+ }
+ }
try {
- await waitForFile(pidFilePath, /* removed=*/ true);
+ await waitFor(
+ () => !isDaemonRunning(sessionId),
+ `Timeout: daemon (pid ${pid}) did not exit within ${FILE_TIMEOUT}ms`,
+ );
+ fs.rmSync(pidFilePath, {force: true});
} catch (error) {
if (pid) {
try {
diff --git a/tests/daemon/client.test.ts b/tests/daemon/client.test.ts
index 89461b92..8a87f128 100644
--- a/tests/daemon/client.test.ts
+++ b/tests/daemon/client.test.ts
@@ -5,8 +5,9 @@
*/
import assert from 'node:assert';
+import {spawn} from 'node:child_process';
import crypto from 'node:crypto';
-import {existsSync, rmSync} from 'node:fs';
+import {existsSync, mkdirSync, rmSync, writeFileSync} from 'node:fs';
import {dirname} from 'node:path';
import {describe, it, afterEach, beforeEach} from 'node:test';
@@ -16,7 +17,11 @@ import {
stopDaemon,
verifyDaemonVersion,
} from '../../src/daemon/client.js';
-import {isDaemonRunning} from '../../src/daemon/utils.js';
+import {
+ getDaemonPid,
+ getPidFilePath,
+ isDaemonRunning,
+} from '../../src/daemon/utils.js';
import {VERSION} from '../../src/version.js';
describe('daemon client', () => {
@@ -73,6 +78,43 @@ describe('daemon client', () => {
'Daemon should still not be running',
);
});
+
+ it('should stop and allow restarting a daemon when its socket is unreachable', async () => {
+ const child = spawn(
+ process.execPath,
+ ['-e', 'setInterval(() => {}, 1000)'],
+ {
+ stdio: 'ignore',
+ },
+ );
+ child.unref();
+ assert.ok(child.pid, 'Child process should have a PID');
+
+ try {
+ const pidFilePath = getPidFilePath(sessionId);
+ mkdirSync(dirname(pidFilePath), {recursive: true, mode: 0o700});
+ writeFileSync(pidFilePath, String(child.pid), 'utf-8');
+ assert.ok(
+ isDaemonRunning(sessionId),
+ 'Daemon should be detected as running via PID file',
+ );
+
+ await stopDaemon(sessionId);
+ assert.ok(
+ !isDaemonRunning(sessionId),
+ 'Unreachable daemon should be stopped via signal fallback',
+ );
+
+ await startDaemon([], sessionId);
+ assert.ok(
+ isDaemonRunning(sessionId),
+ 'New daemon should start after stopping unreachable daemon',
+ );
+ assert.notStrictEqual(getDaemonPid(sessionId), child.pid);
+ } finally {
+ child.kill();
+ }
+ });
});
describe('verifyDaemonVersion', () => {