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(wechat): cushion long-poll timeouts

cyhe0366c6226a1
diff --git a/modules/im/wechat_api.py b/modules/im/wechat_api.py
index 353d22ade..33f415951 100644
--- a/modules/im/wechat_api.py
+++ b/modules/im/wechat_api.py
@@ -7,6 +7,7 @@ auth/QR endpoints are GET.
Ported from the TypeScript reference implementation.
"""
+import asyncio
import base64
import logging
import struct
@@ -52,6 +53,7 @@ UPLOAD_MEDIA_VOICE = 4
# Timeouts (milliseconds)
DEFAULT_LONG_POLL_TIMEOUT_MS = 35_000
+DEFAULT_LONG_POLL_TIMEOUT_GRACE_MS = 5_000
DEFAULT_API_TIMEOUT_MS = 15_000
DEFAULT_CONFIG_TIMEOUT_MS = 10_000
@@ -176,7 +178,7 @@ async def get_updates(
body_bytes = body_str.encode("utf-8")
headers = _build_headers(token=token, body_bytes=body_bytes)
- timeout = aiohttp.ClientTimeout(total=timeout_ms / 1000.0)
+ timeout = aiohttp.ClientTimeout(total=(timeout_ms + DEFAULT_LONG_POLL_TIMEOUT_GRACE_MS) / 1000.0)
logger.info(
"get_updates: POST %s token=%s timeout=%dms buf_len=%d",
url,
@@ -198,7 +200,7 @@ async def get_updates(
if not resp.ok:
raise RuntimeError(f"getUpdates {resp.status}: {raw_text}")
return json.loads(raw_text)
- except (aiohttp.ServerTimeoutError, TimeoutError):
+ except (aiohttp.ServerTimeoutError, asyncio.TimeoutError, TimeoutError):
logger.debug(
"getUpdates: client-side timeout after %dms, returning empty response",
timeout_ms,
diff --git a/tests/test_wechat_bot.py b/tests/test_wechat_bot.py
index 3345c07b0..5e32736c2 100644
--- a/tests/test_wechat_bot.py
+++ b/tests/test_wechat_bot.py
@@ -10,6 +10,7 @@ sys.path.insert(0, str(Path(__file__).resolve().parents[1]))
from core.auth import AuthResult
from modules.im import MessageContext
from modules.im.wechat import WeChatBot, WeChatConfig, _get_updates_error_code
+from modules.im import wechat_api as wechat_api_module
class WeChatBotTests(unittest.IsolatedAsyncioTestCase):
@@ -50,6 +51,117 @@ class WeChatBotTests(unittest.IsolatedAsyncioTestCase):
self.assertEqual(_get_updates_error_code({"errcode": -14, "errmsg": "session timeout"}), -14)
self.assertIsNone(_get_updates_error_code({"ret": 0}))
+ async def test_wechat_api_get_updates_adds_long_poll_timeout_grace(self):
+ captured = {}
+
+ class _Response:
+ ok = True
+ status = 200
+
+ async def __aenter__(self):
+ return self
+
+ async def __aexit__(self, exc_type, exc, tb):
+ return False
+
+ async def text(self):
+ return '{"ret": 0, "msgs": [], "get_updates_buf": "buf-2"}'
+
+ class _Session:
+ def __init__(self, timeout):
+ captured["timeout"] = timeout.total
+
+ async def __aenter__(self):
+ return self
+
+ async def __aexit__(self, exc_type, exc, tb):
+ return False
+
+ def post(self, url, data=None, headers=None):
+ captured["url"] = url
+ return _Response()
+
+ with patch("modules.im.wechat_api.aiohttp.ClientSession", side_effect=lambda timeout: _Session(timeout)):
+ result = await wechat_api_module.get_updates(
+ "https://wechat.example.com",
+ "token",
+ "buf-1",
+ timeout_ms=35_000,
+ )
+
+ self.assertEqual(result["get_updates_buf"], "buf-2")
+ self.assertEqual(captured["timeout"], 40.0)
+ self.assertTrue(captured["url"].endswith("/ilink/bot/getupdates"))
+
+ async def test_wechat_api_get_updates_returns_empty_response_on_timeout(self):
+ class _TimeoutingRequest:
+ async def __aenter__(self):
+ raise TimeoutError()
+
+ async def __aexit__(self, exc_type, exc, tb):
+ return False
+
+ class _Session:
+ async def __aenter__(self):
+ return self
+
+ async def __aexit__(self, exc_type, exc, tb):
+ return False
+
+ def post(self, url, data=None, headers=None):
+ return _TimeoutingRequest()
+
+ with patch("modules.im.wechat_api.aiohttp.ClientSession", side_effect=lambda timeout: _Session()):
+ result = await wechat_api_module.get_updates(
+ "https://wechat.example.com",
+ "token",
+ "buf-1",
+ timeout_ms=35_000,
+ )
+
+ self.assertEqual(result, {"ret": 0, "msgs": [], "get_updates_buf": "buf-1"})
+
+ async def test_wechat_api_fetch_keeps_non_long_poll_timeout_unchanged(self):
+ captured = {}
+
+ class _Response:
+ ok = True
+ status = 200
+
+ async def __aenter__(self):
+ return self
+
+ async def __aexit__(self, exc_type, exc, tb):
+ return False
+
+ async def text(self):
+ return '{"ret": 0}'
+
+ class _Session:
+ def __init__(self, timeout):
+ captured["timeout"] = timeout.total
+
+ async def __aenter__(self):
+ return self
+
+ async def __aexit__(self, exc_type, exc, tb):
+ return False
+
+ def post(self, url, data=None, headers=None):
+ return _Response()
+
+ with patch("modules.im.wechat_api.aiohttp.ClientSession", side_effect=lambda timeout: _Session(timeout)):
+ result = await wechat_api_module._api_fetch(
+ "https://wechat.example.com",
+ "ilink/bot/test",
+ {"hello": "world"},
+ token="token",
+ timeout_ms=15_000,
+ )
+
+ self.assertEqual(result, {"ret": 0})
+ self.assertEqual(captured["timeout"], 15.0)
+
async def test_get_user_info_uses_short_fixed_display_name(self):
bot = self._make_bot()