summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBenjamin Schubert <contact@benschubert.me>2020-08-29 09:32:48 +0000
committerJürg Billeter <j@bitron.ch>2020-09-14 17:38:45 +0200
commitee3f7674585041f0495347959f5fe35b5e22c487 (patch)
treea571321f6bb0e98fe110ecc6fd1fd2d727f12138
parent06d4789e0f8689998b5bb698cd67f881e96c8017 (diff)
downloadbuildstream-ee3f7674585041f0495347959f5fe35b5e22c487.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: