From 8657d8d21c13ea795a8c7576ab47a604111646b8 Mon Sep 17 00:00:00 2001 From: BitterPanda Date: Wed, 30 Apr 2025 12:35:39 +0200 Subject: [PATCH 1/3] Fix windows bug with socket being UDS --- aikido_zen/background_process/__init__.py | 40 +++++++++++++++-------- 1 file changed, 26 insertions(+), 14 deletions(-) diff --git a/aikido_zen/background_process/__init__.py b/aikido_zen/background_process/__init__.py index c6fa7ee66..8fde1a3b6 100644 --- a/aikido_zen/background_process/__init__.py +++ b/aikido_zen/background_process/__init__.py @@ -5,6 +5,8 @@ import os from multiprocessing import Process +import platform + from aikido_zen.helpers.token import get_token_from_env from aikido_zen.helpers.get_temp_dir import get_temp_dir from aikido_zen.helpers.hash_aikido_token import hash_aikido_token @@ -31,20 +33,20 @@ def start_background_process(): ) return - uds_filename = get_uds_filename() - comms = AikidoIPCCommunications(uds_filename, secret_key_bytes) - - if os.path.exists(uds_filename): - # Send out a PING to check if the daemon running is still active : - res = comms.send_data_to_bg_process(action="PING", obj=tuple(), receive=True) - if res["success"] and res["data"] == "Received": - # Ping is active, return. - logger.debug( - "A background agent is already running, not starting a new background agent." - ) - return - # Ping must have failed, remove the existing file to ensure there is no corruption. - os.remove(uds_filename) + address = get_uds_filename() + if platform.system() == "Windows": + # Python does not support Windows UDS just yet, so we have to rely on INET + address = ("127.0.0.1", 49156) + + comms = AikidoIPCCommunications(address, secret_key_bytes) + + if isinstance(address, str) and os.path.exists(address): + if background_process_already_active(comms): + return # Don't start if the background process is already active + os.remove(address) + else: + if background_process_already_active(comms): + return # Don't start if the background process is already active # Daemon is set to True so that the process kills itself when the main process dies background_process = Process( @@ -59,3 +61,13 @@ def get_uds_filename(): prefix = "aikido_python" name = hash_aikido_token() return f"{temp_dir}/{prefix}_{name}.sock" + +def background_process_already_active(comms): + res = comms.send_data_to_bg_process(action="PING", obj=tuple(), receive=True) + if res["success"] and res["data"] == "Received": + # Ping is active, return. + logger.debug( + "A background agent is already running, not starting a new background agent." + ) + return True + return False From 2ef42baf8927b48c87b1ac3ca5bc1e1030abf7d9 Mon Sep 17 00:00:00 2001 From: BitterPanda Date: Wed, 30 Apr 2025 12:42:48 +0200 Subject: [PATCH 2/3] Don't need to do a ping check for windows, since only 1 pproces can listen --- aikido_zen/background_process/__init__.py | 3 --- 1 file changed, 3 deletions(-) diff --git a/aikido_zen/background_process/__init__.py b/aikido_zen/background_process/__init__.py index 8fde1a3b6..2e6fda6c6 100644 --- a/aikido_zen/background_process/__init__.py +++ b/aikido_zen/background_process/__init__.py @@ -44,9 +44,6 @@ def start_background_process(): if background_process_already_active(comms): return # Don't start if the background process is already active os.remove(address) - else: - if background_process_already_active(comms): - return # Don't start if the background process is already active # Daemon is set to True so that the process kills itself when the main process dies background_process = Process( From 53d704f7b1c106d9c576ed6bac5bec1a0589fc52 Mon Sep 17 00:00:00 2001 From: BitterPanda Date: Wed, 30 Apr 2025 12:47:22 +0200 Subject: [PATCH 3/3] Linting --- aikido_zen/background_process/__init__.py | 1 + 1 file changed, 1 insertion(+) diff --git a/aikido_zen/background_process/__init__.py b/aikido_zen/background_process/__init__.py index 2e6fda6c6..71f199e39 100644 --- a/aikido_zen/background_process/__init__.py +++ b/aikido_zen/background_process/__init__.py @@ -59,6 +59,7 @@ def get_uds_filename(): name = hash_aikido_token() return f"{temp_dir}/{prefix}_{name}.sock" + def background_process_already_active(comms): res = comms.send_data_to_bg_process(action="PING", obj=tuple(), receive=True) if res["success"] and res["data"] == "Received":