mirror of
https://github.com/discordjs/discord.js.git
synced 2026-03-11 00:53:31 +01:00
fix(WebSocketShard): Zombie connection fix (#8989)
* fix: zombie connection - Fix backport #7626 missing changes - Reverted the pull request #8956 - Removed unref of wsCloseTimeout - We are resuming the connection for zombie instead of starting a new Co-authored-by: DraftMan <nicovanaarsen@gmail.com> * refactor: ♻️ Format code and remove useless assignation Co-authored-by: DraftMan <nicovanaarsen@gmail.com>
This commit is contained in:
@@ -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);
|
||||
}
|
||||
@@ -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
|
||||
@@ -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;
|
||||
@@ -603,14 +606,14 @@ class WebSocketShard extends EventEmitter {
|
||||
);
|
||||
|
||||
// Cleanup connection listeners
|
||||
if (this.connection) {
|
||||
this._cleanupConnection();
|
||||
}
|
||||
if (this.connection) this._cleanupConnection();
|
||||
|
||||
this.emitClose();
|
||||
// Setting the variable false to check for zombie connections.
|
||||
this.closeEmitted = false;
|
||||
}, time).unref();
|
||||
this.emitClose({
|
||||
code: 4009,
|
||||
reason: 'Session time out.',
|
||||
wasClean: false,
|
||||
});
|
||||
}, time);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -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);
|
||||
|
||||
// Step 2: Null the connection object
|
||||
this.connection = null;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user