From 3bcfa4a70e8aeca6b9c6929723ffc8055b836718 Mon Sep 17 00:00:00 2001 From: Zvonimir Sabljic Date: Sat, 5 Aug 2023 08:36:12 +0200 Subject: [PATCH] Kill the process and the subprocesses after running a command + add both stdout and stderr to the return value of running a command --- euclid/helpers/cli.py | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/euclid/helpers/cli.py b/euclid/helpers/cli.py index 3289d06..d16f8e0 100644 --- a/euclid/helpers/cli.py +++ b/euclid/helpers/cli.py @@ -24,6 +24,7 @@ def run_command(command, root_path, q_stdout, q_stderr, pid_container): stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True, + preexec_fn=os.setsid, cwd=root_path ) pid_container[0] = process.pid @@ -78,7 +79,7 @@ def execute_command(project, command, timeout=5000): # If timeout is reached, kill the process if elapsed_time * 1000 > timeout: - os.kill(pid_container[0], signal.SIGKILL) + os.killpg(pid_container[0], signal.SIGKILL) break try: @@ -95,7 +96,10 @@ def execute_command(project, command, timeout=5000): stderr_output += q_stderr.get_nowait() if return_value is None: - return_value = output[-2000:] if output != '' else stderr_output[-2000:] + return_value = '' + if stderr_output != '': + return_value = 'stderr:\n```\n' + stderr_output[-2000:] + '\n```\n' + return_value += 'stdout:\n```\n' + output[-2000:] + '\n```' command_run = save_command_run(project, command, return_value)