fix(discord): reconnect after missed identify

This commit is contained in:
Peter Steinberger
2026-05-02 08:24:04 +01:00
parent ac5af483cb
commit 93e2d90af1
3 changed files with 50 additions and 2 deletions

View File

@@ -127,6 +127,42 @@ describe("GatewayPlugin", () => {
await vi.waitFor(() => expect(errorSpy).toHaveBeenCalledWith(error));
});
it("reconnects when the socket closes while waiting for identify concurrency", async () => {
vi.useFakeTimers();
vi.setSystemTime(0);
await sharedGatewayIdentifyLimiter.wait({ shardId: 0, maxConcurrency: 1 });
const gateway = new TestGatewayPlugin({
autoInteractions: false,
url: "wss://gateway.example.test",
});
const errorSpy = vi.fn();
gateway.emitter.on("error", errorSpy);
gateway.connect(false);
const socket = gateway.sockets[0];
socket?.emit("open");
socket?.emit(
"message",
JSON.stringify({
op: GatewayOpcodes.Hello,
d: { heartbeat_interval: 45_000 },
s: null,
}),
);
if (socket) {
socket.readyState = 3;
}
await vi.advanceTimersByTimeAsync(5_000);
expect(errorSpy).toHaveBeenCalledWith(
new Error("Discord gateway socket closed before IDENTIFY could be sent"),
);
await vi.advanceTimersByTimeAsync(2_000);
expect(gateway.connectCalls).toEqual([false, false]);
expect(gateway.sockets).toHaveLength(2);
});
it("preserves MESSAGE_CREATE author payloads for inbound dispatch", async () => {
const gateway = new GatewayPlugin({ autoInteractions: false });
const dispatchGatewayEvent = vi.fn(async (_event: string, _data: unknown) => {});

View File

@@ -248,7 +248,12 @@ export class GatewayPlugin extends Plugin {
true,
);
} else {
void this.identifyWithConcurrency();
void this.identifyWithConcurrency().catch((error: unknown) => {
this.emitter.emit(
"error",
error instanceof Error ? error : new Error(String(error), { cause: error }),
);
});
}
break;
case GatewayOpcodes.HeartbeatAck:
@@ -325,7 +330,13 @@ export class GatewayPlugin extends Plugin {
shardId: this.shardId,
maxConcurrency: this.gatewayInfo?.session_start_limit.max_concurrency,
});
if (!this.ws || this.ws.readyState !== READY_STATE_OPEN) {
const socket = this.ws;
if (!socket || socket.readyState !== READY_STATE_OPEN) {
const error = new Error("Discord gateway socket closed before IDENTIFY could be sent");
this.emitter.emit("error", error);
if (socket) {
this.scheduleReconnect(false);
}
return;
}
this.identify();