From f63bae4c94d5af6cbf906a059ca27f07d33227e4 Mon Sep 17 00:00:00 2001 From: vdovhanych Date: Tue, 20 Aug 2024 11:19:14 +0200 Subject: [PATCH] chore: fix bridge stop command not working --- src/bridge.py | 25 +++++++++++++++++++------ 1 file changed, 19 insertions(+), 6 deletions(-) diff --git a/src/bridge.py b/src/bridge.py index d5a6d39..8abd755 100644 --- a/src/bridge.py +++ b/src/bridge.py @@ -4,6 +4,7 @@ import os import socket import time +from shlex import split from typing import TYPE_CHECKING from psutil import Popen @@ -97,14 +98,14 @@ def start(version: str, proxy: bool = False, output_to_logfile: bool = True) -> if helpers.physical_trezor() else f"{bridge_location} -ed 21324:21325 -u=false" ) - # Conditionally redirecting the output to a logfile instead of terminal/stdout + command_list = split(command) if output_to_logfile: log_file = open(helpers.EMU_BRIDGE_LOG, "a") log(f"All the bridge debug output redirected to {helpers.EMU_BRIDGE_LOG}") - BRIDGE = Popen(command, shell=True, stdout=log_file, stderr=log_file) + BRIDGE = Popen(command_list, stdout=log_file, stderr=log_file) else: - BRIDGE = Popen(command, shell=True) + BRIDGE = Popen(command_list) log(f"Bridge spawned: {BRIDGE}. CMD: {BRIDGE.cmdline()}") @@ -129,10 +130,22 @@ def stop(proxy: bool = True) -> None: global VERSION_RUNNING if BRIDGE is None: - log("WARNING: Attempting to stop a brige, but it is not running", "red") + log("WARNING: Attempting to stop a bridge, but it is not running", "red") else: - BRIDGE.kill() - log(f"Bridge killed: {BRIDGE}") + try: + BRIDGE.terminate() + BRIDGE.wait(timeout=5) + except Exception as e: + log(f"Termination failed: {e}, killing process.", "yellow") + BRIDGE.kill() + BRIDGE.wait() + + # Ensuring all child processes are cleaned up + for child in BRIDGE.children(recursive=True): + log(f"Killing child process {child.pid}") + child.kill() + child.wait() + BRIDGE = None VERSION_RUNNING = None