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#destroy): wait for close and cleanup listeners #8479

Merged
merged 2 commits into from
Aug 16, 2022
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
11 changes: 11 additions & 0 deletions packages/ws/src/ws/WebSocketShard.ts
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,18 @@ export class WebSocketShard extends AsyncEventEmitter<WebSocketShardEventsMap> {
this.connection &&
(this.connection.readyState === WebSocket.OPEN || this.connection.readyState === WebSocket.CONNECTING)
) {
// No longer need to listen to messages
this.connection.removeAllListeners('message');
didinele marked this conversation as resolved.
Show resolved Hide resolved
// Prevent a reconnection loop by unbinding the main close event
this.connection.removeAllListeners('close');
this.connection.close(options.code, options.reason);

// Actually wait for the connection to close
await once(this.connection, 'close');

// Lastly, remove the error event.
// Doing this earlier would cause a hard crash in case an error event fired on our `close` call
this.connection.removeAllListeners('error');
}

this.status = WebSocketShardStatus.Idle;
Expand Down