From 99c1e2eb5e55a3f9dbb978b1d4021cdade54d2f4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lubom=C3=ADr=20Sedl=C3=A1=C5=99?= Date: Mon, 9 Nov 2020 08:45:57 +0100 Subject: [PATCH] Kill all subprocess in signal handler MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit When Pungi receives a signal to terminate, it can sometimes get stuck if there are threads running. It has to wait for all worker threads to finish. They generally do finish, unless they get stuck waiting on a subprocess. This patch should reduce the likelihood of this happening by stopping all subprocesses. JIRA: RHELCMP-3056 Signed-off-by: Lubomír Sedlář --- pungi/scripts/pungi_koji.py | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/pungi/scripts/pungi_koji.py b/pungi/scripts/pungi_koji.py index 6fec647f..32638460 100644 --- a/pungi/scripts/pungi_koji.py +++ b/pungi/scripts/pungi_koji.py @@ -14,6 +14,7 @@ import signal import sys import traceback import shutil +import subprocess from six.moves import shlex_quote @@ -581,8 +582,20 @@ def run_compose( compose.log_info("Compose finished: %s" % compose.topdir) +def try_kill_children(signal): + try: + if COMPOSE: + COMPOSE.log_warning("Trying to kill all subprocesses") + pid = os.getpid() + subprocess.call(["pkill", "-P", str(pid)]) + except Exception: + if COMPOSE: + COMPOSE.log_warning("Failed to kill all subprocesses") + + def sigterm_handler(signum, frame): if COMPOSE: + try_kill_children(signum) COMPOSE.log_error("Compose run failed: signal %s" % signum) COMPOSE.log_error("Traceback:\n%s" % "\n".join(traceback.format_stack(frame))) COMPOSE.log_critical("Compose failed: %s" % COMPOSE.topdir)