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)