From 55d3c59301abeda37c4d9976284d5769f9370260 Mon Sep 17 00:00:00 2001 From: Astrea <25420078+AstreaTSS@users.noreply.github.com> Date: Tue, 12 Aug 2025 23:53:22 -0400 Subject: [PATCH] fix: don't reconnect when http client is closed --- interactions/api/gateway/gateway.py | 4 ++++ interactions/api/gateway/websocket.py | 10 +++++++++- interactions/api/http/http_client.py | 11 +++++++++++ 3 files changed, 24 insertions(+), 1 deletion(-) diff --git a/interactions/api/gateway/gateway.py b/interactions/api/gateway/gateway.py index 723d7e663..2ec949259 100644 --- a/interactions/api/gateway/gateway.py +++ b/interactions/api/gateway/gateway.py @@ -158,6 +158,10 @@ async def run(self) -> None: seq = msg.get("s") event = msg.get("t") + if op == MISSING: + # Internal no-op, ignore it. + continue + if seq: self.sequence = seq diff --git a/interactions/api/gateway/websocket.py b/interactions/api/gateway/websocket.py index 63cbec135..1979f859e 100644 --- a/interactions/api/gateway/websocket.py +++ b/interactions/api/gateway/websocket.py @@ -164,7 +164,7 @@ async def send_json(self, data: dict, bypass=False) -> None: serialized = FastJson.dumps(data) await self.send(serialized, bypass) - async def receive(self, force: bool = False) -> str: # noqa: C901 + async def receive(self, force: bool = False) -> dict: # noqa: C901 """ Receive a full event payload from the WebSocket. @@ -208,6 +208,14 @@ async def receive(self, force: bool = False) -> str: # noqa: C901 # is possible after all we can just wait for the event to be set. await self._closed.wait() else: + if self.state.client.http.closed: + # On aiohttp>=3.12.4, the aiohttp WebSocket client will always send a session + # close message when the underlying HTTP connection is closed. We want to + # ignore this to keep the behavior consistent with previous versions + # of Python, so we return this to indicate that no message was received. + # https://github.com/interactions-py/interactions.py/issues/1778 + return {"op": const.MISSING} + # This is an odd corner-case where the underlying socket connection was closed # unexpectedly without communicating the WebSocket closing handshake. We'll have # to reconnect ourselves. diff --git a/interactions/api/http/http_client.py b/interactions/api/http/http_client.py index 18a386dbf..57075e1a4 100644 --- a/interactions/api/http/http_client.py +++ b/interactions/api/http/http_client.py @@ -250,6 +250,17 @@ def __init__( logger = constants.get_logger() self.logger = logger + @property + def closed(self) -> bool: + """ + Returns whether the session is closed. + + Returns: + True if the session is closed, False otherwise. + + """ + return self.__session is None or self.__session.closed + def get_ratelimit(self, route: Route) -> BucketLock: """ Get a route's rate limit bucket.