Don't raise an exception during the signal handlers.
This commit is contained in:
parent
7b95611e62
commit
84c1eac62a
@ -55,12 +55,6 @@ from .appinfo import AppInfo
|
|||||||
|
|
||||||
log = logging.getLogger(__name__)
|
log = logging.getLogger(__name__)
|
||||||
|
|
||||||
class _ProperCleanup(Exception):
|
|
||||||
pass
|
|
||||||
|
|
||||||
def _raise_proper_cleanup():
|
|
||||||
raise _ProperCleanup
|
|
||||||
|
|
||||||
def _cancel_tasks(loop, tasks):
|
def _cancel_tasks(loop, tasks):
|
||||||
if not tasks:
|
if not tasks:
|
||||||
return
|
return
|
||||||
@ -503,6 +497,8 @@ class Client:
|
|||||||
def _do_cleanup(self):
|
def _do_cleanup(self):
|
||||||
log.info('Cleaning up event loop.')
|
log.info('Cleaning up event loop.')
|
||||||
loop = self.loop
|
loop = self.loop
|
||||||
|
if loop.is_closed():
|
||||||
|
return # we're already cleaning up
|
||||||
|
|
||||||
task = asyncio.ensure_future(self.close(), loop=loop)
|
task = asyncio.ensure_future(self.close(), loop=loop)
|
||||||
|
|
||||||
@ -553,15 +549,16 @@ 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, _raise_proper_cleanup)
|
loop.add_signal_handler(signal.SIGINT, self._do_cleanup)
|
||||||
loop.add_signal_handler(signal.SIGTERM, _raise_proper_cleanup)
|
loop.add_signal_handler(signal.SIGTERM, self._do_cleanup)
|
||||||
|
|
||||||
try:
|
try:
|
||||||
loop.run_until_complete(self.start(*args, **kwargs))
|
loop.run_until_complete(self.start(*args, **kwargs))
|
||||||
except (_ProperCleanup, 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:
|
||||||
self._do_cleanup()
|
if is_windows:
|
||||||
|
self._do_cleanup()
|
||||||
|
|
||||||
# properties
|
# properties
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user