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): backport zombie connection fix #9003

Merged
merged 2 commits into from
Jan 2, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
33 changes: 15 additions & 18 deletions src/client/websocket/WebSocketShard.js
Original file line number Diff line number Diff line change
Expand Up @@ -578,28 +578,28 @@ class WebSocketShard extends EventEmitter {
}
this.wsCloseTimeout = setTimeout(() => {
this.setWsCloseTimeout(-1);
this.debug(`[WebSocket] Close Emitted: ${this.closeEmitted}`);

// 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}`,
);
this.debug(`[WebSocket] close was already emitted, assuming the connection was closed properly.`);
// Setting the variable false to check for zombie connections.
this.closeEmitted = false;
return;
}

this.debug(
// eslint-disable-next-line max-len
`[WebSocket] did not close properly, assuming a zombie connection.\nEmitting close and reconnecting again.`,
`[WebSocket] Close Emitted: ${this.closeEmitted} | did not close properly, assuming a zombie connection.\nEmitting close and reconnecting again.`,
);

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 @@ -824,14 +824,11 @@ class WebSocketShard extends EventEmitter {
this._emitDestroyed();
}

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.
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);
}
);
this.setWsCloseTimeout(this.manager.client.options.closeTimeout);

// Step 2: Null the connection object
this.connection = null;
Expand Down
2 changes: 1 addition & 1 deletion test/shard.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ process.send(123);

client.on('ready', () => {
console.log('Ready', client.options.shards);
if (client.options.shards === 0) {
if (client.options.shards[0] === 0) {
setTimeout(() => {
console.log('kek dying');
client.destroy();
Expand Down