From 781edeac04613b7962724be7da4313f643ea1622 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] Remove kthread + subprocess killer dependencies --- clashroyalebuildabot/emulator/emulator.py | 72 +++++++++++++++++++---- pyproject.toml | 4 +- 2 files changed, 63 insertions(+), 13 deletions(-) diff --git a/clashroyalebuildabot/emulator/emulator.py b/clashroyalebuildabot/emulator/emulator.py index 91c7c36..bf0bd22 100644 --- a/clashroyalebuildabot/emulator/emulator.py +++ b/clashroyalebuildabot/emulator/emulator.py @@ -6,17 +6,14 @@ import platform import socket import subprocess +import threading import time import zipfile import av from get_free_port import get_dynamic_ports -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,66 @@ from clashroyalebuildabot.constants import SRC_DIR +class KThread(threading.Thread): + """A subclass of threading.Thread, with a kill() method.""" + + def __init__(self, *args, **kwargs): + super().__init__(*args, **kwargs) + self._kill = threading.Event() + + def start(self): + self._kill.clear() + super().start() + + def run(self): + while not self._kill.is_set(): + super().run() + + def kill(self): + self._kill.set() + + +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 +163,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] @@ -196,7 +248,7 @@ def _connect_to_server(self): self.video_socket.close() def _start_capturing(self): - self.screenshot_thread = kthread.KThread( + self.screenshot_thread = KThread( target=self._update_screenshot, name="update_screenshot_thread" ) self.screenshot_thread.start() diff --git a/pyproject.toml b/pyproject.toml index 394ba5e..541a000 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -24,9 +24,7 @@ dependencies = [ "pybind11>=2.12", "requests>=2.25.1", "av", - "get_free_port", - "kthread", - "subprocesskiller" + "get_free_port" ] [project.optional-dependencies] From 5dd8454a2395afad8cc079fdc263251adbbe42a1 Mon Sep 17 00:00:00 2001 From: peter Date: Sun, 30 Jun 2024 17:47:09 +0100 Subject: [PATCH 2/2] Add pull_request --- .github/workflows/build.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index 9a0a981..c8a5ddb 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -3,6 +3,7 @@ name: Build on: push: workflow_dispatch: + pull_request: jobs: build: