X-Git-Url: https://git.efficios.com/?a=blobdiff_plain;f=lttng_ivc%2Futils%2Fruntime.py;h=d8a2de62018e52a8ccffba8b1a90358f392fc129;hb=686e86ab91dc79dcef6b2d335a6377492b876cb1;hp=3a776cbf2169d348a6f9b78fe6142b80e5802efe;hpb=72429717029af4f5ba6d8df4d12f8ec96770e524;p=deliverable%2Flttng-ivc.git diff --git a/lttng_ivc/utils/runtime.py b/lttng_ivc/utils/runtime.py index 3a776cb..d8a2de6 100644 --- a/lttng_ivc/utils/runtime.py +++ b/lttng_ivc/utils/runtime.py @@ -46,6 +46,7 @@ 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", @@ -72,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): @@ -90,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 @@ -122,7 +141,7 @@ class Runtime(object): _logger.debug("Spawned sub pid: {} args: {} stdout: {} stderr{}".format(p.pid, p.args, out_path, err_path)) return tmp_id - def run(self, command_line, cwd=None, check_return=True, ld_preload="", classpath=""): + def run(self, command_line, cwd=None, check_return=True, ld_preload="", classpath="", timeout=None): """ Run the command and return a tuple of a (CompletedProcess, stdout_path, stderr_path). The subprocess is already executed and returned. The @@ -154,7 +173,8 @@ class Runtime(object): for key, value in env.items(): env_out.write('{}={}\n'.format(key, value)) - cp = subprocess.run(args, stdout=stdout, stderr=stderr, env=env, cwd=cwd) + cp = subprocess.run(args, stdout=stdout, stderr=stderr, env=env, + cwd=cwd, timeout=timeout) _logger.debug("Command #{} args: {} stdout: {} stderr{}".format(tmp_id, cp.args, out_path, err_path)) # Add to the global log file. This can help a little. Leave the other @@ -239,20 +259,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.