Skip to content

Commit 55d3c59

Browse files
committed
fix: don't reconnect when http client is closed
1 parent 0d11f77 commit 55d3c59

File tree

3 files changed

+24
-1
lines changed

3 files changed

+24
-1
lines changed

interactions/api/gateway/gateway.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,10 @@ async def run(self) -> None:
158158
seq = msg.get("s")
159159
event = msg.get("t")
160160

161+
if op == MISSING:
162+
# Internal no-op, ignore it.
163+
continue
164+
161165
if seq:
162166
self.sequence = seq
163167

interactions/api/gateway/websocket.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,7 @@ async def send_json(self, data: dict, bypass=False) -> None:
164164
serialized = FastJson.dumps(data)
165165
await self.send(serialized, bypass)
166166

167-
async def receive(self, force: bool = False) -> str: # noqa: C901
167+
async def receive(self, force: bool = False) -> dict: # noqa: C901
168168
"""
169169
Receive a full event payload from the WebSocket.
170170
@@ -208,6 +208,14 @@ async def receive(self, force: bool = False) -> str: # noqa: C901
208208
# is possible after all we can just wait for the event to be set.
209209
await self._closed.wait()
210210
else:
211+
if self.state.client.http.closed:
212+
# On aiohttp>=3.12.4, the aiohttp WebSocket client will always send a session
213+
# close message when the underlying HTTP connection is closed. We want to
214+
# ignore this to keep the behavior consistent with previous versions
215+
# of Python, so we return this to indicate that no message was received.
216+
# https://github.com/interactions-py/interactions.py/issues/1778
217+
return {"op": const.MISSING}
218+
211219
# This is an odd corner-case where the underlying socket connection was closed
212220
# unexpectedly without communicating the WebSocket closing handshake. We'll have
213221
# to reconnect ourselves.

interactions/api/http/http_client.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -250,6 +250,17 @@ def __init__(
250250
logger = constants.get_logger()
251251
self.logger = logger
252252

253+
@property
254+
def closed(self) -> bool:
255+
"""
256+
Returns whether the session is closed.
257+
258+
Returns:
259+
True if the session is closed, False otherwise.
260+
261+
"""
262+
return self.__session is None or self.__session.closed
263+
253264
def get_ratelimit(self, route: Route) -> BucketLock:
254265
"""
255266
Get a route's rate limit bucket.

0 commit comments

Comments
 (0)