Skip to content

Commit ee5790c

Browse files
committed
fix: attempt resume on websocket closure with close_code = 1000 in edge cases (#1241)
1 parent 95c8f32 commit ee5790c

File tree

2 files changed

+16
-0
lines changed

2 files changed

+16
-0
lines changed

changelog/1241.bugfix.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Attempt to handle abrupt websocket closures on ``aiohttp >= 3.9.0`` and ``python < 3.11.0`` gracefully.

disnake/gateway.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -687,6 +687,21 @@ def latency(self) -> float:
687687
return float("inf") if heartbeat is None else heartbeat.latency
688688

689689
def _can_handle_close(self) -> bool:
690+
# bandaid fix for https://github.com/aio-libs/aiohttp/issues/8138
691+
# tl;dr: on aiohttp >= 3.9.0 and python < 3.11.0, aiohttp returns close code 1000 (OK)
692+
# on abrupt connection loss, not 1006 (ABNORMAL_CLOSURE) like one would expect, ultimately
693+
# due to faulty ssl lifecycle handling in cpython.
694+
# If we end up in a situation where the close code is 1000 but we didn't
695+
# initiate the closure (i.e. `self._close_code` isn't set), assume this has happened and
696+
# try to reconnect.
697+
if self._close_code is None and self.socket.close_code == 1000:
698+
_log.info(
699+
"Websocket remote in shard ID %s closed with %s. Assuming the connection dropped.",
700+
self.shard_id,
701+
self.socket.close_code,
702+
)
703+
return True # consider this a reconnectable close code
704+
690705
code = self._close_code or self.socket.close_code
691706
return code not in (1000, 4004, 4010, 4011, 4012, 4013, 4014)
692707

0 commit comments

Comments
 (0)