From a42bebe5812dabccee574155d3a2bbbba9823bf8 Mon Sep 17 00:00:00 2001 From: Rapptz Date: Fri, 24 Jul 2020 08:05:23 -0400 Subject: [PATCH] Propagate exceptions when an unhandled error happens --- discord/shard.py | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/discord/shard.py b/discord/shard.py index 7659e5ec700c..e3835187bf46 100644 --- a/discord/shard.py +++ b/discord/shard.py @@ -45,6 +45,7 @@ class EventType: reconnect = 1 resume = 2 identify = 3 + terminate = 4 class EventItem: __slots__ = ('type', 'shard', 'error') @@ -139,6 +140,11 @@ async def worker(self): except self._handled_exceptions as e: await self._handle_disconnect(e) break + except asyncio.CancelledError: + break + except Exception as e: + self._queue.put_nowait(EventItem(EventType.terminate, self, e)) + break async def reidentify(self, exc): self._cancel_task() @@ -151,6 +157,10 @@ async def reidentify(self, exc): self.ws = await asyncio.wait_for(coro, timeout=60.0) except self._handled_exceptions as e: await self._handle_disconnect(e) + except asyncio.CancelledError: + return + except Exception as e: + self._queue.put_nowait(EventItem(EventType.terminate, self, e)) else: self.launch() @@ -161,6 +171,10 @@ async def reconnect(self): self.ws = await asyncio.wait_for(coro, timeout=60.0) except self._handled_exceptions as e: await self._handle_disconnect(e) + except asyncio.CancelledError: + return + except Exception as e: + self._queue.put_nowait(EventItem(EventType.terminate, self, e)) else: self.launch() @@ -312,6 +326,9 @@ async def connect(self, *, reconnect=True): await item.shard.reidentify(item.error) elif item.type == EventType.reconnect: await item.shard.reconnect() + elif item.type == EventType.terminate: + await self.close() + raise item.error async def close(self): """|coro|