From d636d86219ee299f92e16ca1a3415fd6d2d0f9f6 Mon Sep 17 00:00:00 2001 From: scooobie <11851190+scoobie-bot@users.noreply.github.com> Date: Thu, 24 Aug 2023 12:28:54 +0800 Subject: [PATCH 1/3] Update cli.py had issues using setsid on Windows, so this detects windows/Unix and uses the correct version of run_command --- pilot/helpers/cli.py | 32 ++++++++++++++++++++++++++++++-- 1 file changed, 30 insertions(+), 2 deletions(-) diff --git a/pilot/helpers/cli.py b/pilot/helpers/cli.py index c34d8a1..eaeae28 100644 --- a/pilot/helpers/cli.py +++ b/pilot/helpers/cli.py @@ -21,15 +21,22 @@ def enqueue_output(out, q): break q.put(line) out.close() - + +# had issues using setsid on Wondows, so this detects windows/Unix and uses the correct version of run_command def run_command(command, root_path, q_stdout, q_stderr, pid_container): + if platform.system() == 'Windows': # Check the operating system + return run_command_windows(command, root_path, q_stdout, q_stderr, pid_container) + else: + return run_command_unix(command, root_path, q_stdout, q_stderr, pid_container) + +def run_command_windows(command, root_path, q_stdout, q_stderr, pid_container): + # Windows-specific implementation using subprocess process = subprocess.Popen( command, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True, - preexec_fn=os.setsid, cwd=root_path ) pid_container[0] = process.pid @@ -41,6 +48,27 @@ def run_command(command, root_path, q_stdout, q_stderr, pid_container): t_stderr.start() return process +def run_command_unix(command, root_path, q_stdout, q_stderr, pid_container): + # Unix-like systems implementation using os.setsid + process = subprocess.Popen( + command, + shell=True, + stdout=subprocess.PIPE, + stderr=subprocess.PIPE, + text=True, + preexec_fn=os.setsid, # Use os.setsid only for Unix-like systems + cwd=root_path + ) + pid_container[0] = process.pid + t_stdout = threading.Thread(target=enqueue_output, args=(process.stdout, q_stdout)) + t_stderr = threading.Thread(target=enqueue_output, args=(process.stderr, q_stderr)) + t_stdout.daemon = True + t_stderr.daemon = True + t_stdout.start() + t_stderr.start() + return process + + def execute_command(project, command, timeout=None, force=False): if timeout is not None: From 1df39a2cb5ea6e0a4db6f15aabc87d653c789326 Mon Sep 17 00:00:00 2001 From: scooobie <11851190+scoobie-bot@users.noreply.github.com> Date: Thu, 24 Aug 2023 13:34:41 +0800 Subject: [PATCH 2/3] Update cli.py added windows version of the os.killpg --- pilot/helpers/cli.py | 24 ++++++++++++++++++++---- 1 file changed, 20 insertions(+), 4 deletions(-) diff --git a/pilot/helpers/cli.py b/pilot/helpers/cli.py index eaeae28..08a85c9 100644 --- a/pilot/helpers/cli.py +++ b/pilot/helpers/cli.py @@ -5,6 +5,7 @@ import threading import queue import time import uuid +import platform # Import the platform module from termcolor import colored from database.database import get_command_run_from_hash_id, save_command_run @@ -21,8 +22,7 @@ def enqueue_output(out, q): break q.put(line) out.close() - -# had issues using setsid on Wondows, so this detects windows/Unix and uses the correct version of run_command + def run_command(command, root_path, q_stdout, q_stderr, pid_container): if platform.system() == 'Windows': # Check the operating system return run_command_windows(command, root_path, q_stdout, q_stderr, pid_container) @@ -68,7 +68,19 @@ def run_command_unix(command, root_path, q_stdout, q_stderr, pid_container): t_stderr.start() return process - +def terminate_process(pid): + if platform.system() == "Windows": + try: + subprocess.run(["taskkill", "/F", "/T", "/PID", str(pid)]) + except subprocess.CalledProcessError: + # Handle any potential errors here + pass + else: # Unix-like systems + try: + os.killpg(pid, signal.SIGTERM) + except OSError: + # Handle any potential errors here + pass def execute_command(project, command, timeout=None, force=False): if timeout is not None: @@ -152,7 +164,11 @@ def execute_command(project, command, timeout=None, force=False): else: print("\nTimeout detected. Stopping command execution...") - os.killpg(pid_container[0], signal.SIGKILL) # Kill the process group + if platform.system() == 'Windows': + pid = pid_container[0] # Replace with the actual PID + terminate_process(pid) + else: + os.killpg(pid_container[0], signal.SIGKILL) # stderr_output = '' # while not q_stderr.empty(): From f294babec3ae64c4f2e93adfdb5e1a0652f5fef3 Mon Sep 17 00:00:00 2001 From: LeonOstrez <41999013+LeonOstrez@users.noreply.github.com> Date: Mon, 28 Aug 2023 16:49:20 +0200 Subject: [PATCH 3/3] cleanup PR --- pilot/helpers/cli.py | 59 +++++++++++++++----------------------------- 1 file changed, 20 insertions(+), 39 deletions(-) diff --git a/pilot/helpers/cli.py b/pilot/helpers/cli.py index 08a85c9..513441e 100644 --- a/pilot/helpers/cli.py +++ b/pilot/helpers/cli.py @@ -5,7 +5,7 @@ import threading import queue import time import uuid -import platform # Import the platform module +import platform from termcolor import colored from database.database import get_command_run_from_hash_id, save_command_run @@ -25,40 +25,25 @@ def enqueue_output(out, q): def run_command(command, root_path, q_stdout, q_stderr, pid_container): if platform.system() == 'Windows': # Check the operating system - return run_command_windows(command, root_path, q_stdout, q_stderr, pid_container) + process = subprocess.Popen( + command, + shell=True, + stdout=subprocess.PIPE, + stderr=subprocess.PIPE, + text=True, + cwd=root_path + ) else: - return run_command_unix(command, root_path, q_stdout, q_stderr, pid_container) + process = subprocess.Popen( + command, + shell=True, + stdout=subprocess.PIPE, + stderr=subprocess.PIPE, + text=True, + preexec_fn=os.setsid, # Use os.setsid only for Unix-like systems + cwd=root_path + ) -def run_command_windows(command, root_path, q_stdout, q_stderr, pid_container): - # Windows-specific implementation using subprocess - process = subprocess.Popen( - command, - shell=True, - stdout=subprocess.PIPE, - stderr=subprocess.PIPE, - text=True, - cwd=root_path - ) - pid_container[0] = process.pid - t_stdout = threading.Thread(target=enqueue_output, args=(process.stdout, q_stdout)) - t_stderr = threading.Thread(target=enqueue_output, args=(process.stderr, q_stderr)) - t_stdout.daemon = True - t_stderr.daemon = True - t_stdout.start() - t_stderr.start() - return process - -def run_command_unix(command, root_path, q_stdout, q_stderr, pid_container): - # Unix-like systems implementation using os.setsid - process = subprocess.Popen( - command, - shell=True, - stdout=subprocess.PIPE, - stderr=subprocess.PIPE, - text=True, - preexec_fn=os.setsid, # Use os.setsid only for Unix-like systems - cwd=root_path - ) pid_container[0] = process.pid t_stdout = threading.Thread(target=enqueue_output, args=(process.stdout, q_stdout)) t_stderr = threading.Thread(target=enqueue_output, args=(process.stderr, q_stderr)) @@ -77,7 +62,7 @@ def terminate_process(pid): pass else: # Unix-like systems try: - os.killpg(pid, signal.SIGTERM) + os.killpg(pid, signal.SIGKILL) except OSError: # Handle any potential errors here pass @@ -164,11 +149,7 @@ def execute_command(project, command, timeout=None, force=False): else: print("\nTimeout detected. Stopping command execution...") - if platform.system() == 'Windows': - pid = pid_container[0] # Replace with the actual PID - terminate_process(pid) - else: - os.killpg(pid_container[0], signal.SIGKILL) + terminate_process(pid_container[0]) # stderr_output = '' # while not q_stderr.empty():