Skip to content

Commit 559b171

Browse files
fix(weixin_oc): keep a cleared timeout field from disabling the HTTP timeout (#10229)
1 parent fbf5286 commit 559b171

2 files changed

Lines changed: 98 additions & 6 deletions

File tree

‎astrbot/core/platform/sources/weixin_oc/weixin_oc_adapter.py‎

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,9 @@ class WeixinOCAdapter(Platform):
113113
REPLY_MATCH_WINDOW_MS = 60_000
114114
RECENT_SESSION_CACHE_TTL_S = 1_800
115115
MAX_RECENT_MESSAGE_SESSIONS = 500
116+
# Aiohttp reads ClientTimeout(total=0) as "no timeout", and the dashboard
117+
# writes 0 when a numeric field is cleared, so a timeout must stay positive.
118+
MIN_TIMEOUT_MS = 1_000
116119

117120
def __init__(
118121
self,
@@ -127,15 +130,20 @@ def __init__(
127130
platform_config.get("weixin_oc_base_url", "https://ilinkai.weixin.qq.com")
128131
).rstrip("/")
129132
self.bot_type = str(platform_config.get("weixin_oc_bot_type", "3"))
130-
self.qr_poll_interval = max(
133+
self.qr_poll_interval = self._get_int_config(
134+
"weixin_oc_qr_poll_interval",
135+
1,
131136
1,
132-
int(platform_config.get("weixin_oc_qr_poll_interval", 1)),
133137
)
134-
self.long_poll_timeout_ms = int(
135-
platform_config.get("weixin_oc_long_poll_timeout_ms", 35_000),
138+
self.long_poll_timeout_ms = self._get_int_config(
139+
"weixin_oc_long_poll_timeout_ms",
140+
35_000,
141+
self.MIN_TIMEOUT_MS,
136142
)
137-
self.api_timeout_ms = int(
138-
platform_config.get("weixin_oc_api_timeout_ms", 120_000),
143+
self.api_timeout_ms = self._get_int_config(
144+
"weixin_oc_api_timeout_ms",
145+
120_000,
146+
self.MIN_TIMEOUT_MS,
139147
)
140148
self.cdn_base_url = str(
141149
platform_config.get(
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
"""The two user-adjustable weixin_oc timeouts must not disable the HTTP timeout.
2+
3+
``weixin_oc_long_poll_timeout_ms`` and ``weixin_oc_api_timeout_ms`` are the only
4+
personal-WeChat fields the dashboard exposes for tuning (see
5+
``tests/test_weixin_oc_config_metadata.py``), and its numeric field writes ``0``
6+
when the box is cleared (``toNumber`` maps ``parseFloat('')`` to ``0``). Aiohttp
7+
treats ``ClientTimeout(total=0)`` as "no timeout" -- measured here: a request that
8+
sleeps 0.4 s returns 200 with ``total=0`` and raises ``TimeoutError`` with
9+
``total=0.001`` -- so a cleared field leaves the receive loop waiting on a single
10+
stalled ``getupdates`` connection forever, which is exactly the case its
11+
``except asyncio.TimeoutError`` retry branch exists for.
12+
"""
13+
14+
import asyncio
15+
16+
from astrbot.core.platform.sources.weixin_oc.weixin_oc_adapter import (
17+
WeixinOCAdapter,
18+
)
19+
20+
DEFAULTS = {
21+
"weixin_oc_qr_poll_interval": 1,
22+
"weixin_oc_long_poll_timeout_ms": 35_000,
23+
"weixin_oc_api_timeout_ms": 120_000,
24+
}
25+
MINIMUM_TIMEOUT_MS = 1_000
26+
27+
28+
def _adapter(**overrides) -> WeixinOCAdapter:
29+
config = {"id": "weixin_oc_test", **overrides}
30+
return WeixinOCAdapter(config, {"settings": {}}, asyncio.Queue())
31+
32+
33+
def test_cleared_api_timeout_falls_back_to_a_positive_floor():
34+
adapter = _adapter(weixin_oc_api_timeout_ms=0)
35+
assert adapter.api_timeout_ms == MINIMUM_TIMEOUT_MS
36+
# the value that actually reaches aiohttp.ClientTimeout(total=...)
37+
assert adapter.client.api_timeout_ms == MINIMUM_TIMEOUT_MS
38+
39+
40+
def test_cleared_long_poll_timeout_falls_back_to_a_positive_floor():
41+
adapter = _adapter(weixin_oc_long_poll_timeout_ms=0)
42+
assert adapter.long_poll_timeout_ms == MINIMUM_TIMEOUT_MS
43+
44+
45+
def test_negative_timeouts_are_clamped_like_the_login_flow():
46+
adapter = _adapter(
47+
weixin_oc_api_timeout_ms=-1,
48+
weixin_oc_long_poll_timeout_ms=-35_000,
49+
)
50+
assert adapter.api_timeout_ms == MINIMUM_TIMEOUT_MS
51+
assert adapter.long_poll_timeout_ms == MINIMUM_TIMEOUT_MS
52+
53+
54+
def test_config_value_below_the_floor_still_clamped():
55+
adapter = _adapter(weixin_oc_api_timeout_ms=200)
56+
assert adapter.api_timeout_ms == MINIMUM_TIMEOUT_MS
57+
58+
59+
def test_a_tuned_value_is_kept():
60+
adapter = _adapter(
61+
weixin_oc_api_timeout_ms=5_000,
62+
weixin_oc_long_poll_timeout_ms=45_000,
63+
)
64+
assert adapter.api_timeout_ms == 5_000
65+
assert adapter.long_poll_timeout_ms == 45_000
66+
67+
68+
def test_unset_fields_keep_the_documented_defaults():
69+
adapter = _adapter()
70+
assert adapter.api_timeout_ms == DEFAULTS["weixin_oc_api_timeout_ms"]
71+
assert adapter.long_poll_timeout_ms == DEFAULTS["weixin_oc_long_poll_timeout_ms"]
72+
assert adapter.qr_poll_interval == DEFAULTS["weixin_oc_qr_poll_interval"]
73+
74+
75+
def test_a_non_numeric_field_does_not_kill_the_platform_adapter():
76+
"""PlatformManager.initialize only logs a bare `invalid literal ...`."""
77+
adapter = _adapter(
78+
weixin_oc_api_timeout_ms="",
79+
weixin_oc_long_poll_timeout_ms=None,
80+
weixin_oc_qr_poll_interval="",
81+
)
82+
assert adapter.api_timeout_ms == DEFAULTS["weixin_oc_api_timeout_ms"]
83+
assert adapter.long_poll_timeout_ms == DEFAULTS["weixin_oc_long_poll_timeout_ms"]
84+
assert adapter.qr_poll_interval == DEFAULTS["weixin_oc_qr_poll_interval"]

0 commit comments

Comments
 (0)