Skip to content

Commit

Permalink
fix(WebSocketShard#destroy): wait for close and cleanup listeners (#8479
Browse files Browse the repository at this point in the history
)

Co-authored-by: kodiakhq[bot] <49736102+kodiakhq[bot]@users.noreply.github.com>
  • Loading branch information
didinele and kodiakhq[bot] committed Aug 16, 2022
1 parent 4fd4252 commit acdafe6
Showing 1 changed file with 11 additions and 0 deletions.
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');
// 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

0 comments on commit acdafe6

Please sign in to comment.