Kill all subprocess in signal handler

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ář <lsedlar@redhat.com>
This commit is contained in:
Lubomír Sedlář 2020-11-09 08:45:57 +01:00
parent 4c4c816e70
commit 99c1e2eb5e
1 changed files with 13 additions and 0 deletions

View File

@ -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)