From 2ba584869909363705b9f1b7232cf2bd89ba9cff Mon Sep 17 00:00:00 2001 From: amankumarsingh77 Date: Mon, 6 Jul 2026 23:15:08 +0530 Subject: fix: stale socket detection prevents daemon spawn after crash isDaemonRunning() only checked file existence of the Unix socket. A dead daemon leaves a stale socket, causing ensureDaemon() to skip spawning and client.start() to fail with 'Chrome not connected'. Replace with a liveness probe: connect, register, verify ack, then disconnect. On failure, clean up the stale socket. Also makes bridge handleRequest async to poll for Chrome connection for up to 15s before rejecting. --- src/daemon/bridge.ts | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) (limited to 'src/daemon/bridge.ts') diff --git a/src/daemon/bridge.ts b/src/daemon/bridge.ts index c2c1355..33ef63b 100644 --- a/src/daemon/bridge.ts +++ b/src/daemon/bridge.ts @@ -29,7 +29,7 @@ export type CdpBridge = { * are initialized (not when Chrome is connected — that may take retries). */ start(): Promise; stop(): Promise; - handleRequest(req: WireRequest, clientId: string, send: SendToClient): void; + handleRequest(req: WireRequest, clientId: string, send: SendToClient): Promise; /** True when the Chrome WebSocket is open and ready. */ isAlive(): boolean; recordSession(clientId: string, sessionId: string): void; @@ -339,11 +339,21 @@ export const createCdpBridge = (): CdpBridge => { wsUrl = null; }; - const handleRequest = ( + const handleRequest = async ( req: WireRequest, clientId: string, send: SendToClient, - ): void => { + ): Promise => { + // ponytail: wait for Chrome to connect (user may still be clicking "Allow"). + // Bridge retries in background; this polls so the client doesn't fail first try. + if (!ws || ws.readyState !== WebSocket.OPEN) { + const deadline = Date.now() + 15_000; + while (Date.now() < deadline) { + await new Promise((r) => setTimeout(r, 250)); + if (ws && ws.readyState === WebSocket.OPEN) break; + } + } + if (!ws || ws.readyState !== WebSocket.OPEN) { send(clientId, { type: "response", -- cgit v1.2.3