From ff5b0daf3d109799c5a19a76dcb7efd0d6a16c10 Mon Sep 17 00:00:00 2001 From: Neil <4138956+nwparker@users.noreply.github.com> Date: Fri, 29 May 2026 02:03:16 -0700 Subject: [PATCH] fix: release CDP proxy resources on startup failure --- src/main/browser/cdp-ws-proxy.ts | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/src/main/browser/cdp-ws-proxy.ts b/src/main/browser/cdp-ws-proxy.ts index a5aa51ffe..6ec1d2bb5 100644 --- a/src/main/browser/cdp-ws-proxy.ts +++ b/src/main/browser/cdp-ws-proxy.ts @@ -24,9 +24,20 @@ export class CdpWsProxy { return new Promise((resolve, reject) => { this.httpServer = createServer((req, res) => this.handleHttpRequest(req, res)) this.wss = new WebSocketServer({ server: this.httpServer }) - const onListenError = (error: Error): void => { + const failStart = (error: Error): void => { + this.httpServer?.removeListener('error', onListenError) + this.wss?.close() + this.wss = null + this.httpServer?.close() + this.httpServer = null + // Why: a bind failure happens after debugger attach; release it here + // because callers cannot safely call stop() on a failed start. + this.detachDebugger() reject(error) } + const onListenError = (error: Error): void => { + failStart(error) + } this.wss.on('connection', (ws) => { if (this.client) { this.client.close() @@ -46,7 +57,7 @@ export class CdpWsProxy { this.port = addr.port resolve(`ws://127.0.0.1:${this.port}`) } else { - reject(new Error('Failed to bind proxy server')) + failStart(new Error('Failed to bind proxy server')) } }) this.httpServer.once('error', onListenError)