Skip to content

The agent that grows with you

fix: a process reaped mid-probe reads as gone, not as an error or alive

Tekniumc6d6538c5113
diff --git a/agent/proxy_sources/iron_proxy.py b/agent/proxy_sources/iron_proxy.py
index 941908d0834..32b3a88687e 100644
--- a/agent/proxy_sources/iron_proxy.py
+++ b/agent/proxy_sources/iron_proxy.py
@@ -636,9 +636,15 @@ def _pid_alive(pid: int) -> bool:
env_bytes = Path(f"/proc/{pid}/environ").read_bytes()
if any(f"{_HERMES_IRON_PROXY_NONCE_ENV}={n}".encode() in env_bytes for n in nonce_candidates):
return True
- with suppress(OSError):
- if (cmdline_path := Path(f"/proc/{pid}/cmdline")).exists():
- return os.path.basename(cmdline_path.read_bytes().split(b"\x00")[0].decode("utf-8", errors="ignore")).startswith("iron-proxy")
+ if Path("/proc/self/cmdline").exists(): # Linux: /proc answers for every pid
+ try:
+ cmdline = Path(f"/proc/{pid}/cmdline").read_bytes()
+ except (FileNotFoundError, ProcessLookupError):
+ return False # exited since the liveness probe; `ps` would fail and read as alive below
+ except OSError:
+ cmdline = None
+ if cmdline is not None:
+ return os.path.basename(cmdline.split(b"\x00")[0].decode("utf-8", errors="ignore")).startswith("iron-proxy")
with suppress(OSError, subprocess.TimeoutExpired): # macOS / non-Linux fallback
res = _run(["ps", "-p", str(pid), "-o", "comm="], timeout=2, text=True)
if res.returncode == 0:
diff --git a/tests/agent/test_iron_proxy.py b/tests/agent/test_iron_proxy.py
index e5d8397f5ee..1dea7155e65 100644
--- a/tests/agent/test_iron_proxy.py
+++ b/tests/agent/test_iron_proxy.py
@@ -645,3 +645,28 @@ def test_bitwarden_importerror_raise_without_fallback(
)
[email protected](not sys.platform.startswith("linux"), reason="/proc cmdline identity is Linux-only")
+def test_pid_alive_reads_a_proxy_that_exited_after_the_liveness_probe_as_dead(hermes_home, monkeypatch):
+ """The pid passes the existence probe, then exits and is reaped before /proc/<pid>/cmdline is
+ read. On Linux the missing entry means the process is gone; the `ps` fallback must not run,
+ because a failed `ps` falls through to "believe the OS: alive" and a dead proxy then blocks
+ start_proxy and shows as running."""
+ import subprocess
+
+ import psutil
+
+ from tests.waiting import wait_until
+
+ monkeypatch.setattr(ip, "_proxy_nonce", None)
+ monkeypatch.setattr(ip, "_read_persisted_nonce", lambda: None)
+ monkeypatch.setattr(ip, "_run", MagicMock(return_value=MagicMock(returncode=1, stdout="")))
+ proxy = subprocess.Popen(["bash", "-c", "exec -a iron-proxy sleep 30"])
+ try:
+ wait_until(lambda: Path(f"/proc/{proxy.pid}/cmdline").read_bytes().startswith(b"iron-proxy"), timeout=15)
+ assert ip._pid_alive(proxy.pid) is True # control: a live proxy, known by argv[0]
+ finally:
+ proxy.kill()
+ proxy.wait()
+ monkeypatch.setattr(psutil, "pid_exists", lambda pid: True) # the probe ran before the exit
+ assert ip._pid_alive(proxy.pid) is False
+ ip._run.assert_not_called()
diff --git a/tests/hermes_cli/test_update_custody_tree.py b/tests/hermes_cli/test_update_custody_tree.py
index 81574f68c5a..2dbe7ffe95d 100644
--- a/tests/hermes_cli/test_update_custody_tree.py
+++ b/tests/hermes_cli/test_update_custody_tree.py
@@ -30,10 +30,13 @@ def _alive(pid: int) -> bool:
os.kill(pid, 0)
except ProcessLookupError:
return False
- if Path(f"/proc/{pid}/stat").exists(): # Linux: a zombie is dead
+ if not sys.platform.startswith("linux"):
+ return True
+ try: # Linux: a zombie is dead
stat = Path(f"/proc/{pid}/stat").read_text(encoding="utf-8")
- return stat[stat.rfind(")") + 2:][:1] != "Z"
- return True
+ except (FileNotFoundError, ProcessLookupError):
+ return False # reaped after the signal probe: an orphan's reaper runs at any moment
+ return stat[stat.rfind(")") + 2:][:1] != "Z"
def _gone_within(pid: int, seconds: float) -> bool: