Skip to content
Closed
Show file tree
Hide file tree
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
26 changes: 20 additions & 6 deletions hermes_cli/gateway.py
Original file line number Diff line number Diff line change
Expand Up @@ -221,12 +221,26 @@ def _matches_current_profile(command: str) -> bool:
pass
current_cmd = ""
else:
result = subprocess.run(
["ps", "eww", "-ax", "-o", "pid=,command="],
capture_output=True,
text=True,
timeout=10,
)
result = None
# procps on Ubuntu/WSL accepts `-eww`, while the mixed BSD-style
# `eww -ax` form can fail and make status checks report false
# negatives even when the gateway is running.
for ps_cmd in (
["ps", "-eww", "-ax", "-o", "pid=,command="],
["ps", "-ax", "-o", "pid=,command="],
):
result = subprocess.run(
ps_cmd,
capture_output=True,
text=True,
timeout=10,
)
if result.returncode == 0:
break

if result is None or result.returncode != 0:
return pids

for line in result.stdout.split('\n'):
stripped = line.strip()
if not stripped or 'grep' in stripped:
Expand Down
28 changes: 28 additions & 0 deletions tests/hermes_cli/test_update_gateway_restart.py
Original file line number Diff line number Diff line change
Expand Up @@ -799,6 +799,34 @@ def fake_run(cmd, **kwargs):

assert pids == [100]

def test_falls_back_when_primary_ps_flags_fail(self, monkeypatch):
monkeypatch.setattr(gateway_cli, "is_windows", lambda: False)
monkeypatch.setattr(gateway_cli, "_get_service_pids", lambda: set())
monkeypatch.setattr(gateway_cli, "_profile_arg", lambda hermes_home=None: "")
monkeypatch.setattr("os.getpid", lambda: 999)

calls = []

def fake_run(cmd, **kwargs):
calls.append(cmd)
if cmd[:3] == ["ps", "-eww", "-ax"]:
return subprocess.CompletedProcess(cmd, 1, stdout="", stderr="unsupported")
return subprocess.CompletedProcess(
cmd, 0,
stdout="100 python gateway/run.py\n",
stderr="",
)

monkeypatch.setattr(gateway_cli.subprocess, "run", fake_run)

pids = gateway_cli.find_gateway_pids()

assert pids == [100]
assert calls == [
["ps", "-eww", "-ax", "-o", "pid=,command="],
["ps", "-ax", "-o", "pid=,command="],
]


# ---------------------------------------------------------------------------
# Gateway mode writes exit code before restart (#8300)
Expand Down