Skip to content

Commit

Permalink
Update emulator.py
Browse files Browse the repository at this point in the history
  • Loading branch information
Leviaria authored Jun 30, 2024
1 parent 6122949 commit 38f4ce6
Showing 1 changed file with 41 additions and 8 deletions.
49 changes: 41 additions & 8 deletions clashroyalebuildabot/emulator/emulator.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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]
Expand Down

0 comments on commit 38f4ce6

Please sign in to comment.