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:
Voxelli
2023-01-01 23:19:20 +05:30
committed by GitHub
parent d3e9f2a355
commit 876b181312

View File

@@ -375,7 +375,11 @@ class WebSocketShard extends EventEmitter {
// Clearing the WebSocket close timeout as close was emitted. // Clearing the WebSocket close timeout as close was emitted.
this.setWsCloseTimeout(-1); this.setWsCloseTimeout(-1);
// If we still have a connection object, clean up its listeners // 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.status = Status.Disconnected;
this.emitClose(event); this.emitClose(event);
} }
@@ -404,6 +408,7 @@ class WebSocketShard extends EventEmitter {
*/ */
this.emit(WebSocketShardEvents.Close, event); this.emit(WebSocketShardEvents.Close, event);
} }
/** /**
* Called whenever a packet is received. * Called whenever a packet is received.
* @param {Object} packet The received packet * @param {Object} packet The received packet
@@ -589,9 +594,7 @@ class WebSocketShard extends EventEmitter {
// Check if close event was emitted. // Check if close event was emitted.
if (this.closeEmitted) { if (this.closeEmitted) {
this.debug( this.debug(
`[WebSocket] was closed. | WS State: ${ `[WebSocket] was closed. | WS State: ${CONNECTION_STATE[this.connection?.readyState ?? WebSocket.CLOSED]}`,
CONNECTION_STATE[this.connection?.readyState ?? WebSocket.CLOSED]
} | Close Emitted: ${this.closeEmitted}`,
); );
// Setting the variable false to check for zombie connections. // Setting the variable false to check for zombie connections.
this.closeEmitted = false; this.closeEmitted = false;
@@ -603,14 +606,14 @@ class WebSocketShard extends EventEmitter {
); );
// Cleanup connection listeners // Cleanup connection listeners
if (this.connection) { if (this.connection) this._cleanupConnection();
this._cleanupConnection();
}
this.emitClose(); this.emitClose({
// Setting the variable false to check for zombie connections. code: 4009,
this.closeEmitted = false; reason: 'Session time out.',
}, time).unref(); wasClean: false,
});
}, time);
} }
/** /**
@@ -837,25 +840,19 @@ class WebSocketShard extends EventEmitter {
} }
// Emit the destroyed event if needed // Emit the destroyed event if needed
if (emit) { if (emit) this._emitDestroyed();
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);
}
} }
} else if (emit) { } else if (emit) {
// We requested a destroy, but we had no connection. Emit destroyed // We requested a destroy, but we had no connection. Emit destroyed
this._emitDestroyed(); 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 // Step 2: Null the connection object
this.connection = null; this.connection = null;