Enhances signal handling, adds force exit capability

This commit is contained in:
Mose Müller 2023-10-19 10:59:00 +02:00
parent 0a77cc1f36
commit 1bc2bb3605

View File

@ -358,20 +358,16 @@ class Server:
# Signals can only be listened to from the main thread.
return
try:
for sig in HANDLED_SIGNALS:
self._loop.add_signal_handler(sig, self.handle_exit, sig, None)
except NotImplementedError:
# Windows
for sig in HANDLED_SIGNALS:
signal.signal(sig, self.handle_exit)
for sig in HANDLED_SIGNALS:
signal.signal(sig, self.handle_exit)
def handle_exit(self, sig: int = 0, frame: Optional[FrameType] = None) -> None:
logger.info("Handling exit")
if self.should_exit and sig == signal.SIGINT:
self.force_exit = True
logger.warning(f"Received signal {sig}, forcing exit...")
os._exit(1)
else:
self.should_exit = True
logger.warning(f"Received signal {sig}, exiting...")
def custom_exception_handler(
self, loop: asyncio.AbstractEventLoop, context: dict[str, Any]