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(WebSocketShard): Zombie connection fix #8989

Merged
merged 2 commits into from
Jan 1, 2023
Merged
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
47 changes: 22 additions & 25 deletions packages/discord.js/src/client/websocket/WebSocketShard.js
Original file line number Diff line number Diff line change
Expand Up @@ -375,7 +375,11 @@ class WebSocketShard extends EventEmitter {
// Clearing the WebSocket close timeout as close was emitted.
this.setWsCloseTimeout(-1);
// If we still have a connection object, clean up its listeners
if (this.connection) this._cleanupConnection();
if (this.connection) {
this._cleanupConnection();
// Having this after _cleanupConnection to just clean up the connection and not listen to ws.onclose
this.destroy({ reset: !this.sessionId, emit: false, log: false });
}
this.status = Status.Disconnected;
this.emitClose(event);
}
Expand Down Expand Up @@ -404,6 +408,7 @@ class WebSocketShard extends EventEmitter {
*/
this.emit(WebSocketShardEvents.Close, event);
}

/**
* Called whenever a packet is received.
* @param {Object} packet The received packet
Expand Down Expand Up @@ -589,9 +594,7 @@ class WebSocketShard extends EventEmitter {
// Check if close event was emitted.
if (this.closeEmitted) {
this.debug(
`[WebSocket] was closed. | WS State: ${
CONNECTION_STATE[this.connection?.readyState ?? WebSocket.CLOSED]
} | Close Emitted: ${this.closeEmitted}`,
`[WebSocket] was closed. | WS State: ${CONNECTION_STATE[this.connection?.readyState ?? WebSocket.CLOSED]}`,
);
// Setting the variable false to check for zombie connections.
this.closeEmitted = false;
Expand All @@ -603,14 +606,14 @@ class WebSocketShard extends EventEmitter {
);

// Cleanup connection listeners
if (this.connection) {
this._cleanupConnection();
}

this.emitClose();
// Setting the variable false to check for zombie connections.
this.closeEmitted = false;
}, time).unref();
if (this.connection) this._cleanupConnection();

this.emitClose({
code: 4009,
reason: 'Session time out.',
wasClean: false,
});
}, time);
}

/**
Expand Down Expand Up @@ -837,25 +840,19 @@ class WebSocketShard extends EventEmitter {
}

// Emit the destroyed event if needed
if (emit) {
this._emitDestroyed();
} else if (
this.connection.readyState === WebSocket.CLOSING ||
this.connection.readyState === WebSocket.CLOSED
) {
this.closeEmitted = false;
this.debug(
`[WebSocket] Adding a WebSocket close timeout to ensure a correct WS reconnect.
Timeout: ${this.manager.client.options.closeTimeout}ms`,
);
this.setWsCloseTimeout(this.manager.client.options.closeTimeout);
}
if (emit) this._emitDestroyed();
}
} else if (emit) {
// We requested a destroy, but we had no connection. Emit destroyed
this._emitDestroyed();
}

this.debug(
`[WebSocket] Adding a WebSocket close timeout to ensure a correct WS reconnect.
Timeout: ${this.manager.client.options.closeTimeout}ms`,
);
this.setWsCloseTimeout(this.manager.client.options.closeTimeout);
iCrawl marked this conversation as resolved.
Show resolved Hide resolved

// Step 2: Null the connection object
this.connection = null;

Expand Down