From 57990fdcf34167d3b22c82a13ef1d759928129bd Mon Sep 17 00:00:00 2001 From: Neil <4138956+nwparker@users.noreply.github.com> Date: Tue, 26 May 2026 20:13:56 -0700 Subject: [PATCH] fix: clear CDP proxy startup error listener (#2876) --- src/main/browser/cdp-ws-proxy.test.ts | 8 ++++++++ src/main/browser/cdp-ws-proxy.ts | 6 +++++- 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/src/main/browser/cdp-ws-proxy.test.ts b/src/main/browser/cdp-ws-proxy.test.ts index ddb3f3d1a..ea3730130 100644 --- a/src/main/browser/cdp-ws-proxy.test.ts +++ b/src/main/browser/cdp-ws-proxy.test.ts @@ -94,6 +94,14 @@ describe('CdpWsProxy', () => { expect(proxy.getPort()).toBeGreaterThan(0) }) + it('does not retain an extra startup server error listener after binding', () => { + const server = ( + proxy as unknown as { httpServer: { listenerCount: (event: string) => number } } + ).httpServer + + expect(server.listenerCount('error')).toBeLessThanOrEqual(1) + }) + it('attaches debugger on start', () => { expect(mock.webContents.debugger.attach).toHaveBeenCalledWith('1.3') }) diff --git a/src/main/browser/cdp-ws-proxy.ts b/src/main/browser/cdp-ws-proxy.ts index ae257b40c..a5aa51ffe 100644 --- a/src/main/browser/cdp-ws-proxy.ts +++ b/src/main/browser/cdp-ws-proxy.ts @@ -24,6 +24,9 @@ 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 => { + reject(error) + } this.wss.on('connection', (ws) => { if (this.client) { this.client.close() @@ -37,6 +40,7 @@ export class CdpWsProxy { }) }) this.httpServer.listen(0, '127.0.0.1', () => { + this.httpServer?.removeListener('error', onListenError) const addr = this.httpServer!.address() if (typeof addr === 'object' && addr) { this.port = addr.port @@ -45,7 +49,7 @@ export class CdpWsProxy { reject(new Error('Failed to bind proxy server')) } }) - this.httpServer.on('error', reject) + this.httpServer.once('error', onListenError) }) }