aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRapptz <[email protected]>2019-04-09 00:56:36 -0400
committerRapptz <[email protected]>2019-04-09 00:56:36 -0400
commite77dec85e923745a0468f197d56ad6f948b368c0 (patch)
tree97ee37551f987a90ff1b3c4da5494be73949a8ab
parentStop the event loop if it's running during cleanup. (diff)
downloaddiscord.py-e77dec85e923745a0468f197d56ad6f948b368c0.tar.xz
discord.py-e77dec85e923745a0468f197d56ad6f948b368c0.zip
Fix cleanup code on Linux not working properly.
-rw-r--r--discord/client.py26
1 files changed, 17 insertions, 9 deletions
diff --git a/discord/client.py b/discord/client.py
index 2ce85d27..008d44fe 100644
--- a/discord/client.py
+++ b/discord/client.py
@@ -62,8 +62,21 @@ def _cancel_tasks(loop, tasks):
log.info('Cleaning up after %d tasks.', len(tasks))
gathered = asyncio.gather(*tasks, loop=loop, return_exceptions=True)
gathered.cancel()
- gathered.add_done_callback(lambda fut: loop.stop())
+ def stop_and_silence(fut):
+ loop.stop()
+ try:
+ fut.result()
+ except asyncio.CancelledError:
+ pass
+ except Exception as e:
+ loop.call_exception_handler({
+ 'message': 'Unhandled exception during Client.run shutdown.',
+ 'exception': e,
+ 'future': fut
+ })
+
+ gathered.add_done_callback(stop_and_silence)
while not gathered.done():
loop.run_forever()
@@ -501,10 +514,6 @@ class Client:
if loop.is_closed():
return # we're already cleaning up
- # Stop the event loop if it's running
- if loop.is_running():
- loop.stop()
-
task = asyncio.ensure_future(self.close(), loop=loop)
def stop_loop(fut):
@@ -554,8 +563,8 @@ class Client:
is_windows = sys.platform == 'win32'
loop = self.loop
if not is_windows:
- loop.add_signal_handler(signal.SIGINT, self._do_cleanup)
- loop.add_signal_handler(signal.SIGTERM, self._do_cleanup)
+ loop.add_signal_handler(signal.SIGINT, lambda: loop.stop())
+ loop.add_signal_handler(signal.SIGTERM, lambda: loop.stop())
future = asyncio.ensure_future(self.start(*args, **kwargs), loop=loop)
future.add_done_callback(lambda f: loop.stop())
@@ -565,8 +574,7 @@ class Client:
except KeyboardInterrupt:
log.info('Received signal to terminate bot and event loop.')
finally:
- if is_windows:
- self._do_cleanup()
+ self._do_cleanup()
# properties