Skip to content

Commit

Permalink
Unbind all listeners before socket destruction
Browse files Browse the repository at this point in the history
  • Loading branch information
jkhsjdhjs committed Aug 1, 2024
1 parent da58eb7 commit 5377bd5
Showing 1 changed file with 10 additions and 11 deletions.
21 changes: 10 additions & 11 deletions src/irc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1272,12 +1272,7 @@ export class Client extends (EventEmitter as unknown as new () => TypedEmitter<C

// destroy old socket before allocating a new one
if (this.isOurSocket && this.conn) {
// The 'close' listener is called when the socket is destroyed, which
// initiates another reconnect attempt. This reconnect attempt disconnects
// the new connection created by this control flow after the `retryDelay`.
// To avoid an endless reconnect loop, we need to remove the 'close'
// listener here before destroying the socket.
this.conn.removeAllListeners('close');
this.unbindListeners();
this.conn.destroy();
this.conn = undefined;
}
Expand Down Expand Up @@ -1431,6 +1426,14 @@ export class Client extends (EventEmitter as unknown as new () => TypedEmitter<C
});
}

private unbindListeners() {
(
['data', 'end', 'close', 'timeout', 'error'] as (keyof IrcConnectionEventsMap)[]
).forEach(evtType => {
this.conn?.removeAllListeners(evtType);
});
}

private reconnect(retryCount: number) {
if (!this.isOurSocket) {
// Cannot reconnect if the socket is not ours.
Expand Down Expand Up @@ -1461,11 +1464,7 @@ export class Client extends (EventEmitter as unknown as new () => TypedEmitter<C
*/
public destroy() {
util.log('Destroying connection');
(
['data', 'end', 'close', 'timeout', 'error'] as (keyof IrcConnectionEventsMap)[]
).forEach(evtType => {
this.conn?.removeAllListeners(evtType);
});
this.unbindListeners();
if (this.isOurSocket) {
this.disconnect();
}
Expand Down

0 comments on commit 5377bd5

Please sign in to comment.