fix(serve): announce READY sentinel on fd 1, not the redirected sys.stdout

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=<n> 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.
This commit is contained in:
Kitson Kelly
2026-08-27 20:49:33 +10:00
committed by kshitij
parent dfff42d3dd
commit f2dd32d3e5
2 changed files with 57 additions and 3 deletions
+12 -1
View File
@@ -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.
+45 -2
View File
@@ -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