summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBenjamin Schubert <contact@benschubert.me>2020-08-29 09:32:48 +0000
committerBenjamin Schubert <contact@benschubert.me>2020-08-30 14:29:24 +0000
commita23223d1527b2342020774e19a62a7b595c606f1 (patch)
tree4588e97386eb41eaffa82e054ad43954bf007a32
parentee927b2c03806da80b9dc09e24a937e886700071 (diff)
downloadbuildstream-a23223d1527b2342020774e19a62a7b595c606f1.tar.gz
utils.py: Catch correctly if a process is dead when trying to kill it
Before, we would throw an exception that the process is not dead yet if it happened to die at the exact moment we tried to access it. This ensures we don't throw exceptions in such cases, since the process would be already dead, as we wanted
-rw-r--r--src/buildstream/utils.py14
1 files changed, 12 insertions, 2 deletions
diff --git a/src/buildstream/utils.py b/src/buildstream/utils.py
index 9c6761ccc..438543b83 100644
--- a/src/buildstream/utils.py
+++ b/src/buildstream/utils.py
@@ -1295,7 +1295,12 @@ def _tempnamedfile_name(dir): # pylint: disable=redefined-builtin
# pid (int): Process ID
#
def _kill_process_tree(pid):
- proc = psutil.Process(pid)
+ try:
+ proc = psutil.Process(pid)
+ except psutil.NoSuchProcess:
+ # Process already died, we're good
+ return
+
children = proc.children(recursive=True)
def kill_proc(p):
@@ -1352,7 +1357,12 @@ def _call(*popenargs, terminate=False, **kwargs):
# Some callers know that their subprocess can be
# gracefully terminated, make an attempt first
if terminate:
- proc = psutil.Process(process.pid)
+ try:
+ proc = psutil.Process(process.pid)
+ except psutil.NoSuchProcess:
+ # Nothing to do, the process already terminated
+ return
+
proc.terminate()
try: