Skip to content

The agent that grows with you

fix(tui): bound waiting context reads and stop cancelling controls on a failed read reply

Tekniuma262df87d935
diff --git a/tests/tui_gateway/test_ws_concurrent_reads.py b/tests/tui_gateway/test_ws_concurrent_reads.py
new file mode 100644
index 00000000000..530403cdb0c
--- /dev/null
+++ b/tests/tui_gateway/test_ws_concurrent_reads.py
@@ -0,0 +1,154 @@
+"""The WS concurrent-read lane (#134889 GW-18 review): waiting context reads stay bounded, a server
+shutdown or client hang-up never waits for reads that have not started, and a reply the read lane
+cannot send never cancels the control handler the serial lane is running.
+
+The real ``handle_ws`` dispatcher; only ``_dispatch_request`` is a gated async stand-in.
+"""
+import asyncio
+import json
+
+import pytest
+
+from tui_gateway import server
+from tui_gateway import ws as ws_mod
+
+
+class Socket:
+ def __init__(self):
+ self.inbound = asyncio.Queue()
+ self.received = 0
+ self.sent = []
+ self.fail_ids = set()
+
+ async def accept(self):
+ pass
+
+ async def send_text(self, line):
+ frame = json.loads(line)
+ if frame.get("id") in self.fail_ids:
+ raise RuntimeError("peer gone")
+ self.sent.append(frame)
+
+ async def receive_text(self):
+ item = await self.inbound.get()
+ if item is None:
+ raise ws_mod._WebSocketDisconnect()
+ self.received += 1
+ return json.dumps(item)
+
+ async def close(self):
+ pass
+
+
+def rig(monkeypatch):
+ gates, log = {}, []
+
+ async def dispatch(authority, req, method, transport):
+ log.append(f"start:{req['id']}")
+ try:
+ gate = gates.get(method)
+ if gate is not None:
+ await gate.wait()
+ except asyncio.CancelledError:
+ log.append(f"cancelled:{req['id']}")
+ raise
+ log.append(f"done:{req['id']}")
+ return {"jsonrpc": "2.0", "id": req["id"], "result": {}}
+
+ monkeypatch.setattr(ws_mod, "_dispatch_request", dispatch)
+ monkeypatch.setattr(ws_mod, "_start_backend_liveness", lambda: None)
+ monkeypatch.setattr(server, "_ensure_skin_watcher", lambda: None)
+ monkeypatch.setattr(server, "_ensure_lease_watcher", lambda: None)
+ monkeypatch.setattr(server, "_WS_ORPHAN_REAP_GRACE_S", 0)
+ monkeypatch.setattr(server, "_close_sessions_for_transport", lambda transport, end_reason: (log.append("teardown"), (0, 0))[1])
+ return Socket(), gates, log
+
+
+def rpc(req_id, method="session.context_breakdown"):
+ return {"jsonrpc": "2.0", "id": req_id, "method": method, "params": {}}
+
+
+async def settle(predicate, timeout=5.0):
+ deadline = asyncio.get_running_loop().time() + timeout
+ while not predicate():
+ if asyncio.get_running_loop().time() > deadline:
+ return False
+ await asyncio.sleep(0.01)
+ return True
+
+
+async def test_waiting_context_reads_keep_reader_backpressure(rig):
+ sock, gates, log = rig
+ gates["session.context_breakdown"] = release = asyncio.Event()
+ task = asyncio.create_task(ws_mod.handle_ws(sock))
+ burst = ws_mod._WS_DISPATCH_QUEUE_MAX * 4
+ for n in range(burst):
+ sock.inbound.put_nowait(rpc(n))
+ try:
+ # A bounded reader parks with frames unread; give it every chance to drain them all.
+ assert not await settle(lambda: sock.inbound.empty(), timeout=2.0)
+ bound = ws_mod._WS_DISPATCH_QUEUE_MAX + ws_mod._WS_CONCURRENT_READ_LIMIT + ws_mod._WS_WAITING_READ_LIMIT + 2
+ assert sock.received <= bound, (sock.received, bound)
+ assert sum(entry.startswith("start:") for entry in log) == ws_mod._WS_CONCURRENT_READ_LIMIT
+ finally:
+ release.set()
+ assert await settle(lambda: sock.inbound.empty())
+ sock.inbound.put_nowait(None)
+ await asyncio.wait_for(task, 30)
+ assert len(sock.sent) == burst + 1 # gateway.ready + one reply per read
+
+
+async def test_server_shutdown_does_not_wait_for_a_running_context_read(rig):
+ sock, gates, log = rig
+ gates["session.context_breakdown"] = release = asyncio.Event()
+ task = asyncio.create_task(ws_mod.handle_ws(sock))
+ sock.inbound.put_nowait(rpc(1))
+ try:
+ assert await settle(lambda: "start:1" in log)
+ task.cancel()
+ done, _ = await asyncio.wait({task}, timeout=5)
+ assert task in done, "socket shutdown still waits for the slow context read"
+ assert "cancelled:1" in log
+ finally:
+ release.set()
+ await asyncio.gather(task, return_exceptions=True)
+
+
+async def test_hang_up_drops_reads_that_never_started(rig):
+ sock, gates, log = rig
+ gates["session.context_breakdown"] = release = asyncio.Event()
+ task = asyncio.create_task(ws_mod.handle_ws(sock))
+ for n in range(6):
+ sock.inbound.put_nowait(rpc(n))
+ assert await settle(lambda: sock.received == 6 and sum(e.startswith("start:") for e in log) == 2)
+ sock.inbound.put_nowait(None)
+ assert not await settle(lambda: "teardown" in log, timeout=2.0), "teardown ran under a started read"
+ release.set()
+ await asyncio.wait_for(task, 10)
+ assert sorted(e for e in log if e.startswith("start:")) == ["start:0", "start:1"]
+ assert log[-1] == "teardown"
+
+
+async def test_a_failed_read_reply_does_not_cancel_the_running_control(rig):
+ """The serial lane's handler (a submit, an interrupt, a mutate) finishes; then the link ends."""
+ sock, gates, log = rig
+ gates["prompt.submit"] = release = asyncio.Event()
+ gates["session.context_breakdown"] = read_gate = asyncio.Event()
+ sock.fail_ids.add("read")
+ task = asyncio.create_task(ws_mod.handle_ws(sock))
+ sock.inbound.put_nowait(rpc("read"))
+ assert await settle(lambda: "start:read" in log)
+ sock.inbound.put_nowait(rpc("submit", "prompt.submit"))
+ assert await settle(lambda: "start:submit" in log)
+ read_gate.set() # the read's reply now fails while the submit is mid-await
+ assert await settle(lambda: "done:read" in log)
+ assert not await settle(lambda: task.done(), timeout=2.0), "the link ended under the running control"
+ release.set()
+ await asyncio.wait_for(task, 10)
+ assert "cancelled:submit" not in log and "done:submit" in log
+ assert log[-1] == "teardown"
diff --git a/tui_gateway/ws.py b/tui_gateway/ws.py
index eb73e8fdd8d..266ff612755 100644
--- a/tui_gateway/ws.py
+++ b/tui_gateway/ws.py
@@ -102,6 +102,8 @@ _WS_DISPATCH_QUEUE_MAX = 256
# RPCs still retain arrival order, and no more than two expensive reads consume a connection.
_WS_CONCURRENT_READ_METHODS = frozenset({"session.context_breakdown"})
_WS_CONCURRENT_READ_LIMIT = 2
+# Reads admitted beyond the running ones; one more parks the reader (as the serial lane did).
+_WS_WAITING_READ_LIMIT = 8
# Per-token streaming frames are coalesced: buffered and flushed as a batch on a short timer instead
# of waking the loop once per token (each wakeup competes with the agent turn for the GIL). Keep this
@@ -412,7 +414,13 @@ async def handle_ws(ws: Any, *, auth_identity: dict | None = None, subprotocol:
pending: asyncio.Queue = asyncio.Queue(maxsize=_WS_DISPATCH_QUEUE_MAX)
dispatcher: asyncio.Task | None = None
concurrent_reads: set[asyncio.Task] = set()
+ started_reads: set[asyncio.Task] = set()
concurrent_read_slots = asyncio.Semaphore(_WS_CONCURRENT_READ_LIMIT)
+ # Running + waiting reads: taken by the reader before a read is queued, so a flood parks it.
+ read_admission = asyncio.Semaphore(_WS_CONCURRENT_READ_LIMIT + _WS_WAITING_READ_LIMIT)
+ # Set when the read lane could not send a reply: ends the reader without cancelling the
+ # control handler the serial lane is running (it finishes, then the dispatcher stops).
+ send_failed: asyncio.Future = asyncio.get_running_loop().create_future()
stop = object() # queue sentinel; a JSON ``null`` frame parses to None, so None can't be it
async def _dispatch_one(req: Any) -> None:
@@ -438,22 +446,36 @@ async def handle_ws(ws: Any, *, auth_identity: dict | None = None, subprotocol:
async def _dispatch_concurrent_read(req: Any) -> None:
async with concurrent_read_slots:
+ started_reads.add(asyncio.current_task())
try:
await _dispatch_one(req)
except _SendFailed:
- # A failed response must end the reader even when it came from the concurrent lane.
- if dispatcher is not None:
- dispatcher.cancel()
+ # A failed response ends the reader even from the concurrent lane, never by
+ # cancelling the serial lane's in-flight handler.
+ if not send_failed.done():
+ send_failed.set_result(None)
raise
def _finish_concurrent_read(task: asyncio.Task) -> None:
+ # Here, not in the task: a task cancelled before its first step never runs its body.
+ read_admission.release()
concurrent_reads.discard(task)
+ started_reads.discard(task)
with contextlib.suppress(asyncio.CancelledError, _SendFailed):
task.result()
+ async def _next_request() -> Any:
+ """The next queued frame, or ``stop`` once the read lane failed to send a reply."""
+ step = asyncio.ensure_future(pending.get())
+ await asyncio.wait({step, send_failed}, return_when=asyncio.FIRST_COMPLETED)
+ if step.done():
+ return stop if send_failed.done() else step.result()
+ step.cancel()
+ return stop
+
async def _dispatch_loop() -> None:
"""Dispatch controls in arrival order; bounded expensive read-only projections run alongside them."""
- while (req := await pending.get()) is not stop:
+ while (req := await _next_request()) is not stop:
req_method = req.get("method") if isinstance(req, dict) else None
if req_method in _WS_CONCURRENT_READ_METHODS:
task = asyncio.create_task(_dispatch_concurrent_read(req))
@@ -463,11 +485,12 @@ async def handle_ws(ws: Any, *, auth_identity: dict | None = None, subprotocol:
await _dispatch_one(req)
async def _unless_dispatch_failed(aw: Any) -> Any:
- """Await *aw*, or raise _SendFailed once the dispatcher has ended on a reply it could not send."""
+ """Await *aw*, or raise _SendFailed once a reply could not be sent (the dispatcher ended on one,
+ or the read lane failed one)."""
assert dispatcher is not None
step = asyncio.ensure_future(aw)
try:
- await asyncio.wait({step, dispatcher}, return_when=asyncio.FIRST_COMPLETED)
+ await asyncio.wait({step, dispatcher, send_failed}, return_when=asyncio.FIRST_COMPLETED)
if step.done():
return step.result()
raise _SendFailed
@@ -545,6 +568,10 @@ async def handle_ws(ws: Any, *, auth_identity: dict | None = None, subprotocol:
await _reply({"jsonrpc": "2.0", "result": {"ok": True}, "id": req_id}, "send_failed_after_heartbeat",
"ws heartbeat reply send failed peer=%s id=%s", peer, req_id)
continue
+ if isinstance(req, dict) and req.get("method") in _WS_CONCURRENT_READ_METHODS:
+ # A read past the waiting cap parks the reader, as the serial lane did; released
+ # when its task ends (a read cancelled before it ran releases it too).
+ await _unless_dispatch_failed(read_admission.acquire())
await _unless_dispatch_failed(pending.put(req))
except _SendFailed:
pass
@@ -563,7 +590,13 @@ async def handle_ws(ws: Any, *, auth_identity: dict | None = None, subprotocol:
failure = None if dispatcher.cancelled() else dispatcher.exception()
if failure is not None and not isinstance(failure, _SendFailed):
_log.error("ws dispatcher failed peer=%s", peer, exc_info=failure)
- # Concurrent reads finish before the authority closes (gathering none is a no-op).
+ # Reads that never started are dropped (their client is gone); started ones finish before the
+ # authority closes, unless the connection itself was cancelled (server shutdown).
+ task = asyncio.current_task()
+ shutting_down = task is not None and task.cancelling()
+ for read in concurrent_reads:
+ if shutting_down or read not in started_reads:
+ read.cancel()
await asyncio.gather(*concurrent_reads, return_exceptions=True)
if authority_connection is not None:
# After the dispatcher drained: an in-flight authority call must not see its connection closed.