diff options
Diffstat (limited to 'src')
| -rw-r--r-- | src/cdp/session.ts | 8 | ||||
| -rw-r--r-- | src/cdp/transport.ts | 3 | ||||
| -rw-r--r-- | src/client.ts | 4 | ||||
| -rw-r--r-- | src/index.ts | 8 |
4 files changed, 21 insertions, 2 deletions
diff --git a/src/cdp/session.ts b/src/cdp/session.ts index fffa241..b1ae71c 100644 --- a/src/cdp/session.ts +++ b/src/cdp/session.ts @@ -45,7 +45,13 @@ export const createCdpSession = (transport: CdpTransport): CdpSession => { }; const restartConsumer = (): void => { - activeConsumer = activeConsumer.then(() => consumeEvents()).catch(() => undefined); + activeConsumer = activeConsumer.then(() => consumeEvents()).catch((e: unknown) => { + // The .then() chain calls consumeEvents() which iterates the transport's + // events() AsyncIterable. The iterable resolves cleanly on close (returns + // {done:true}); any rejection here is an unexpected bug in the event + // handler, not a normal termination. Surface it on stderr so it's not lost. + console.warn("[pi-browser-harness] CDP event consumer crashed:", e); + }); }; restartConsumer(); diff --git a/src/cdp/transport.ts b/src/cdp/transport.ts index 6da25fa..c5a5a41 100644 --- a/src/cdp/transport.ts +++ b/src/cdp/transport.ts @@ -156,6 +156,9 @@ export const createCdpTransport = (): CdpTransport => { clearTimeout(timer); ws = null; cleanup("WebSocket closed"); + // If onopen already settled the connect promise, this settle is a no-op + // (guarded by `settled`). cleanup() above still runs unconditionally so + // pending requests are rejected and the event queue is rotated. settle(err(cdpError("transport_closed", "CDP WebSocket closed"))); }; }); diff --git a/src/client.ts b/src/client.ts index 1fcef67..51dfa9f 100644 --- a/src/client.ts +++ b/src/client.ts @@ -117,6 +117,10 @@ export const createBrowserClient = (opts: BrowserClientOptions): BrowserClient = }; const evaluateJs = async (expression: string, sessionId?: string): Promise<Result<unknown, CdpError>> => { + // Heuristic IIFE wrap: legacy convenience that lets agents write + // `return foo` instead of `(() => foo)()`. False-positive on substring + // `"return "` inside string literals or comments — agents that hit this + // can pass an explicit IIFE instead. const wrapped = expression.includes("return ") && !expression.trim().startsWith("(") ? `(function(){${expression}})()` : expression; diff --git a/src/index.ts b/src/index.ts index f5c7676..ee59f10 100644 --- a/src/index.ts +++ b/src/index.ts @@ -115,7 +115,13 @@ export default function browserHarnessExtension(pi: ExtensionAPI): void { pi.on("session_shutdown", async () => { persistState(pi, state); if (client) { - try { await client.stop(); } catch { /* best-effort */ } + try { + await client.stop(); + } catch (e) { + // Shutdown is best-effort, but a stuck stop() points at a transport + // bug worth surfacing for debugging. + console.warn("[pi-browser-harness] client.stop() failed during shutdown:", e); + } client = null; } toolsRegistered = false; |
