From f2dd32d3e50cf1c90e16f4ef55d41289b1848031 Mon Sep 17 00:00:00 2001 From: Kitson Kelly Date: Thu, 27 Aug 2026 20:49:33 +1000 Subject: [PATCH] fix(serve): announce READY sentinel on fd 1, not the redirected sys.stdout MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Since 6d4e851d8 the serve startup path imports tui_gateway.server (for the flush-on-SIGTERM handlers) before the READY sentinel is printed. That module redirects sys.stdout to sys.stderr at import time, so the HERMES_(BACKEND|DASHBOARD)_READY port= sentinel landed on stderr while the Electron desktop spawn watches child.stdout only — the desktop timed out after 90s and killed a perfectly healthy backend (issue #96282). Write the sentinel to the real stdout file descriptor (fd 1 is untouched by the Python-level redirect), with a print() fallback. Adds a regression test that captures stdout/stderr separately — the existing E2E suite merges them, which is exactly how this slipped past CI. --- hermes_cli/web_server.py | 13 +++++- tests/hermes_cli/test_serve_port_in_use.py | 47 +++++++++++++++++++++- 2 files changed, 57 insertions(+), 3 deletions(-) diff --git a/hermes_cli/web_server.py b/hermes_cli/web_server.py index 2d27c5da98..62547f08fc 100644 --- a/hermes_cli/web_server.py +++ b/hermes_cli/web_server.py @@ -19888,7 +19888,18 @@ def start_server( # plain backend, not a dashboard, so it announces a neutral token; # `dashboard` keeps the legacy one. The desktop matches either. ready_token = "HERMES_BACKEND_READY" if headless else "HERMES_DASHBOARD_READY" - print(f"{ready_token} port={actual_port}", flush=True) + # tui_gateway.server (imported above for the flush-on-SIGTERM + # handlers, #94724) redirects sys.stdout→sys.stderr at import time + # to keep stray prints off the JSON-RPC protocol stream. fd 1 is + # still the real stdout — and the Desktop spawn watches + # child.stdout for this sentinel — so write to the fd, not to the + # (redirected) sys.stdout, or the desktop times out after 90s + # against a perfectly healthy backend. + _ready_line = f"{ready_token} port={actual_port}\n" + try: + os.write(1, _ready_line.encode()) + except OSError: + print(_ready_line, end="", flush=True) if headless: # No SPA, and the JSON-RPC/WS endpoints are auth-gated — don't # advertise a paste-and-connect URL, just announce the bind. diff --git a/tests/hermes_cli/test_serve_port_in_use.py b/tests/hermes_cli/test_serve_port_in_use.py index f800174f2b..2e12430d1b 100644 --- a/tests/hermes_cli/test_serve_port_in_use.py +++ b/tests/hermes_cli/test_serve_port_in_use.py @@ -91,7 +91,7 @@ def test_exit_code_is_distinct_tempfail(): # --------------------------------------------------------------------------- -def _spawn_serve(port: int, tmp_path: Path) -> subprocess.Popen: +def _spawn_serve(port: int, tmp_path: Path, merge_stderr: bool = True) -> subprocess.Popen: home = tmp_path / "hermes_home" home.mkdir(exist_ok=True) env = dict(os.environ) @@ -114,7 +114,7 @@ def _spawn_serve(port: int, tmp_path: Path) -> subprocess.Popen: cwd=str(REPO_ROOT), env=env, stdout=subprocess.PIPE, - stderr=subprocess.STDOUT, + stderr=subprocess.STDOUT if merge_stderr else subprocess.PIPE, text=True, ) @@ -201,3 +201,46 @@ def test_ephemeral_port_zero_unaffected(tmp_path): proc.wait(timeout=30) except subprocess.TimeoutExpired: proc.kill() + + +def test_ready_sentinel_arrives_on_stdout_not_stderr(tmp_path): + """#96282 — the Desktop spawn watches child.stdout for the READY sentinel. + + ``tui_gateway.server`` (imported on the serve startup path since 6d4e851d8 + for the flush-on-SIGTERM handlers) redirects ``sys.stdout`` to + ``sys.stderr`` at import time. If the sentinel is printed through the + redirected ``sys.stdout``, it lands on stderr and the desktop times out + after 90s against a perfectly healthy backend. The sentinel must reach + the real stdout (fd 1). + + The other E2E tests here merge stderr into stdout, which is exactly how + the regression slipped past CI — this test keeps the streams separate. + """ + probe = socket.socket(socket.AF_INET, socket.SOCK_STREAM) + probe.bind(("127.0.0.1", 0)) + port = probe.getsockname()[1] + probe.close() + + proc = _spawn_serve(port, tmp_path, merge_stderr=False) + try: + # stdout only: the sentinel MUST arrive here (desktop watches this pipe) + ready, lines = _read_until(proc, "HERMES_BACKEND_READY") + out = "".join(lines) + assert ready, ( + f"READY sentinel not on stdout (desktop boot would time out); " + f"stdout:\n{out}" + ) + assert f"HERMES_BACKEND_READY port={port}" in out + finally: + proc.terminate() + try: + 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