Fix cleanup code on Linux not working properly.
This commit is contained in:
parent
dcdcf1adac
commit
e77dec85e9
@ -62,8 +62,21 @@ def _cancel_tasks(loop, tasks):
|
|||||||
log.info('Cleaning up after %d tasks.', len(tasks))
|
log.info('Cleaning up after %d tasks.', len(tasks))
|
||||||
gathered = asyncio.gather(*tasks, loop=loop, return_exceptions=True)
|
gathered = asyncio.gather(*tasks, loop=loop, return_exceptions=True)
|
||||||
gathered.cancel()
|
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():
|
while not gathered.done():
|
||||||
loop.run_forever()
|
loop.run_forever()
|
||||||
|
|
||||||
@ -501,10 +514,6 @@ class Client:
|
|||||||
if loop.is_closed():
|
if loop.is_closed():
|
||||||
return # we're already cleaning up
|
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)
|
task = asyncio.ensure_future(self.close(), loop=loop)
|
||||||
|
|
||||||
def stop_loop(fut):
|
def stop_loop(fut):
|
||||||
@ -554,8 +563,8 @@ class Client:
|
|||||||
is_windows = sys.platform == 'win32'
|
is_windows = sys.platform == 'win32'
|
||||||
loop = self.loop
|
loop = self.loop
|
||||||
if not is_windows:
|
if not is_windows:
|
||||||
loop.add_signal_handler(signal.SIGINT, self._do_cleanup)
|
loop.add_signal_handler(signal.SIGINT, lambda: loop.stop())
|
||||||
loop.add_signal_handler(signal.SIGTERM, self._do_cleanup)
|
loop.add_signal_handler(signal.SIGTERM, lambda: loop.stop())
|
||||||
|
|
||||||
future = asyncio.ensure_future(self.start(*args, **kwargs), loop=loop)
|
future = asyncio.ensure_future(self.start(*args, **kwargs), loop=loop)
|
||||||
future.add_done_callback(lambda f: loop.stop())
|
future.add_done_callback(lambda f: loop.stop())
|
||||||
@ -565,7 +574,6 @@ class Client:
|
|||||||
except KeyboardInterrupt:
|
except KeyboardInterrupt:
|
||||||
log.info('Received signal to terminate bot and event loop.')
|
log.info('Received signal to terminate bot and event loop.')
|
||||||
finally:
|
finally:
|
||||||
if is_windows:
|
|
||||||
self._do_cleanup()
|
self._do_cleanup()
|
||||||
|
|
||||||
# properties
|
# properties
|
||||||
|
Loading…
x
Reference in New Issue
Block a user