From a24abf43297215b6fe70e0bc61b2ec1dada84b9f Mon Sep 17 00:00:00 2001 From: shiftinv <8530778+shiftinv@users.noreply.github.com> Date: Sat, 20 Jan 2024 17:44:58 +0100 Subject: [PATCH] fix(shard): handle `RECONNECT` opcode during initial connection flow (#1155) --- changelog/1155.bugfix.rst | 1 + disnake/shard.py | 18 ++++++++++++++++++ 2 files changed, 19 insertions(+) create mode 100644 changelog/1155.bugfix.rst diff --git a/changelog/1155.bugfix.rst b/changelog/1155.bugfix.rst new file mode 100644 index 0000000000..9dabc40f9a --- /dev/null +++ b/changelog/1155.bugfix.rst @@ -0,0 +1 @@ +Handle unexpected ``RECONNECT`` opcode where ``HELLO`` is expected during initial shard connection. diff --git a/disnake/shard.py b/disnake/shard.py index a82ae13efd..3903d413aa 100644 --- a/disnake/shard.py +++ b/disnake/shard.py @@ -190,6 +190,16 @@ async def reidentify(self, exc: ReconnectWebSocket) -> None: gateway=self.ws.resume_gateway if exc.resume else None, ) self.ws = await asyncio.wait_for(coro, timeout=60.0) + # n.b. this is the same error handling as for the actual worker, but for the initial connect steps + except ReconnectWebSocket as e: + _log.debug( + "Unexpectedly received request to %s shard ID %s while attempting to %s", + e.op, + self.id, + exc.op, + ) + etype = EventType.resume if e.resume else EventType.identify + self._queue_put(EventItem(etype, self, e)) except self._handled_exceptions as e: await self._handle_disconnect(e) except asyncio.CancelledError: @@ -204,6 +214,14 @@ async def reconnect(self) -> None: try: coro = DiscordWebSocket.from_client(self._client, shard_id=self.id) self.ws = await asyncio.wait_for(coro, timeout=60.0) + except ReconnectWebSocket as e: + _log.debug( + "Unexpectedly received request to %s shard ID %s while attempting to reconnect", + e.op, + self.id, + ) + etype = EventType.resume if e.resume else EventType.identify + self._queue_put(EventItem(etype, self, e)) except self._handled_exceptions as e: await self._handle_disconnect(e) except asyncio.CancelledError: