From e2f42597a5d81c048ac926c434e81f3997fedb7e Mon Sep 17 00:00:00 2001 From: Rapptz Date: Sun, 3 May 2020 01:28:29 -0400 Subject: Handle Connection Reset by Peer connection errors. This should work both on Windows and on Linux. Apparently these types of blips are considered normal for Discord. So rather than letting the reconnect logic handler expect these to be catastrophic, it should handle it specially so it doesn't waste an IDENTIFY for what ultimately should just be a small networking blip. This also makes it less noisy for the end-user as these complaints happen from time to time. --- discord/gateway.py | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) (limited to 'discord/gateway.py') diff --git a/discord/gateway.py b/discord/gateway.py index 3f92ec1f..f262477f 100644 --- a/discord/gateway.py +++ b/discord/gateway.py @@ -508,16 +508,21 @@ class DiscordWebSocket: elif msg.type is aiohttp.WSMsgType.ERROR: log.debug('Received %s', msg) raise msg.data - elif msg.type in (aiohttp.WSMsgType.CLOSED, aiohttp.WSMsgType.CLOSE): + elif msg.type in (aiohttp.WSMsgType.CLOSED, aiohttp.WSMsgType.CLOSING, aiohttp.WSMsgType.CLOSE): log.debug('Received %s', msg) raise WebSocketClosure - except WebSocketClosure as e: + except WebSocketClosure: + # Ensure the keep alive handler is closed + if self._keep_alive: + self._keep_alive.stop() + self._keep_alive = None + if self._can_handle_close(): log.info('Websocket closed with %s, attempting a reconnect.', self.socket.close_code) - raise ReconnectWebSocket(self.shard_id) from e + raise ReconnectWebSocket(self.shard_id) from None elif self.socket.close_code is not None: log.info('Websocket closed with %s, cannot reconnect.', self.socket.close_code) - raise ConnectionClosed(self.socket, shard_id=self.shard_id) from e + raise ConnectionClosed(self.socket, shard_id=self.shard_id) from None async def send(self, data): self._dispatch('socket_raw_send', data) @@ -598,6 +603,7 @@ class DiscordWebSocket: async def close(self, code=4000): if self._keep_alive: self._keep_alive.stop() + self._keep_alive = None await self.socket.close(code=code) -- cgit v1.2.3