Skip to content

Commit

Permalink
cherry-pick(#17349): fix(socks proxy): destroy sockets on close to av…
Browse files Browse the repository at this point in the history
…oid hanging (#17366)
  • Loading branch information
dgozman authored and aslushnikov committed Sep 19, 2022
1 parent ae4c5bf commit e1681a3
Showing 1 changed file with 14 additions and 0 deletions.
14 changes: 14 additions & 0 deletions packages/playwright-core/src/common/socksProxy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -294,6 +294,8 @@ export class SocksProxy extends EventEmitter implements SocksConnectionClient {

private _server: net.Server;
private _connections = new Map<string, SocksConnection>();
private _sockets = new Set<net.Socket>();
private _closed = false;

constructor() {
super();
Expand All @@ -302,6 +304,14 @@ export class SocksProxy extends EventEmitter implements SocksConnectionClient {
const connection = new SocksConnection(uid, socket, this);
this._connections.set(uid, connection);
});
this._server.on('connection', socket => {
if (this._closed) {
socket.destroy();
return;
}
this._sockets.add(socket);
socket.once('close', () => this._sockets.delete(socket));
});
}

async listen(port: number): Promise<number> {
Expand All @@ -315,6 +325,10 @@ export class SocksProxy extends EventEmitter implements SocksConnectionClient {
}

async close() {
this._closed = true;
for (const socket of this._sockets)
socket.destroy();
this._sockets.clear();
await new Promise(f => this._server.close(f));
}

Expand Down

0 comments on commit e1681a3

Please sign in to comment.