You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@buildstream.apache.org by ro...@apache.org on 2020/12/29 13:41:33 UTC

[buildstream] 01/10: utils.py: Catch correctly if a process is dead when trying to kill it

This is an automated email from the ASF dual-hosted git repository.

root pushed a commit to branch bschubert/remove-parent-child-pipe
in repository https://gitbox.apache.org/repos/asf/buildstream.git

commit a23223d1527b2342020774e19a62a7b595c606f1
Author: Benjamin Schubert <co...@benschubert.me>
AuthorDate: Sat Aug 29 09:32:48 2020 +0000

    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
---
 src/buildstream/utils.py | 14 ++++++++++++--
 1 file changed, 12 insertions(+), 2 deletions(-)

diff --git a/src/buildstream/utils.py b/src/buildstream/utils.py
index 9c6761c..438543b 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: