fix(weixin): recognize ret=-2 errmsg='unknown error' as stale-session signal (#17228) - #17432
Merged
Conversation
The Weixin adapter only recognized errcode=-14 as a session-expired signal. However, iLink also returns ret=-2 with errmsg="unknown error" for the same underlying condition (stale session). The adapter treated ret=-2 as a rate-limit, exhausting retries with the same stale context_token instead of refreshing the session. Added _is_stale_session_ret() helper that distinguishes ret=-2 with "unknown error" from genuine rate limits. Updated both the poll loop and _send_text_chunk to use the helper. Fixes #17228
Regression test for the ret=-2 / errmsg='unknown error' disambiguation: - ret=-2 or errcode=-2 with 'unknown error' → stale session (True) - ret=-2 with 'freq limit' or other errmsg → rate limit (False) - ret=-14 → not matched here (handled by SESSION_EXPIRED_ERRCODE path) - Success codes and missing errmsg → False
This was referenced Apr 29, 2026
6 tasks
Open
13 tasks
This was referenced Jul 31, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Weixin cron pushes to inactive chats now recover by stripping the stale
context_tokenand retrying tokenless, instead of being silently dropped.Root cause: iLink returns
ret=-2 errmsg="unknown error"for a stalecontext_token(same condition aserrcode=-14), but the adapter was treating allret=-2as a rate limit and exhausting retries with the same bad token. Genuine rate limits areret=-2with a different errmsg (e.g."freq limit") — the two share a code but are distinguishable by errmsg.Changes
gateway/platforms/weixin.py: new_is_stale_session_ret(ret, errcode, errmsg)helper that matches-2only whenerrmsg == "unknown error". Wired into both_send_text_chunk(send path) and the poll loop. Genuine rate-limit-2still takes the existing backoff path.tests/gateway/test_weixin.py: 7-case truth table for the helper — covers stale/rate-limit disambiguation, case-insensitivity, empty errmsg, and non-match on-14.Validation
ret=-2 errmsg="unknown error"(stale token)context_token, retry tokenless, push succeedsret=-2 errmsg="freq limit"(genuine rate limit)errcode=-14(documented session expired)tests/gateway/test_weixin.py: 49 passed.Credit
Salvaged from #17287 by @vominh1919 — original commit preserved via cherry-pick. Also closes #16465 by @Grey0202 (submitted first, same root cause, different implementation — blanket
-2matching which would have masked genuine rate limits).Closes #17228.