fix: release CDP proxy resources on startup failure

This commit is contained in:
Neil
2026-05-29 02:03:16 -07:00
committed by GitHub
parent 582a0486c4
commit ff5b0daf3d
+13 -2
View File
@@ -24,9 +24,20 @@ export class CdpWsProxy {
return new Promise<string>((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)