From 34cf6900cafc0da77d7959275f24ef69929f717d Mon Sep 17 00:00:00 2001 From: Jan Kiszka Date: Wed, 28 Jun 2017 12:43:51 +0200 Subject: [PATCH] Terminate process group when terminating kas This addresses the case that someone calls sys.exit() while some processes are still running. Signed-off-by: Jan Kiszka --- kas/kas.py | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/kas/kas.py b/kas/kas.py index 22389b5..a3af239 100644 --- a/kas/kas.py +++ b/kas/kas.py @@ -27,8 +27,11 @@ """ import argparse +import atexit +import asyncio import traceback import logging +import signal import sys import os import pkg_resources @@ -72,6 +75,13 @@ def create_logger(): return logging.getLogger(__name__) +def _atexit_handler(loop): + """ + Terminate the whole process group + """ + os.killpg(os.getpid(), signal.SIGTERM) + + def kas(argv): """ The main entry point of kas. @@ -99,6 +109,10 @@ def kas(argv): if args.debug: logging.getLogger().setLevel(logging.DEBUG) + loop = asyncio.get_event_loop() + + atexit.register(_atexit_handler, loop=loop) + for cmd in sub_cmds: if cmd.run(args): return