Skip to content

The agent that grows with you

fix(serve): reserve ProcessLookupError for known missing process signals

salch-cred3e72aef74484
diff --git a/hermes_cli/web_server_lifecycle.py b/hermes_cli/web_server_lifecycle.py
index ccb26d425c7..d84e752c67d 100644
--- a/hermes_cli/web_server_lifecycle.py
+++ b/hermes_cli/web_server_lifecycle.py
@@ -75,7 +75,10 @@ def _process_start_marker(pid: int) -> str:
marker = result.stdout.strip()
if result.returncode == 0 and marker:
return f"ps:{marker}"
- if result.returncode != 0 and (not marker or "no such process" in result.stderr.lower() or "not found" in result.stderr.lower()):
+ stderr_lower = result.stderr.lower()
+ if (result.returncode == 1 and not marker) or any(
+ msg in stderr_lower for msg in ("no such process", "not found")
+ ):
raise ProcessLookupError(pid)
raise OSError(f"ps could not inspect PID {pid}: {result.stderr.strip()}")
diff --git a/tests/hermes_cli/test_serve_parent_watchdog.py b/tests/hermes_cli/test_serve_parent_watchdog.py
index 1f23d02b7d9..e12ebe4ef98 100644
--- a/tests/hermes_cli/test_serve_parent_watchdog.py
+++ b/tests/hermes_cli/test_serve_parent_watchdog.py
@@ -223,13 +223,14 @@ def test_parent_watchdog_detects_dead_parent_on_process_lookup_error():
def test_ps_process_start_marker_raises_process_lookup_error_on_missing_process(monkeypatch):
- """#80204: _process_start_marker raises ProcessLookupError when ps exits non-zero with no marker."""
+ """#80204: _process_start_marker raises ProcessLookupError when ps exits with known missing-process markers."""
import subprocess
import pytest
from hermes_cli import web_server_lifecycle
+ # returncode != 1 but explicit "No such process" in stderr
def fake_run(*args, **kwargs):
- return subprocess.CompletedProcess(args=args, returncode=1, stdout="", stderr="ps: 4242: No such process")
+ return subprocess.CompletedProcess(args=args, returncode=2, stdout="", stderr="ps: 4242: No such process")
monkeypatch.setattr(subprocess, "run", fake_run)
monkeypatch.setattr(web_server_lifecycle.sys, "platform", "darwin")
@@ -238,3 +239,56 @@ def test_ps_process_start_marker_raises_process_lookup_error_on_missing_process(
with pytest.raises(ProcessLookupError):
web_server_lifecycle._process_start_marker(4242)
+
+def test_ps_process_start_marker_raises_oserror_on_unrelated_nonzero_failure(monkeypatch):
+ """#80204: unrelated ps failures (e.g. returncode 2 without missing-process message) must raise OSError."""
+ import subprocess
+ import pytest
+ from hermes_cli import web_server_lifecycle
+
+ def fake_run(*args, **kwargs):
+ return subprocess.CompletedProcess(args=args, returncode=2, stdout="", stderr="ps: temporary process table failure")
+
+ monkeypatch.setattr(subprocess, "run", fake_run)
+ monkeypatch.setattr(web_server_lifecycle.sys, "platform", "darwin")
+ monkeypatch.setattr(web_server_lifecycle.os, "name", "posix")
+
+ with pytest.raises(OSError) as excinfo:
+ web_server_lifecycle._process_start_marker(4242)
+ assert "ps could not inspect PID 4242" in str(excinfo.value)
+ assert not isinstance(excinfo.value, ProcessLookupError)
+
+
+def test_parent_watchdog_does_not_kill_live_parent_on_unrelated_ps_failure(monkeypatch):
+ """#80204: an unrelated nonzero ps error raises OSError, degrading to pid_exists which keeps a live backend alive."""
+ import subprocess
+ from hermes_cli import web_server_lifecycle
+
+ def fake_run(*args, **kwargs):
+ return subprocess.CompletedProcess(args=args, returncode=2, stdout="", stderr="ps: temporary process table failure")
+
+ monkeypatch.setattr(subprocess, "run", fake_run)
+ monkeypatch.setattr(web_server_lifecycle.sys, "platform", "darwin")
+ monkeypatch.setattr(web_server_lifecycle.os, "name", "posix")
+
+ # Parent is alive: must return False (do not kill healthy backend)
+ assert (
+ web_server_lifecycle._is_serve_orphaned(
+ 4242,
+ "ps:Thu Aug 20 22:33:11 2026",
+ pid_exists=lambda _pid: True,
+ )
+ is False
+ )
+
+ # Parent is dead: must return True (reap orphan)
+ assert (
+ web_server_lifecycle._is_serve_orphaned(
+ 4242,
+ "ps:Thu Aug 20 22:33:11 2026",
+ pid_exists=lambda _pid: False,
+ )
+ is True
+ )
+
+