From f26313367e55ebf7555dfc97aad40682dfc71d13 Mon Sep 17 00:00:00 2001 From: black-sliver <59490463+black-sliver@users.noreply.github.com> Date: Tue, 3 Mar 2026 23:02:12 +0000 Subject: [PATCH] MultiServer: graceful shutdown for ctrl+c and sigterm (#5996) --- MultiServer.py | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/MultiServer.py b/MultiServer.py index ed50c98db617..0ba5adaf4025 100644 --- a/MultiServer.py +++ b/MultiServer.py @@ -21,6 +21,7 @@ import typing import weakref import zlib +from signal import SIGINT, SIGTERM import ModuleUpdate @@ -2571,6 +2572,8 @@ async def console(ctx: Context): input_text = await queue.get() queue.task_done() ctx.commandprocessor(input_text) + except asyncio.exceptions.CancelledError: + ctx.logger.info("ConsoleTask cancelled") except: import traceback traceback.print_exc() @@ -2737,6 +2740,15 @@ async def main(args: argparse.Namespace): console_task = asyncio.create_task(console(ctx)) if ctx.auto_shutdown: ctx.shutdown_task = asyncio.create_task(auto_shutdown(ctx, [console_task])) + + def stop(): + for remove_signal in [SIGINT, SIGTERM]: + asyncio.get_event_loop().remove_signal_handler(remove_signal) + ctx.commandprocessor._cmd_exit() + + for signal in [SIGINT, SIGTERM]: + asyncio.get_event_loop().add_signal_handler(signal, stop) + await ctx.exit_event.wait() console_task.cancel() if ctx.shutdown_task: