Use subprocess_terminate on Runtime close
[deliverable/lttng-ivc.git] / lttng_ivc / utils / runtime.py
index 9efdabee085b0bc7d4c33ec5160209002eddef93..4d367d3c170a352df9042589d482220e4271a932 100644 (file)
@@ -73,13 +73,20 @@ class Runtime(object):
     def subprocess_signal(self, subprocess_uuid, signal):
         self.__subproces[subprocess_uuid].send_signal(signal)
 
-    def subprocess_terminate(self, subprocess_uuid, timeout=60):
+    def subprocess_terminate(self, subprocess_uuid, timeout=60, check_return=True):
         process = self.__subprocess[subprocess_uuid]
         process.terminate()
-        process.wait(timeout)
+        try:
+            process.wait(timeout)
+        except subprocess.TimeoutExpired:
+            # Force kill
+            return self.subprocess_kill(subprocess_uuid)
         stdout, stderr = self.__stdout_stderr[subprocess_uuid]
         stdout.close()
         stderr.close()
+        if check_return:
+            if process.returncode != 0:
+                raise subprocess.CalledProcessError(process.returncode, process.args)
         return process
 
     def subprocess_kill(self, subprocess_uuid):
@@ -91,6 +98,17 @@ class Runtime(object):
         stderr.close()
         return process
 
+    def subprocess_wait(self, subprocess_uuid, check_return=True):
+        process = self.__subprocess[subprocess_uuid]
+        process.wait()
+        stdout, stderr = self.__stdout_stderr[subprocess_uuid]
+        stdout.close()
+        stderr.close()
+        if check_return:
+            if process.returncode != 0:
+                raise subprocess.CalledProcessError(process.returncode, process.args)
+        return process
+
     def get_subprocess_stdout_path(self, subprocess_uuid):
         stdout, stderr = self.__stdout_stderr[subprocess_uuid]
         return stdout.name
@@ -249,13 +267,7 @@ class Runtime(object):
 
     def close(self):
         for key, subp in self.__subprocess.items():
-            subp.terminate()
-        for key, subp in self.__subprocess.items():
-            # TODO move timeout to settings
-            subp.wait(timeout=60)
-        for key, (stdout, stderr) in self.__stdout_stderr.items():
-            stdout.close()
-            stderr.close()
+            self.subprocess_terminate(key, check_return=False)
 
         # Always try to remove test module but do not perform check on return
         # value.
This page took 0.030868 seconds and 5 git commands to generate.