From 38f4ce668b6fd3d6b313553230a43fff6b10ba37 Mon Sep 17 00:00:00 2001 From: Leviaria <113382526+Leviaria@users.noreply.github.com> Date: Sun, 30 Jun 2024 16:45:23 +0200 Subject: [PATCH 1/2] Update emulator.py --- clashroyalebuildabot/emulator/emulator.py | 49 +++++++++++++++++++---- 1 file changed, 41 insertions(+), 8 deletions(-) diff --git a/clashroyalebuildabot/emulator/emulator.py b/clashroyalebuildabot/emulator/emulator.py index 91c7c36..d2d508b 100644 --- a/clashroyalebuildabot/emulator/emulator.py +++ b/clashroyalebuildabot/emulator/emulator.py @@ -14,9 +14,6 @@ import kthread from loguru import logger import requests -from subprocesskiller import kill_pid -from subprocesskiller import kill_process_children_parents -from subprocesskiller import kill_subprocs import yaml from clashroyalebuildabot.constants import ADB_DIR @@ -27,9 +24,47 @@ from clashroyalebuildabot.constants import SRC_DIR +def kill_pid(pid): + try: + os.kill(pid, 9) + except OSError as e: + logger.error(f"Error killing pid {pid}: {e}") + + +def kill_process_children_parents(pid): + try: + for child_pid in get_child_processes(pid): + kill_pid(child_pid) + kill_pid(pid) + except Exception as e: + logger.error(f"Error killing process tree for pid {pid}: {e}") + + +def get_child_processes(pid): + try: + child_pids = [] + ps_command = subprocess.Popen( + ["ps", "-o", "pid", "--ppid", str(pid), "--noheaders"], + stdout=subprocess.PIPE, + ) + ps_output = ps_command.stdout.read() + ps_command.stdout.close() + for line in ps_output.splitlines(): + child_pids.append(int(line)) + return child_pids + except Exception as e: + logger.error(f"Error getting child processes for pid {pid}: {e}") + return [] + + @atexit.register def kill_them_all(): - kill_subprocs() + try: + parent_pids = get_child_processes(os.getpid()) + for parent_pid in parent_pids: + kill_process_children_parents(parent_pid) + except Exception as e: + logger.error(f"Error in kill_them_all: {e}") @contextmanager @@ -109,13 +144,11 @@ def quit(self): self.scrcpy_proc.kill() with ignored(Exception): - kill_process_children_parents( - pid=self.scrcpy_proc.pid, max_parent_exe="adb.exe", dontkill=() - ) + kill_process_children_parents(pid=self.scrcpy_proc.pid) time.sleep(2) with ignored(Exception): - kill_pid(pid=self.scrcpy_proc.pid) + kill_pid(self.scrcpy_proc.pid) def _run_command(self, command): command = [ADB_PATH, "-s", self.serial, *command] From 39a4c6fad0a6542ec27a53d615d82654359b0c52 Mon Sep 17 00:00:00 2001 From: Leviaria <113382526+Leviaria@users.noreply.github.com> Date: Sun, 30 Jun 2024 16:45:56 +0200 Subject: [PATCH 2/2] Update pyproject.toml --- pyproject.toml | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/pyproject.toml b/pyproject.toml index 394ba5e..96cf0ce 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -25,8 +25,7 @@ dependencies = [ "requests>=2.25.1", "av", "get_free_port", - "kthread", - "subprocesskiller" + "kthread" ] [project.optional-dependencies]