The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(webapp): drop frames held for a start that has not ended when the client disconnects
unsupportedpastels83ad72667290
diff --git a/tests/gateway/test_sibling_attach_while_profile_starting.py b/tests/gateway/test_sibling_attach_while_profile_starting.pyindex dedb39cedae..7205f984b62 100644--- a/tests/gateway/test_sibling_attach_while_profile_starting.py+++ b/tests/gateway/test_sibling_attach_while_profile_starting.py@@ -28,6 +28,7 @@ class _Socket:self.replies = {}self.order = []self.arrived = asyncio.Event()+ self.task: asyncio.Task | None = None # the handle_ws task serving this socketasync def accept(self, **_kw):pass@@ -110,7 +111,8 @@ async def rig(tmp_path, monkeypatch):'native_bootstrap': True, 'profile_scope': grant.get('scope', 'profile')}sock = _Socket(authority_for_profile_id(runner, grant['profile_id']))sockets.append(sock)- tasks.append(asyncio.create_task(ws_mod.handle_ws(sock, auth_identity=identity, operator=True)))+ sock.task = asyncio.create_task(ws_mod.handle_ws(sock, auth_identity=identity, operator=True))+ tasks.append(sock.task)return sockdef create(sock, profile, request_id):@@ -281,3 +283,70 @@ async def test_a_frame_read_after_the_start_ends_still_queues_behind_the_held_frerror = sock.replies[request_id].get('error') or {}assert error.get('message') not in ('profile_mismatch', 'internal error'), sock.replies[request_id]assert (error.get('data') or {}).get('reason') != 'runtime_draining', sock.replies[request_id]+++def _record_dispatch(monkeypatch):+ from tui_gateway import ws as ws_mod++ real_dispatch = ws_mod._dispatch_request+ ran = []++ async def recording_dispatch(connection, req, method, transport):+ ran.append(req.get('id'))+ return await real_dispatch(connection, req, method, transport)++ monkeypatch.setattr(ws_mod, '_dispatch_request', recording_dispatch)+ return ran+++async def test_a_disconnect_drops_the_frames_held_for_a_start_that_has_not_ended(rig, monkeypatch):+ """Nobody reads the replies once the client is gone: the socket's handler ends without waiting+ out the start, and the held frames are never run, not even once the start ends."""+ from tui_gateway import ws as ws_mod++ monkeypatch.setattr(ws_mod, '_WS_PROFILE_START_WAIT_S', 30.0)+ ran = _record_dispatch(monkeypatch)+ sock = await rig.connect()+ for n in range(2):+ sock.inbound.put_nowait({'jsonrpc': '2.0', 'id': f'held-{n}', 'method': 'ping', 'params': {'profile': 'beta'}})+ sock.inbound.put_nowait({'jsonrpc': '2.0', 'id': 'probe', 'method': 'ping', 'params': {}})+ await sock.reply('probe', timeout=10) # both beta frames were set aside before it+ sock.inbound.put_nowait(None) # the client disconnects++ done, _ = await asyncio.wait({sock.task}, timeout=5)+ assert sock.task in done, 'the handler waited out the start of a profile for a client that is gone'+ rig.release.set()+ await asyncio.wait_for(rig.serving, 30)+ assert ran == ['probe'], ran+++async def test_frames_held_for_a_start_that_ended_before_the_disconnect_still_run(rig, monkeypatch):+ """Frames whose start ended while the client was connected finish as the serial lane's queue does."""+ from tui_gateway import ws as ws_mod++ real_dispatch = ws_mod._dispatch_request+ ran, first_held_running, first_held_may_finish = [], asyncio.Event(), asyncio.Event()++ async def gated_dispatch(connection, req, method, transport):+ ran.append(req.get('id'))+ if req.get('id') == 'held-0':+ first_held_running.set()+ await asyncio.wait_for(first_held_may_finish.wait(), timeout=10)+ return await real_dispatch(connection, req, method, transport)++ monkeypatch.setattr(ws_mod, '_dispatch_request', gated_dispatch)+ sock = await rig.connect()+ for n in range(2):+ sock.inbound.put_nowait({'jsonrpc': '2.0', 'id': f'held-{n}', 'method': 'ping', 'params': {'profile': 'beta'}})+ sock.inbound.put_nowait({'jsonrpc': '2.0', 'id': 'probe', 'method': 'ping', 'params': {}})+ await sock.reply('probe', timeout=10)+ rig.release.set()+ await asyncio.wait_for(rig.serving, 30)+ await asyncio.wait_for(first_held_running.wait(), 10)+ sock.inbound.put_nowait(None) # the client disconnects while the lane runs held-0+ first_held_may_finish.set()++ await asyncio.wait_for(sock.task, 10)+ assert ran == ['probe', 'held-0', 'held-1'], randiff --git a/tui_gateway/ws.py b/tui_gateway/ws.pyindex adaabf0036f..c4835d7d1e0 100644--- a/tui_gateway/ws.py+++ b/tui_gateway/ws.py@@ -454,6 +454,8 @@ async def handle_ws(ws: Any, *, auth_identity: dict | None = None, subprotocol:# Held frames count against the serial lane's bound: once that many wait, the dispatcher waits for# room, the queue fills and the reader parks, as it does behind a busy handler.held_room = asyncio.Semaphore(_WS_DISPATCH_QUEUE_MAX)+ # Set once the client is gone: frames still waiting for a start are dropped, not run for nobody.+ client_gone = asyncio.Event()async def _dispatch_one(req: Any) -> None:"""Dispatch one request and write its inline reply."""@@ -499,11 +501,20 @@ async def handle_ws(ws: Any, *, auth_identity: dict | None = None, subprotocol:async def _dispatch_after_start(key: str, started: asyncio.Event) -> None:"""Dispatch the frames held for one start, in arrival order, once it ends or the wait runs out(the authority then refuses a profile still starting retryably). A frame for that profile read- meanwhile joins the queue instead of overtaking it on the serial lane."""+ meanwhile joins the queue instead of overtaking it on the serial lane. A client that leaves+ before the start ends drops them: nobody reads those replies."""try:- with contextlib.suppress(TimeoutError):- await asyncio.wait_for(started.wait(), _WS_PROFILE_START_WAIT_S)+ waits = {asyncio.ensure_future(started.wait()), asyncio.ensure_future(client_gone.wait())}+ try:+ await asyncio.wait(waits, timeout=_WS_PROFILE_START_WAIT_S, return_when=asyncio.FIRST_COMPLETED)+ finally:+ for wait in waits:+ wait.cancel()frames = held[key]+ if client_gone.is_set() and not started.is_set():+ for _ in frames:+ held_room.release()+ frames.clear()while frames:await _dispatch_one(frames[0])frames.popleft()@@ -535,8 +546,8 @@ async def handle_ws(ws: Any, *, auth_identity: dict | None = None, subprotocol:return Trueasync def _end_held() -> None:- """Held frames were read before the disconnect: their lanes finish, as the serial lane finishes- its queue, unless the connection itself was cancelled (server shutdown)."""+ """Frames held for a start that ended before the disconnect finish, as the serial lane finishes+ its queue (the others were dropped), unless the connection itself was cancelled (server shutdown)."""task = asyncio.current_task()if task is not None and task.cancelling():for lane in held_lanes:@@ -673,6 +684,7 @@ async def handle_ws(ws: Any, *, auth_identity: dict | None = None, subprotocol:except _SendFailed:passfinally:+ client_gone.set()if dispatcher is not None:# Finish the in-flight handler and the frames read before the disconnect (as the serial read loop# did) before the teardown below parks this transport's sessions. A cancelled connection (server