fix(websocket): isolate slow clients from outbound fanout (#5655)

* fix(websocket): isolate slow outbound clients

* fix(websocket): preserve thin dispatch boundary
This commit is contained in:
chengyongru authored and GitHub committed 2026-09-04 15:39:59 +08:00
1 parent 6b82d642fc
commit e2df7df9f4
3 files changed
+684 -37

No files matched your search

@@ -143,6 +143,14 @@ async def _connect_when_ready(url: str) -> Any:
await asyncio.sleep(0.02)
async def _wait_for_connection_cleanup(
channel: WebSocketChannel,
connection: Any,
) -> None:
while connection in channel._conn_chats:
await asyncio.sleep(0)
async def _webui_mutate(
client: Any,
action: str,
@@ -1392,6 +1400,7 @@ async def test_webui_sidebar_state_update_broadcasts_workbench_to_other_devices(
source.request = SimpleNamespace(headers=Headers())
other_device = AsyncMock()
channel._webui_connections.update({source, other_device})
channel._register_connection_outbound(other_device)
request_id = "sidebar-workbench-state"
await channel._dispatch_envelope(
@@ -2243,6 +2252,7 @@ async def test_send_removes_connection_on_connection_closed() -> None:
msg = OutboundMessage(channel="websocket", chat_id="chat-1", content="hello")
await channel.send(msg)
await asyncio.wait_for(_wait_for_connection_cleanup(channel, mock_ws), timeout=1)
assert "chat-1" not in channel._subs
assert mock_ws not in channel._conn_chats
@@ -2384,6 +2394,7 @@ async def test_send_delta_removes_connection_on_connection_closed() -> None:
channel._attach(mock_ws, "chat-1")
await channel.send_delta("chat-1", "chunk", stream_id="s1")
await asyncio.wait_for(_wait_for_connection_cleanup(channel, mock_ws), timeout=1)
assert "chat-1" not in channel._subs
assert mock_ws not in channel._conn_chats
@@ -2872,7 +2883,7 @@ async def test_system_command_turn_end_only_refreshes_session_metadata() -> None
("owner-new", "owner-old", False),
],
)
async def test_turn_end_persists_and_conditionally_clears_when_fanout_fails(
async def test_turn_end_persists_and_conditionally_clears_when_delivery_fails(
active_owner: str,
event_owner: str,
expected_cleared: bool,
@@ -2891,14 +2902,14 @@ async def test_turn_end_persists_and_conditionally_clears_when_fanout_fails(
wth._WEBSOCKET_TURN_OWNERS[chat_id] = active_owner
try:
with pytest.raises(RuntimeError, match="fanout failed"):
await channel.send(OutboundMessage(
channel="websocket",
chat_id=chat_id,
content="",
metadata={WEBSOCKET_TURN_OWNER_METADATA_KEY: event_owner},
event=TurnEndEvent(),
))
await channel.send(OutboundMessage(
channel="websocket",
chat_id=chat_id,
content="",
metadata={WEBSOCKET_TURN_OWNER_METADATA_KEY: event_owner},
event=TurnEndEvent(),
))
await asyncio.wait_for(_wait_for_connection_cleanup(channel, mock_ws), timeout=1)
assert read_transcript_lines(f"websocket:{chat_id}")[-1]["event"] == "turn_end"
assert (wth.websocket_turn_wall_started_at(chat_id) is None) is expected_cleared
@@ -3264,7 +3275,7 @@ async def test_non_webui_transcript_failure_does_not_block_idle_cleanup(
@pytest.mark.asyncio
async def test_idle_clears_matching_owner_when_fanout_fails() -> None:
async def test_idle_clears_matching_owner_when_delivery_fails() -> None:
bus = MagicMock()
channel = WebSocketChannel(
{"enabled": True, "allowFrom": ["*"]},
@@ -3279,14 +3290,14 @@ async def test_idle_clears_matching_owner_when_fanout_fails() -> None:
wth._WEBSOCKET_TURN_WALL_STARTED_AT[chat_id] = 1234.5
wth._WEBSOCKET_TURN_OWNERS[chat_id] = owner
with pytest.raises(RuntimeError, match="fanout failed"):
await channel.send(OutboundMessage(
channel="websocket",
chat_id=chat_id,
content="",
metadata={WEBSOCKET_TURN_OWNER_METADATA_KEY: owner},
event=GoalStatusEvent(status="idle"),
))
await channel.send(OutboundMessage(
channel="websocket",
chat_id=chat_id,
content="",
metadata={WEBSOCKET_TURN_OWNER_METADATA_KEY: owner},
event=GoalStatusEvent(status="idle"),
))
await asyncio.wait_for(_wait_for_connection_cleanup(channel, mock_ws), timeout=1)
assert wth.websocket_turn_wall_started_at(chat_id) is None
assert chat_id not in wth._WEBSOCKET_TURN_OWNERS
@@ -3620,7 +3631,7 @@ async def test_send_session_updated_includes_scope_when_present() -> None:
@pytest.mark.asyncio
async def test_send_non_connection_closed_exception_is_raised() -> None:
async def test_send_non_connection_closed_exception_is_isolated() -> None:
bus = MagicMock()
channel = WebSocketChannel({"enabled": True, "allowFrom": ["*"]}, bus, gateway=_basic_handler(bus))
mock_ws = AsyncMock()
@@ -3628,8 +3639,11 @@ async def test_send_non_connection_closed_exception_is_raised() -> None:
channel._attach(mock_ws, "chat-1")
msg = OutboundMessage(channel="websocket", chat_id="chat-1", content="hello")
with pytest.raises(RuntimeError, match="unexpected"):
await channel.send(msg)
await channel.send(msg)
await asyncio.wait_for(_wait_for_connection_cleanup(channel, mock_ws), timeout=1)
mock_ws.close.assert_awaited_once_with(code=1011, reason="outbound send failed")
assert "chat-1" not in channel._subs
@pytest.mark.asyncio