Skip to content

The agent that grows with you

fix(serve): review follow-ups — never-raise sentinel fallback, DEVNULL stderr in split-stream test

kshitijk4poor8d95ab1b3718
diff --git a/hermes_cli/web_server.py b/hermes_cli/web_server.py
index bbf023b8b4e..2b5f0fd6b53 100644
--- a/hermes_cli/web_server.py
+++ b/hermes_cli/web_server.py
@@ -19524,13 +19524,22 @@ def _write_machine_sentinel_line(line: str) -> None:
machine-readable sentinel printed after that import via ``print()`` lands
on stderr — invisible to consumers that parse the child's stdout pipe
(the Desktop spawn, scripts). fd 1 is untouched by the Python-level
- redirect, so write there; fall back to ``print`` for exotic environments
- where fd 1 isn't writable (e.g. closed).
+ redirect, so write there.
+
+ Best-effort by design: if fd 1 is unwritable (closed; invalid under
+ pythonw.exe), fall back to ``print()`` for human visibility only — the
+ redirected stream can't reach stdout-parsing consumers, and pythonw
+ Desktop spawns rely on ``_write_dashboard_ready_file()`` (the
+ HERMES_DESKTOP_READY_FILE channel) for port discovery instead. Never
+ raises: a sentinel-delivery failure must not kill a healthy serve.
"""
try:
os.write(1, (line + "\n").encode())
except OSError:
- print(line, flush=True)
+ try:
+ print(line, flush=True)
+ except Exception:
+ pass
def _report_port_in_use(host: str, port: int) -> None:
diff --git a/tests/hermes_cli/test_serve_port_in_use.py b/tests/hermes_cli/test_serve_port_in_use.py
index 2e12430d1b4..4179ce1e2f8 100644
--- a/tests/hermes_cli/test_serve_port_in_use.py
+++ b/tests/hermes_cli/test_serve_port_in_use.py
@@ -114,7 +114,12 @@ def _spawn_serve(port: int, tmp_path: Path, merge_stderr: bool = True) -> subpro
cwd=str(REPO_ROOT),
env=env,
stdout=subprocess.PIPE,
- stderr=subprocess.STDOUT if merge_stderr else subprocess.PIPE,
+ # merge_stderr=False: the caller only asserts on stdout. Discard
+ # stderr instead of piping it — with the serve-path stdout redirect
+ # active ALL server logging lands on stderr, and an unread stderr
+ # pipe can fill (~64KB) and block the child before it ever emits
+ # the READY sentinel, turning the test into a timeout flake.
+ stderr=subprocess.STDOUT if merge_stderr else subprocess.DEVNULL,
text=True,
)
@@ -237,10 +242,3 @@ def test_ready_sentinel_arrives_on_stdout_not_stderr(tmp_path):
proc.wait(timeout=30)
except subprocess.TimeoutExpired:
proc.kill()
- # Drain stderr after exit so the pipe doesn't fill and block the child
- # on platforms with small pipe buffers.
- if proc.stderr is not None:
- try:
- proc.stderr.read()
- except Exception:
- pass