Skip to content

The agent that grows with you

fix(dashboard): add idle back-off to PTY pump loop (#42627)

Christopher5bceb3e84bd3
diff --git a/hermes_cli/web_server.py b/hermes_cli/web_server.py
index 3f527ed0f6e..01c5e9d77f8 100644
--- a/hermes_cli/web_server.py
+++ b/hermes_cli/web_server.py
@@ -15136,6 +15136,10 @@ else:
_RESIZE_RE = re.compile(rb"\x1b\[RESIZE:(\d+);(\d+)\]")
_PTY_READ_CHUNK_TIMEOUT = 0.2
+# Back-off delay between idle PTY reads so a quiet terminal does not spin
+# the event loop. A positive sleep lets other coroutines run and keeps
+# dashboard idle CPU low (#42627).
+_PTY_IDLE_BACKOFF = 0.05
# Keep-alive PTY sessions: a terminal connecting with ``?attach=<token>`` is
# bound to a process that survives disconnect/refresh and is reattachable.
@@ -15170,7 +15174,7 @@ async def _legacy_pump(ws: "WebSocket", bridge) -> None:
if chunk is None: # EOF
return
if not chunk: # no data this tick; yield control and retry
- await asyncio.sleep(0)
+ await asyncio.sleep(_PTY_IDLE_BACKOFF)
continue
try:
await ws.send_bytes(chunk)
diff --git a/tests/hermes_cli/test_web_server_pty_idle_backoff.py b/tests/hermes_cli/test_web_server_pty_idle_backoff.py
new file mode 100644
index 00000000000..7a755259814
--- /dev/null
+++ b/tests/hermes_cli/test_web_server_pty_idle_backoff.py
@@ -0,0 +1,73 @@
+"""Regression: dashboard PTY pump must back off when the terminal is idle."""
+
+from __future__ import annotations
+
+import asyncio
+from unittest.mock import patch
+
+import pytest
+
+from hermes_cli.web_server import _legacy_pump
+
+
+class _FakeBridge:
+ def __init__(self, reads):
+ self._reads = list(reads)
+ self.closed = False
+
+ def read(self, timeout):
+ if self._reads:
+ return self._reads.pop(0)
+ return None
+
+ def write(self, data):
+ pass
+
+ def resize(self, cols, rows):
+ pass
+
+ def close(self):
+ self.closed = True
+
+
+async def test_legacy_pump_sleeps_on_idle_pty():
+ """An empty PTY read must not spin with ``asyncio.sleep(0)``."""
+ sleeps: list[float] = []
+ first_idle_seen = asyncio.Event()
+ real_sleep = asyncio.sleep
+
+ async def fake_sleep(delay: float) -> None:
+ sleeps.append(delay)
+ first_idle_seen.set()
+ # Yield with the real sleep so we don't recurse through the patch.
+ await real_sleep(0)
+
+ bridge = _FakeBridge([b"", b"", None])
+
+ class _FakeWebSocket:
+ def __init__(self):
+ self.sent: list[bytes] = []
+
+ async def send_bytes(self, data):
+ self.sent.append(data)
+
+ async def receive(self):
+ await first_idle_seen.wait()
+ return {"type": "websocket.disconnect"}
+
+ async def close(self):
+ pass
+
+ ws = _FakeWebSocket()
+
+ with patch("hermes_cli.web_server.asyncio.sleep", side_effect=fake_sleep):
+ # _legacy_pump is typed against starlette WebSocket; our fake is an
+ # intentional behavioral shim. # type: ignore[invalid-argument-type]
+ await _legacy_pump(ws, bridge)
+
+ assert bridge.closed
+ assert sleeps, "pty pump did not call asyncio.sleep on idle ticks"
+ assert all(d > 0 for d in sleeps), (
+ f"idle pump used zero or negative sleeps: {sleeps}"
+ )