Skip to content

The agent that grows with you

fix(tui): release a forwarded prompt's owner socket once its Desktop session closes

Teknium5abc25796762
diff --git a/tests/tui_gateway/test_legacy_submit_forwards_to_owner.py b/tests/tui_gateway/test_legacy_submit_forwards_to_owner.py
index dc4e0e99152..73aea35833e 100644
--- a/tests/tui_gateway/test_legacy_submit_forwards_to_owner.py
+++ b/tests/tui_gateway/test_legacy_submit_forwards_to_owner.py
@@ -158,6 +158,27 @@ def test_outcome_unknown_work_is_still_refused_and_never_forwarded(monkeypatch,
db.close()
+def test_a_closed_desktop_session_releases_its_relay_without_an_owner_frame(monkeypatch):
+ """The relay re-checked ``_finalized`` only when an owner frame arrived, so a closed Desktop session
+ held the relay thread and its owner socket until the owner's next frame (up to RELAY_TIMEOUT_S)."""
+ from tui_gateway import owner_forward
+ monkeypatch.setattr(owner_forward, "RELAY_FINALIZE_POLL_S", 0.05, raising=False)
+ session, emitted = {}, []
+ relay = owner_forward._Relay("ui", session, "key", None, None, "hi", lambda *a: emitted.append(a))
+
+ async def scenario():
+ client = SimpleNamespace(events=asyncio.Queue()) # the owner sends nothing more
+ task = asyncio.create_task(relay._relay(client, "a1"))
+ await asyncio.sleep(0)
+ session["_finalized"] = True
+ done, _ = await asyncio.wait({task}, timeout=2)
+ task.cancel()
+ return task in done
+
+ assert asyncio.run(scenario()), "the relay kept its owner socket after the Desktop session closed"
+ assert emitted == [] # a closed session gets no synthetic completion
+
+
def test_an_unconfirmed_forward_is_never_reported_as_not_sent(monkeypatch, tmp_path):
"""The owner's receipt timed out but the relay keeps going, so the prompt may already sit in the
owner's FIFO: the refusal says to check the chat, with a reason the client never offers Retry for
diff --git a/tui_gateway/owner_forward.py b/tui_gateway/owner_forward.py
index d4a5369cef6..4bb4dbb6031 100644
--- a/tui_gateway/owner_forward.py
+++ b/tui_gateway/owner_forward.py
@@ -35,6 +35,9 @@ FORWARDABLE_LEDGER_STATES = frozenset({"started", "queued", "registered", "runni
ADMIT_TIMEOUT_S = 30.0
#: Ceiling on one relay; the owner keeps the work either way (it is durable).
RELAY_TIMEOUT_S = 6 * 3600.0
+#: How often an idle relay re-checks that its Desktop session still exists: a closed session
+#: releases the thread and the owner socket without waiting for the owner's next frame.
+RELAY_FINALIZE_POLL_S = 1.0
#: Owner turn frames relayed for the forwarded admission. Not its deltas: a Desktop turn that streamed
#: nothing re-reads the stored transcript at ``message.complete``, which brings the user's own row
#: (written by the owner, not by this backend) and the reply in their committed order.
@@ -153,7 +156,11 @@ class _Relay:
try:
async with asyncio.timeout(RELAY_TIMEOUT_S):
while not settled and not self.session.get("_finalized"):
- frame = await client.events.get()
+ try:
+ async with asyncio.timeout(RELAY_FINALIZE_POLL_S):
+ frame = await client.events.get()
+ except TimeoutError:
+ continue
if isinstance(frame, BaseException):
return
params = frame.get("params") if isinstance(frame, dict) else None