Skip to content

The agent that grows with you

fix(feishu): a receive loop ending during disconnect() is not logged as a died link

teknium10d0ccb88342e
diff --git a/plugins/platforms/feishu/adapter.py b/plugins/platforms/feishu/adapter.py
index 4e3621af252..26120d699fa 100644
--- a/plugins/platforms/feishu/adapter.py
+++ b/plugins/platforms/feishu/adapter.py
@@ -1066,10 +1066,15 @@ def _install_lark_ws_isolation(ws_client_module: Any) -> None:
# A *normal* return means the SDK's own ladder already reconnected (it scheduled a
# fresh receive loop) and must NOT stop the loop. Deliberate disconnects nil
# ``_ws_client`` first, so the supervisor exits without restarting.
- logger.exception(
- "[Feishu] lark WS receive loop died; stopping the worker "
- "loop so the supervisor can rebuild"
- )
+ adapter = getattr(_ws_isolation_state, "adapter", None)
+ if adapter is None or getattr(adapter, "_running", True):
+ logger.exception(
+ "[Feishu] lark WS receive loop died; stopping the worker "
+ "loop so the supervisor can rebuild"
+ )
+ else:
+ # ``disconnect()`` sent the CLOSE frame itself: the loop ending here is expected.
+ logger.debug("[Feishu] lark WS receive loop ended during disconnect", exc_info=True)
asyncio.get_running_loop().stop()
ws_client_module.Client._receive_message_loop = _receive_message_loop_exit_notify
@@ -1120,6 +1125,7 @@ def _run_official_feishu_ws_client(ws_client: Any, adapter: Any) -> None:
_ws_isolation_state.loop = loop
_ws_isolation_state.connect_kwargs = connect_overrides
_ws_isolation_state.on_link_up = _on_link_up
+ _ws_isolation_state.adapter = adapter
def _configure_with_overrides(conf: Any) -> Any:
if original_configure is None:
@@ -1139,6 +1145,7 @@ def _run_official_feishu_ws_client(ws_client: Any, adapter: Any) -> None:
_ws_isolation_state.loop = None
_ws_isolation_state.connect_kwargs = None
_ws_isolation_state.on_link_up = None
+ _ws_isolation_state.adapter = None
if original_configure is not None:
setattr(ws_client, "_configure", original_configure)
pending = [t for t in asyncio.all_tasks(loop) if not t.done()]
diff --git a/tests/gateway/test_feishu_ws_multiplex_isolation.py b/tests/gateway/test_feishu_ws_multiplex_isolation.py
index 6ec3eba2754..6efa67ff53a 100644
--- a/tests/gateway/test_feishu_ws_multiplex_isolation.py
+++ b/tests/gateway/test_feishu_ws_multiplex_isolation.py
@@ -8,6 +8,7 @@ sibling's loop that never hears anything again.
"""
import asyncio
+import logging
import sys
import threading
import types
@@ -152,6 +153,40 @@ def test_dead_receive_loop_unparks_start_and_exits_the_thread(monkeypatch, caplo
assert "simulated half-open peer" in caplog.text
+def test_receive_loop_death_during_disconnect_is_not_an_error(monkeypatch, caplog):
+ """``disconnect()`` clears ``_running`` and sends the CLOSE frame itself, so the
+ receive loop ending with ``ConnectionClosedOK`` is the expected shutdown path —
+ it must not be reported as a died link (ERROR + traceback) on every graceful stop."""
+ client_mod = _inject_fake_lark_module(monkeypatch)
+
+ class FakeSDKClient:
+ async def _receive_message_loop(self):
+ await asyncio.sleep(0.01)
+ raise ConnectionError("sent 1000 (OK); then received 1000 (OK)")
+
+ def start(self):
+ loop = client_mod.loop
+
+ async def _select():
+ while True:
+ await asyncio.sleep(3600)
+
+ loop.create_task(self._receive_message_loop())
+ loop.run_until_complete(_select())
+
+ client_mod.Client = FakeSDKClient
+ stub = _adapter_stub(_running=False) # disconnect() already flipped it
+
+ thread = threading.Thread(
+ target=lambda: feishu_adapter._run_official_feishu_ws_client(FakeSDKClient(), stub), daemon=True
+ )
+ thread.start()
+ thread.join(timeout=10)
+ assert not thread.is_alive()
+ errors = [r for r in caplog.records if r.levelno >= logging.ERROR and "receive loop" in r.getMessage()]
+ assert errors == [], [r.getMessage() for r in errors]
+
+
def test_receive_loop_normal_return_keeps_start_parked(monkeypatch):
"""The exit-notify wrap must only fire on an exception. When the SDK's own
reconnect ladder succeeds, the old receive loop *returns* (a fresh one was