aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRapptz <[email protected]>2020-05-24 04:05:09 -0400
committerRapptz <[email protected]>2020-07-25 09:59:40 -0400
commite0660ef8a670cbcf0e5f250400fd9e604803dfd1 (patch)
tree6ebd9dc3964a34c7099deecda1bac30e216d02b5
parentIf we're out of retries just raise the OSError (diff)
downloaddiscord.py-e0660ef8a670cbcf0e5f250400fd9e604803dfd1.tar.xz
discord.py-e0660ef8a670cbcf0e5f250400fd9e604803dfd1.zip
Add a timeout for receiving websocket messages.
-rw-r--r--discord/gateway.py12
1 files changed, 8 insertions, 4 deletions
diff --git a/discord/gateway.py b/discord/gateway.py
index f262477f..56c1be9e 100644
--- a/discord/gateway.py
+++ b/discord/gateway.py
@@ -96,7 +96,7 @@ class KeepAliveHandler(threading.Thread):
try:
f.result()
except Exception:
- pass
+ log.exception('An error occurred while stopping the gateway. Ignoring.')
finally:
self.stop()
return
@@ -500,7 +500,7 @@ class DiscordWebSocket:
The websocket connection was terminated for unhandled reasons.
"""
try:
- msg = await self.socket.receive()
+ msg = await self.socket.receive(timeout=self._max_heartbeat_timeout)
if msg.type is aiohttp.WSMsgType.TEXT:
await self.received_message(msg.data)
elif msg.type is aiohttp.WSMsgType.BINARY:
@@ -511,12 +511,16 @@ class DiscordWebSocket:
elif msg.type in (aiohttp.WSMsgType.CLOSED, aiohttp.WSMsgType.CLOSING, aiohttp.WSMsgType.CLOSE):
log.debug('Received %s', msg)
raise WebSocketClosure
- except WebSocketClosure:
+ except (asyncio.TimeoutError, WebSocketClosure) as e:
# Ensure the keep alive handler is closed
if self._keep_alive:
self._keep_alive.stop()
self._keep_alive = None
+ if isinstance(e, asyncio.TimeoutError):
+ log.info('Timed out receiving packet. Attempting a reconnect.')
+ raise ReconnectWebSocket(self.shard_id) from 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 None
@@ -819,7 +823,7 @@ class DiscordVoiceWebSocket:
elif msg.type is aiohttp.WSMsgType.ERROR:
log.debug('Received %s', msg)
raise ConnectionClosed(self.ws, shard_id=None) from msg.data
- elif msg.type in (aiohttp.WSMsgType.CLOSED, aiohttp.WSMsgType.CLOSE):
+ elif msg.type in (aiohttp.WSMsgType.CLOSED, aiohttp.WSMsgType.CLOSE, aiohttp.WSMsgType.CLOSING):
log.debug('Received %s', msg)
raise ConnectionClosed(self.ws, shard_id=None)