Skip to content

Commit

Permalink
Merge pull request #115 from jkhsjdhjs/fix/reconnect_loop
Browse files Browse the repository at this point in the history
Fix reconnect loop
  • Loading branch information
tadzik authored Sep 13, 2024
2 parents 06d8ff2 + 5377bd5 commit 1dd2a84
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 5 deletions.
1 change: 1 addition & 0 deletions changelog.d/115.bugfix
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fix reconnect loop due to the close listener being called on socket destruction.
15 changes: 10 additions & 5 deletions src/irc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1272,6 +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) {
this.unbindListeners();
this.conn.destroy();
this.conn = undefined;
}
Expand Down Expand Up @@ -1425,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 @@ -1455,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 1dd2a84

Please sign in to comment.