From efcff9b5f7531ef95837dfb5c9cf7d5f7d11e481 Mon Sep 17 00:00:00 2001 From: Matthew Jones Date: Wed, 5 Aug 2015 11:20:48 -0400 Subject: [PATCH 1/2] Fix psutil usage on el6 for job cancel psutil will fail to import on el6 due to not being able to access a pseudo terminal. This issues a SIGKILL to the proot process in order to force the stop --- awx/main/tasks.py | 20 +++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-) diff --git a/awx/main/tasks.py b/awx/main/tasks.py index d3ca5280de..453038c8fc 100644 --- a/awx/main/tasks.py +++ b/awx/main/tasks.py @@ -23,7 +23,10 @@ import uuid from distutils.version import LooseVersion as Version import dateutil.parser import yaml -import psutil +try: + import psutil +except: + psutil = None # Pexpect import pexpect @@ -434,13 +437,16 @@ class BaseTask(Task): if instance.cancel_flag: try: if settings.AWX_PROOT_ENABLED: - main_proc = psutil.Process(pid=child.pid) - if hasattr(main_proc, "children"): - child_procs = main_proc.children(recursive=False) + if not psutil: + os.kill(child.pid, signal.SIGKILL) else: - child_procs = main_proc.get_children(recursive=False) - for child_proc in child_procs: - os.kill(child_proc.pid, signal.SIGTERM) + main_proc = psutil.Process(pid=child.pid) + if hasattr(main_proc, "children"): + child_procs = main_proc.children(recursive=False) + else: + child_procs = main_proc.get_children(recursive=False) + for child_proc in child_procs: + os.kill(child_proc.pid, signal.SIGTERM) else: os.kill(child.pid, signal.SIGTERM) time.sleep(3) From ab042c269b807511856eba09ad368f9a8a3a624a Mon Sep 17 00:00:00 2001 From: Matthew Jones Date: Wed, 5 Aug 2015 11:44:50 -0400 Subject: [PATCH 2/2] Set recursive on child process canceling Sometimes ansible spawns many subprocesses that can get orphaned and stuck if we only kill the direct descendent child processes --- awx/main/tasks.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/awx/main/tasks.py b/awx/main/tasks.py index 453038c8fc..b8449d33f7 100644 --- a/awx/main/tasks.py +++ b/awx/main/tasks.py @@ -442,9 +442,9 @@ class BaseTask(Task): else: main_proc = psutil.Process(pid=child.pid) if hasattr(main_proc, "children"): - child_procs = main_proc.children(recursive=False) + child_procs = main_proc.children(recursive=True) else: - child_procs = main_proc.get_children(recursive=False) + child_procs = main_proc.get_children(recursive=True) for child_proc in child_procs: os.kill(child_proc.pid, signal.SIGTERM) else: