Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: attempt resume on websocket closure with close_code = 1000 in edge cases #1241

Merged
merged 4 commits into from
Nov 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions changelog/1241.bugfix.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Attempt to handle abrupt websocket closures on ``aiohttp >= 3.9.0`` and ``python < 3.11.0`` gracefully.
15 changes: 15 additions & 0 deletions disnake/gateway.py
Original file line number Diff line number Diff line change
Expand Up @@ -687,6 +687,21 @@ def latency(self) -> float:
return float("inf") if heartbeat is None else heartbeat.latency

def _can_handle_close(self) -> bool:
# bandaid fix for https://github.com/aio-libs/aiohttp/issues/8138
# tl;dr: on aiohttp >= 3.9.0 and python < 3.11.0, aiohttp returns close code 1000 (OK)
# on abrupt connection loss, not 1006 (ABNORMAL_CLOSURE) like one would expect, ultimately
# due to faulty ssl lifecycle handling in cpython.
# If we end up in a situation where the close code is 1000 but we didn't
# initiate the closure (i.e. `self._close_code` isn't set), assume this has happened and
# try to reconnect.
if self._close_code is None and self.socket.close_code == 1000:
_log.info(
"Websocket remote in shard ID %s closed with %s. Assuming the connection dropped.",
self.shard_id,
self.socket.close_code,
)
return True # consider this a reconnectable close code

code = self._close_code or self.socket.close_code
return code not in (1000, 4004, 4010, 4011, 4012, 4013, 4014)

Expand Down
Loading