Skip to content

The agent that grows with you

fix(bot-screen): the RFB bridge finishes its pumps and socket before returning

teknium1462c702fc893
diff --git a/hermes_cli/web_routers/display.py b/hermes_cli/web_routers/display.py
index 408490ce926..4733b69ca1d 100644
--- a/hermes_cli/web_routers/display.py
+++ b/hermes_cli/web_routers/display.py
@@ -172,6 +172,9 @@ async def _bridge(ws: WebSocket, info: dict) -> None:
done, pending = await asyncio.wait(tasks, return_when=asyncio.FIRST_COMPLETED)
for t in pending:
t.cancel()
+ # Cancelled pumps must finish before we tear down the socket they hold, or they outlive the
+ # bridge on the loop (a viewer reconnecting in a loop piled them up).
+ await asyncio.gather(*pending, return_exceptions=True)
for t in done:
exc = t.exception()
if exc and not isinstance(exc, (WebSocketDisconnect, ConnectionError)):
@@ -179,6 +182,10 @@ async def _bridge(ws: WebSocket, info: dict) -> None:
finally:
unsubscribe()
writer.close()
+ try:
+ await writer.wait_closed()
+ except OSError: # Xvnc already gone (ECONNRESET / EPIPE on the FIN)
+ pass
# Closing the viewer window hands control back. A DROPPED link (laptop lid, Wi-Fi, 1006)
# keeps the human's exclusion: they may be mid-login on that screen and the agent must not
# resume into it. The Desktop reconnects into the same lease, or the human hands back.
diff --git a/tests/hermes_cli/test_display_ws_drop_keeps_lease.py b/tests/hermes_cli/test_display_ws_drop_keeps_lease.py
index 2bc98c15dba..7c44ade257c 100644
--- a/tests/hermes_cli/test_display_ws_drop_keeps_lease.py
+++ b/tests/hermes_cli/test_display_ws_drop_keeps_lease.py
@@ -128,3 +128,33 @@ def test_a_takeover_made_by_another_process_stops_input_within_the_refresh_inter
elapsed = asyncio.run(_run(home))
lease._reset_for_tests()
assert elapsed < 0.5, elapsed
+
+
+def test_no_bridge_task_or_socket_outlives_the_bridge():
+ """The pumps were cancelled but never awaited and the Xvnc writer closed without wait_closed, so
+ a returned _bridge still had tasks (and a half-closed socket) in flight on the loop; a viewer
+ reconnecting in a tight loop accumulated them. Everything the bridge started is finished, and
+ Xvnc has seen EOF, when it returns."""
+ async def _run(home: str) -> set:
+ sock_dir = os.path.join(home, "bot-desktop")
+ os.makedirs(sock_dir, exist_ok=True)
+ xvnc_saw_eof = asyncio.Event()
+
+ async def _xvnc(reader, writer):
+ await reader.read() # until the bridge closes its end
+ xvnc_saw_eof.set()
+
+ server = await asyncio.start_unix_server(_xvnc, path=os.path.join(sock_dir, "rfb.sock"))
+ try:
+ await display._bridge(_Ws(1000), {"hermes_home": home, "viewer_id": "desk-1"})
+ leftover = {t for t in asyncio.all_tasks()
+ if not t.done() and t.get_coro().__qualname__.startswith("_bridge.")}
+ await asyncio.wait_for(xvnc_saw_eof.wait(), 2)
+ return leftover
+ finally:
+ server.close()
+
+ lease._reset_for_tests()
+ with tempfile.TemporaryDirectory() as home:
+ leftover = asyncio.run(_run(home))
+ assert leftover == set(), [t.get_coro() for t in leftover]