Skip to content

armgit / avibe

Public Imported from GitHub
0

Make your computer a home for your AI partner. Avibe is the local-first Agent OS: Vibey, its built-in AI partner, works out of the box, and Claude Code, Codex & OpenCode run unchanged beside it — reach them from your browser, phone, or any chat app.

fix(im): fail hot remove on leaked adapter runtime

cyh2d63b8347063
diff --git a/modules/im/base.py b/modules/im/base.py
index 5ebe19f99..97d1aed6a 100644
--- a/modules/im/base.py
+++ b/modules/im/base.py
@@ -149,6 +149,15 @@ class BaseIMClient(ABC):
"""Whether sent messages can be edited after delivery."""
return True
+ def verify_stopped(self) -> bool:
+ """Return whether adapter-owned runtime resources have stopped.
+
+ ``MultiIMClient.remove_client`` joins the adapter's top-level runtime
+ thread. Adapters that start nested SDK/runtime threads should override
+ this so hot-remove can fail instead of silently leaking callbacks.
+ """
+ return True
+
def should_use_thread_for_dm_session(self) -> bool:
"""Check if DM conversations should use thread-based session IDs.
@@ -305,7 +314,7 @@ class BaseIMClient(ABC):
@abstractmethod
async def send_message(
self, context: MessageContext, text: str, parse_mode: Optional[str] = None, reply_to: Optional[str] = None
- ) -> str:
+ ) -> Optional[str]:
"""Send a text message
Args:
@@ -315,14 +324,14 @@ class BaseIMClient(ABC):
reply_to: Optional message ID to reply to
Returns:
- Message ID of sent message
+ Message ID of sent message, or None when not delivered
"""
pass
@abstractmethod
async def send_message_with_buttons(
self, context: MessageContext, text: str, keyboard: InlineKeyboard, parse_mode: Optional[str] = None
- ) -> str:
+ ) -> Optional[str]:
"""Send a message with inline buttons
Args:
@@ -332,7 +341,7 @@ class BaseIMClient(ABC):
parse_mode: Optional formatting mode
Returns:
- Message ID of sent message
+ Message ID of sent message, or None when not delivered
"""
pass
diff --git a/modules/im/feishu.py b/modules/im/feishu.py
index d87d8d95a..206201ab6 100644
--- a/modules/im/feishu.py
+++ b/modules/im/feishu.py
@@ -3328,6 +3328,10 @@ class FeishuBot(BaseIMClient):
else:
self._ws_thread = None
+ def verify_stopped(self) -> bool:
+ thread = self._ws_thread
+ return thread is None or not thread.is_alive()
+
async def shutdown(self) -> None:
"""Best-effort async shutdown."""
self._stop_ws_client()
diff --git a/modules/im/multi.py b/modules/im/multi.py
index e27e3f25d..2d629f2a2 100644
--- a/modules/im/multi.py
+++ b/modules/im/multi.py
@@ -495,6 +495,11 @@ class MultiIMClient(BaseIMClient):
message = f"IM thread for {platform} did not stop within timeout"
logger.error("%s; hot-remove failed", message)
raise IMClientRemovalError(message)
+ verify_stopped = getattr(client, "verify_stopped", None)
+ if callable(verify_stopped) and not bool(verify_stopped()):
+ message = f"IM client for {platform} did not stop all runtime resources"
+ logger.error("%s; hot-remove failed", message)
+ raise IMClientRemovalError(message)
ready_callback = None
with self._clients_lock:
diff --git a/tests/test_feishu_hot_stop.py b/tests/test_feishu_hot_stop.py
index d1076ccd0..b8bdf4875 100644
--- a/tests/test_feishu_hot_stop.py
+++ b/tests/test_feishu_hot_stop.py
@@ -1,9 +1,11 @@
from __future__ import annotations
import asyncio
+import threading
from config.v2_config import LarkConfig
from modules.im.feishu import FeishuBot
+from modules.im.multi import IMClientRemovalError, MultiIMClient
def test_feishu_stop_disconnects_nested_ws_client(monkeypatch):
@@ -63,3 +65,38 @@ def test_feishu_stop_stops_running_lark_ws_loop(monkeypatch):
assert disconnected == [True]
assert thread.is_alive() is False
+
+
+def test_feishu_nested_ws_thread_leak_fails_hot_remove():
+ stop_outer = threading.Event()
+ stop_leaked_ws = threading.Event()
+ leaked_ws = threading.Thread(target=stop_leaked_ws.wait, daemon=True, name="feishu-ws-test")
+ leaked_ws.start()
+ outer = threading.Thread(target=stop_outer.wait, daemon=True, name="feishu-outer-test")
+ outer.start()
+
+ bot = FeishuBot(LarkConfig(app_id="cli_a", app_secret="secret"))
+ bot._ws_thread = leaked_ws
+ client = MultiIMClient({"lark": bot}, primary_platform="lark")
+ client._threads["lark"] = outer
+
+ def _stop_outer_only() -> None:
+ stop_outer.set()
+
+ bot.stop = _stop_outer_only
+
+ try:
+ try:
+ client.remove_client("lark")
+ except IMClientRemovalError as exc:
+ assert "did not stop all runtime resources" in str(exc)
+ else:
+ raise AssertionError("remove_client should fail when Feishu nested WS thread leaks")
+
+ assert client.clients["lark"] is bot
+ assert client._threads["lark"] is outer
+ finally:
+ stop_outer.set()
+ stop_leaked_ws.set()
+ outer.join(timeout=2)
+ leaked_ws.join(timeout=2)