Skip to content

Commit 5377bd5

Browse files
committed
Unbind all listeners before socket destruction
1 parent da58eb7 commit 5377bd5

File tree

1 file changed

+10
-11
lines changed

1 file changed

+10
-11
lines changed

src/irc.ts

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1272,12 +1272,7 @@ export class Client extends (EventEmitter as unknown as new () => TypedEmitter<C
12721272

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

1429+
private unbindListeners() {
1430+
(
1431+
['data', 'end', 'close', 'timeout', 'error'] as (keyof IrcConnectionEventsMap)[]
1432+
).forEach(evtType => {
1433+
this.conn?.removeAllListeners(evtType);
1434+
});
1435+
}
1436+
14341437
private reconnect(retryCount: number) {
14351438
if (!this.isOurSocket) {
14361439
// Cannot reconnect if the socket is not ours.
@@ -1461,11 +1464,7 @@ export class Client extends (EventEmitter as unknown as new () => TypedEmitter<C
14611464
*/
14621465
public destroy() {
14631466
util.log('Destroying connection');
1464-
(
1465-
['data', 'end', 'close', 'timeout', 'error'] as (keyof IrcConnectionEventsMap)[]
1466-
).forEach(evtType => {
1467-
this.conn?.removeAllListeners(evtType);
1468-
});
1467+
this.unbindListeners();
14691468
if (this.isOurSocket) {
14701469
this.disconnect();
14711470
}

0 commit comments

Comments
 (0)