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(WebSocketManager): await WebSocket destroy #9519

Merged
merged 4 commits into from
Jun 9, 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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions packages/discord.js/src/client/Client.js
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,7 @@ class Client extends BaseClient {
await this.ws.connect();
return this.token;
} catch (error) {
this.destroy();
await this.destroy();
throw error;
}
}
Expand All @@ -242,13 +242,13 @@ class Client extends BaseClient {

/**
* Logs out, terminates the connection to Discord, and destroys the client.
* @returns {void}
* @returns {Promise<void>}
*/
destroy() {
async destroy() {
super.destroy();

this.sweepers.destroy();
this.ws.destroy();
await this.ws.destroy();
this.token = null;
this.rest.setToken(null);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -320,12 +320,12 @@ class WebSocketManager extends EventEmitter {
* Destroys this manager and all its shards.
* @private
*/
destroy() {
async destroy() {
if (this.destroyed) return;
// TODO: Make a util for getting a stack
this.debug(`Manager was destroyed. Called by:\n${new Error().stack}`);
this.destroyed = true;
this._ws?.destroy({ code: CloseCodes.Normal });
await this._ws?.destroy({ code: CloseCodes.Normal });
}

/**
Expand Down
2 changes: 1 addition & 1 deletion packages/discord.js/test/createGuild.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ client.on('ready', async () => {
} catch (error) {
console.error(error);
} finally {
client.destroy();
await client.destroy();
}
});

Expand Down
4 changes: 2 additions & 2 deletions packages/discord.js/test/shard.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,9 @@ process.send(123);
client.on('ready', () => {
console.log('Ready', client.options.shards);
if (client.options.shards === 0) {
setTimeout(() => {
setTimeout(async () => {
console.log('kek dying');
client.destroy();
await client.destroy();
}, 5_000);
}
});
Expand Down
2 changes: 1 addition & 1 deletion packages/discord.js/test/templateCreateGuild.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ client
} catch (error) {
console.error(error);
} finally {
client.destroy();
await client.destroy();
}
})
.login(token)
Expand Down
4 changes: 2 additions & 2 deletions packages/discord.js/typings/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -956,7 +956,7 @@ export class Client<Ready extends boolean = boolean> extends BaseClient {
public users: UserManager;
public voice: ClientVoiceManager;
public ws: WebSocketManager;
public destroy(): void;
public destroy(): Promise<void>;
public fetchGuildPreview(guild: GuildResolvable): Promise<GuildPreview>;
public fetchInvite(invite: InviteResolvable, options?: ClientFetchInviteOptions): Promise<Invite>;
public fetchGuildTemplate(template: GuildTemplateResolvable): Promise<GuildTemplate>;
Expand Down Expand Up @@ -3329,7 +3329,7 @@ export class WebSocketManager extends EventEmitter {
private debug(message: string, shardId?: number): void;
private connect(): Promise<void>;
private broadcast(packet: unknown): void;
private destroy(): void;
private destroy(): Promise<void>;
private handlePacket(packet?: unknown, shard?: WebSocketShard): boolean;
private checkShardsReady(): void;
private triggerClientReady(): void;
Expand Down