Skip to content

The agent that grows with you

fix(tui): drop test stand-in branch from WSTransport.close, narrow loop-closed guard

kshitijk4poor1d68deacda4a
diff --git a/tests/tui_gateway/test_multi_client_fanout.py b/tests/tui_gateway/test_multi_client_fanout.py
index 620498f33de..2b539d4de44 100644
--- a/tests/tui_gateway/test_multi_client_fanout.py
+++ b/tests/tui_gateway/test_multi_client_fanout.py
@@ -97,13 +97,25 @@ class PipeClient:
return self.frames.get(timeout=5)
+class _SocketWS:
+ """ASGI-ws stand-in for SocketClient: send goes to the socketpair, close is a no-op."""
+ def __init__(self, client):
+ self.client = client
+
+ async def send_text(self, payload):
+ await self.client.send_text(payload)
+
+ async def close(self, code=1000):
+ return None
+
+
class SocketClient(WSTransport):
"""Real WSTransport with its ASGI send backed by a kernel socketpair."""
def __init__(self, stack, *, reading=True):
self.reader, self.writer = socket.socketpair()
self.writer.setblocking(False)
loop = asyncio.new_event_loop()
- super().__init__(self, loop)
+ super().__init__(_SocketWS(self), loop)
self.writes = 0
self.loop_thread = threading.Thread(target=loop.run_forever, daemon=True)
self.loop_thread.start()
diff --git a/tui_gateway/transport.py b/tui_gateway/transport.py
index bd564487df4..f5bf36bdcd6 100644
--- a/tui_gateway/transport.py
+++ b/tui_gateway/transport.py
@@ -145,9 +145,10 @@ class FanoutTransport:
One slow socket must not stop the emitting turn or any healthy subscriber.
Each peer has at most one daemon writer and a bounded backlog. On overflow
- it loses its subscription and that peer's transport is closed so reconnect
- plus history/replay can run; other subscribers are left alone. A write
- already in the OS cannot be revoked.
+ it loses its subscription and that peer's transport is closed so the client
+ notices, reconnects and replays history. Closing the socket also drops any
+ other sessions multiplexed on it; the same reconnect + replay recovers them.
+ Other sockets are unaffected. A write already in the OS cannot be revoked.
"""
_MAX_PENDING_FRAMES = 256
diff --git a/tui_gateway/ws.py b/tui_gateway/ws.py
index b3590f8fa92..93663350bda 100644
--- a/tui_gateway/ws.py
+++ b/tui_gateway/ws.py
@@ -6,6 +6,7 @@ after accept). Mount as ``@app.websocket("/api/ws") async def ws(ws): await hand
from __future__ import annotations
import asyncio
+import contextlib
import concurrent.futures
import json
import logging
@@ -223,8 +224,7 @@ class WSTransport:
self._token_flush_handle = None
if handle is not None:
handle.cancel()
- # SocketClient tests pass ``self`` as the ASGI ws; skip that stand-in.
- if self._ws is not None and self._ws is not self:
+ if self._ws is not None:
self._loop.create_task(self._close_stalled_socket())
try:
@@ -234,10 +234,9 @@ class WSTransport:
if on_loop:
_finish_close()
return
- try:
+ # A loop that already shut down has nothing left to cancel or close.
+ with contextlib.suppress(RuntimeError):
self._loop.call_soon_threadsafe(_finish_close)
- except RuntimeError:
- pass
async def _close_stalled_socket(self) -> None:
"""Close the peer socket after a send deadline so ``handle_ws``'s ``receive_text`` unblocks and its