Skip to content

Self-hosted, Self-Improving, Federated Everything App — dev management, agent orchestration, persistent mind, digital twin, second brain, and creative pipeline from a single dashboard over Tailscale to port the user into the OS

fix: keep CDP probe deadline through response body

Adam Eivy521319c28a2c
diff --git a/browser/cdpProbe.js b/browser/cdpProbe.js
new file mode 100644
index 0000000000..cea3c5a5a7
--- /dev/null
+++ b/browser/cdpProbe.js
@@ -0,0 +1,12 @@
+/** Read a CDP version response under one deadline for headers and body. */
+export async function probeCdpVersion(url) {
+ const controller = new AbortController();
+ const timeout = setTimeout(() => controller.abort(), 2000);
+ return fetch(url, { signal: controller.signal }).then(async res => {
+ if (!res.ok) return null;
+ // A successful HTTP response can still be malformed JSON or a non-CDP
+ // service. Both remain unavailable, just like a failed connection.
+ const version = await res.json();
+ return version?.webSocketDebuggerUrl ? version : null;
+ }).catch(() => null).finally(() => clearTimeout(timeout));
+}
diff --git a/browser/server.js b/browser/server.js
index b29b43703d..f67a5f6501 100644
--- a/browser/server.js
+++ b/browser/server.js
@@ -1,3 +1,4 @@
+import { probeCdpVersion } from './cdpProbe.js';
import { spawn } from 'child_process';
import { createServer } from 'http';
import { readFile, writeFile, mkdir } from 'fs/promises';
@@ -55,22 +56,7 @@ async function loadConfig() {
}
}
-async function checkCdp() {
- const controller = new AbortController();
- const timeout = setTimeout(() => controller.abort(), 2000);
- const res = await fetch(`http://${CDP_HOST}:${CDP_PORT}/json/version`, { signal: controller.signal }).catch(() => null);
- clearTimeout(timeout);
- if (!res?.ok) return null;
- // A 200 here doesn't guarantee it's Chrome's CDP endpoint — something else
- // (a stray dev server, a captive portal, a Chrome HTML error page) can answer
- // on this port with `<!DOCTYPE html>…`. `res.json()` would then reject with
- // "Unexpected token '<'", and that rejection escapes through every caller —
- // most visibly spamming scheduleDownloadReconnect()'s catch every 2s. Treat
- // any non-JSON or non-CDP body as "not reachable" by returning null.
- const version = await res.json().catch(() => null);
- if (!version?.webSocketDebuggerUrl) return null;
- return version;
-}
+const checkCdp = () => probeCdpVersion(`http://${CDP_HOST}:${CDP_PORT}/json/version`);
// Chrome's download directory is set through the profile's own `Preferences`
// file, NOT through CDP's `Browser.setDownloadBehavior`.
diff --git a/scripts/browserCdpProbe.test.js b/scripts/browserCdpProbe.test.js
new file mode 100644
index 0000000000..25407e9a75
--- /dev/null
+++ b/scripts/browserCdpProbe.test.js
@@ -0,0 +1,32 @@
+import { afterEach, expect, it } from 'vitest';
+import { createServer } from 'node:http';
+import { probeCdpVersion } from '../browser/cdpProbe.js';
+
+let server;
+afterEach(async () => {
+ if (!server) return;
+ server.closeAllConnections();
+ await new Promise(resolve => server.close(resolve));
+});
+
+it('bounds a stalled CDP body while preserving valid and malformed endpoint handling', async () => {
+ let stalledBodySent = false;
+ server = createServer((req, res) => {
+ res.writeHead(200, { 'Content-Type': 'application/json' });
+ if (req.url === '/valid') res.end(JSON.stringify({ webSocketDebuggerUrl: 'ws://example.test/devtools/browser/fixture' }));
+ else if (req.url === '/malformed') res.end('<html>not CDP</html>');
+ else if (req.url === '/other') res.end(JSON.stringify({ service: 'other' }));
+ else {
+ res.flushHeaders();
+ res.write('{"webSocketDebuggerUrl":');
+ stalledBodySent = true; // Headers/body began; the fixture never finishes it.
+ }
+ });
+ await new Promise(resolve => server.listen(0, '127.0.0.1', resolve));
+ const base = `http://127.0.0.1:${server.address().port}`;
+ expect(await probeCdpVersion(`${base}/valid`)).toEqual({ webSocketDebuggerUrl: 'ws://example.test/devtools/browser/fixture' });
+ expect(await probeCdpVersion(`${base}/malformed`)).toBeNull();
+ expect(await probeCdpVersion(`${base}/other`)).toBeNull();
+ expect(await probeCdpVersion(`${base}/stalled`)).toBeNull();
+ expect(stalledBodySent).toBe(true);
+});