Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix reconnect loop #115

Merged
merged 4 commits into from
Sep 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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