The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(webapp): count frames held for a starting profile against the socket's bound
Adolanium422f2f84564f
diff --git a/tests/gateway/test_sibling_attach_while_profile_starting.py b/tests/gateway/test_sibling_attach_while_profile_starting.pyindex c5e856e0b47..71939ade9a1 100644--- a/tests/gateway/test_sibling_attach_while_profile_starting.py+++ b/tests/gateway/test_sibling_attach_while_profile_starting.py@@ -187,3 +187,34 @@ async def test_a_profile_bound_grant_naming_a_starting_profile_is_refused_at_oncrig.create(sock, 'beta', 'bound-names-beta')reply = await sock.reply('bound-names-beta', timeout=10)assert reply.get('error', {}).get('message') == 'profile_mismatch', reply+++async def test_frames_held_for_a_starting_profile_still_park_the_reader(rig, monkeypatch):+ """Held frames count against the socket's dispatch bound: a flood naming the starting profile stops+ being read off the socket once that many wait, instead of piling up in memory, and every frame is+ still answered once the start ends."""+ from tui_gateway import ws as ws_mod++ bound = 4+ monkeypatch.setattr(ws_mod, '_WS_DISPATCH_QUEUE_MAX', bound)+ sock = await rig.connect()+ flood = 10 * bound+ for n in range(flood):+ sock.inbound.put_nowait({'jsonrpc': '2.0', 'id': f'held-{n}', 'method': 'ping', 'params': {'profile': 'beta'}})++ # The reader settles once it parks: what it took is the held bound, the queue bound and one frame+ # each in the dispatcher's and the reader's hands.+ unread = flood+ for _ in range(100):+ await asyncio.sleep(0.02)+ if sock.inbound.qsize() == unread:+ break+ unread = sock.inbound.qsize()+ assert flood - sock.inbound.qsize() <= 2 * bound + 2, 'held frames escaped the dispatch bound'+ assert not sock.replies # all still waiting for beta++ rig.release.set()+ await asyncio.wait_for(rig.serving, 30)+ answered = [await sock.reply(f'held-{n}', timeout=10) for n in range(flood)]+ assert [reply['id'] for reply in answered] == [f'held-{n}' for n in range(flood)]diff --git a/tui_gateway/ws.py b/tui_gateway/ws.pyindex d0df1439af7..adaabf0036f 100644--- a/tui_gateway/ws.py+++ b/tui_gateway/ws.py@@ -451,6 +451,9 @@ async def handle_ws(ws: Any, *, auth_identity: dict | None = None, subprotocol:# Frames waiting for a profile's start, per start, in arrival order; one lane task drains each.held: dict[str, collections.deque] = {}held_lanes: set[asyncio.Task] = set()+ # 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)async def _dispatch_one(req: Any) -> None:"""Dispatch one request and write its inline reply."""@@ -504,15 +507,22 @@ async def handle_ws(ws: Any, *, auth_identity: dict | None = None, subprotocol:while frames:await _dispatch_one(frames[0])frames.popleft()+ held_room.release()except _SendFailed:if not send_failed.done():send_failed.set_result(None)finally:del held[key]- def _hold_for_profile_start(req: Any) -> bool:- start = authority_connection.awaits_profile_start(req, held) if authority_connection is not None else None+ async def _hold_for_profile_start(req: Any) -> bool:+ if authority_connection is None or authority_connection.awaits_profile_start(req, held) is None:+ return False+ await _unless_dispatch_failed(held_room.acquire())+ # Asked again after waiting for room: the start may have ended and its lane drained meanwhile,+ # and then the frame goes to the serial lane behind the frames that lane already answered.+ start = authority_connection.awaits_profile_start(req, held)if start is None:+ held_room.release()return Falsekey, started = startif key in held:@@ -551,7 +561,7 @@ async def handle_ws(ws: Any, *, auth_identity: dict | None = None, subprotocol:concurrent_reads.add(task)task.add_done_callback(_finish_concurrent_read)continue- if _hold_for_profile_start(req):+ if await _hold_for_profile_start(req):continueawait _dispatch_one(req)