Skip to content

Fix windows bug with socket being UDS #361

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Apr 30, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 24 additions & 14 deletions aikido_zen/background_process/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -31,20 +33,17 @@
)
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":

Check warning on line 37 in aikido_zen/background_process/__init__.py

View check run for this annotation

Codecov / codecov/patch

aikido_zen/background_process/__init__.py#L36-L37

Added lines #L36 - L37 were not covered by tests
# Python does not support Windows UDS just yet, so we have to rely on INET
address = ("127.0.0.1", 49156)

Check warning on line 39 in aikido_zen/background_process/__init__.py

View check run for this annotation

Codecov / codecov/patch

aikido_zen/background_process/__init__.py#L39

Added line #L39 was not covered by tests

comms = AikidoIPCCommunications(address, secret_key_bytes)

Check warning on line 41 in aikido_zen/background_process/__init__.py

View check run for this annotation

Codecov / codecov/patch

aikido_zen/background_process/__init__.py#L41

Added line #L41 was not covered by tests

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)

Check warning on line 46 in aikido_zen/background_process/__init__.py

View check run for this annotation

Codecov / codecov/patch

aikido_zen/background_process/__init__.py#L43-L46

Added lines #L43 - L46 were not covered by tests

# Daemon is set to True so that the process kills itself when the main process dies
background_process = Process(
Expand All @@ -59,3 +58,14 @@
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":

Check warning on line 65 in aikido_zen/background_process/__init__.py

View check run for this annotation

Codecov / codecov/patch

aikido_zen/background_process/__init__.py#L64-L65

Added lines #L64 - L65 were not covered by tests
# Ping is active, return.
logger.debug(

Check warning on line 67 in aikido_zen/background_process/__init__.py

View check run for this annotation

Codecov / codecov/patch

aikido_zen/background_process/__init__.py#L67

Added line #L67 was not covered by tests
"A background agent is already running, not starting a new background agent."
)
return True
return False

Check warning on line 71 in aikido_zen/background_process/__init__.py

View check run for this annotation

Codecov / codecov/patch

aikido_zen/background_process/__init__.py#L70-L71

Added lines #L70 - L71 were not covered by tests
Loading