Use subprocess_terminate on Runtime close
[deliverable/lttng-ivc.git] / lttng_ivc / utils / runtime.py
index a409c192314c69fdf581fe2a61d57502f8033de7..4d367d3c170a352df9042589d482220e4271a932 100644 (file)
@@ -7,7 +7,6 @@ import logging
 import shutil
 import contextlib
 import pprint
-import traceback
 
 from tempfile import TemporaryDirectory
 
@@ -47,9 +46,10 @@ class Runtime(object):
         self._runtime_log_aggregation = os.path.join(self.__runtime_log, "runtime.log")
 
         self._run_command_count = 0
+        self._is_test_modules_loaded = False
 
         self.special_env_variables = {"LTTNG_UST_DEBUG": "1",
-                                      #"LTTNG_APP_SOCKET_TIMEOUT": "-1",
+                                      "LTTNG_APP_SOCKET_TIMEOUT": "-1",
                                       #"LTTNG_UST_REGISTER_TIMEOUT": "-1",
                                       "LTTNG_NETWORK_SOCKET_TIMEOUT": "-1"}
 
@@ -67,16 +67,26 @@ class Runtime(object):
     def add_project(self, project):
         self.__projects.append(project)
 
+    def remove_project(self, project):
+        self.__projects.remove(project)
+
     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):
@@ -88,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
@@ -237,20 +258,16 @@ class Runtime(object):
     def load_test_module(self):
         # Base directory is provided by env
         self.run("modprobe lttng-test")
+        self._is_test_modules_loaded = True
 
     def unload_test_module(self, check_return=True):
         # Base directory is provided by env
-        self.run("modprobe -r lttng-test", check_return=check_return)
+        if self._is_test_modules_loaded:
+            self.run("modprobe -r lttng-test", check_return=check_return)
 
     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.
@@ -259,4 +276,3 @@ class Runtime(object):
         # Copy the lttng_home used at runtime using hardlink to prevent useless
         # data duplication
         shutil.copytree(self.lttng_home, self.__post_runtime_lttng_home_path, copy_function=os.link)
-
This page took 0.024466 seconds and 5 git commands to generate.