Skip to content

feat(weixin_oc): handle session timeout and clear login state#8196

Merged
Soulter merged 1 commit into
masterfrom
fix/wechat-inbound-error-loop
May 15, 2026
Merged

feat(weixin_oc): handle session timeout and clear login state#8196
Soulter merged 1 commit into
masterfrom
fix/wechat-inbound-error-loop

Conversation

@Soulter
Copy link
Copy Markdown
Member

@Soulter Soulter commented May 15, 2026

Modifications / 改动点

  • This is NOT a breaking change. / 这不是一个破坏性变更。

Screenshots or Test Results / 运行截图或测试结果


Checklist / 检查清单

  • 😊 If there are new features added in the PR, I have discussed it with the authors through issues/emails, etc.
    / 如果 PR 中有新加入的功能,已经通过 Issue / 邮件等方式和作者讨论过。

  • 👀 My changes have been well-tested, and "Verification Steps" and "Screenshots" have been provided above.
    / 我的更改经过了良好的测试,并已在上方提供了“验证步骤”和“运行截图”

  • 🤓 I have ensured that no new dependencies are introduced, OR if new dependencies are introduced, they have been added to the appropriate locations in requirements.txt and pyproject.toml.
    / 我确保没有引入新依赖库,或者引入了新依赖库的同时将其添加到 requirements.txtpyproject.toml 文件相应位置。

  • 😮 My changes do not introduce malicious code.
    / 我的更改没有引入恶意代码。

Summary by Sourcery

Handle Weixin OC session timeout errors by resetting login state and persisting the cleared account context.

New Features:

  • Automatically clear Weixin OC login and conversation state when the platform reports a session timeout.

Enhancements:

  • Add centralized helper for reading Weixin OC API error codes and introduce a short backoff delay after inbound polling errors.

@Soulter Soulter merged commit 6982ef7 into master May 15, 2026
20 checks passed
@Soulter Soulter deleted the fix/wechat-inbound-error-loop branch May 15, 2026 07:31
Copy link
Copy Markdown
Contributor

@sourcery-ai sourcery-ai Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey - I've left some high level feedback:

  • When clearing the session in _handle_inbound_session_timeout, consider also resetting related error/connection state (e.g., _last_inbound_error, any long-running tasks using self.token) to avoid subsequent logic acting on stale error/connection information.
  • The new _api_errcode helper and SESSION_TIMEOUT_ERRCODE constant are only used in _poll_inbound_updates; if the same timeout condition can occur in other API paths (e.g., outbound calls), it may be worth reusing this logic there for consistent handling of session expiry.
Prompt for AI Agents
Please address the comments from this code review:

## Overall Comments
- When clearing the session in `_handle_inbound_session_timeout`, consider also resetting related error/connection state (e.g., `_last_inbound_error`, any long-running tasks using `self.token`) to avoid subsequent logic acting on stale error/connection information.
- The new `_api_errcode` helper and `SESSION_TIMEOUT_ERRCODE` constant are only used in `_poll_inbound_updates`; if the same timeout condition can occur in other API paths (e.g., outbound calls), it may be worth reusing this logic there for consistent handling of session expiry.

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

Copy link
Copy Markdown
Contributor

@gemini-code-assist gemini-code-assist Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request introduces session timeout handling for the weixin_oc adapter by adding a specific error code constant and a handler to reset the adapter's state. The polling logic was updated to detect this timeout and trigger the cleanup process. Feedback suggests explicitly calling _cleanup_typing_tasks() within the timeout handler to ensure active tasks are stopped and resources are released immediately.

Comment on lines +959 to +970
async def _handle_inbound_session_timeout(self) -> None:
logger.warning(
"weixin_oc(%s): session timed out, clearing login state and waiting for QR login.",
self.meta().id,
)
self.token = None
self.account_id = None
self._sync_buf = ""
self._context_tokens = {}
self._context_tokens_dirty = False
self._login_session = None
await self._save_account_state()
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

When a session times out, it is important to clean up any active typing keepalive tasks. These tasks rely on the session and will continue to run and fail repeatedly if not stopped. Explicitly calling _cleanup_typing_tasks() ensures that these resources are released immediately.

Suggested change
async def _handle_inbound_session_timeout(self) -> None:
logger.warning(
"weixin_oc(%s): session timed out, clearing login state and waiting for QR login.",
self.meta().id,
)
self.token = None
self.account_id = None
self._sync_buf = ""
self._context_tokens = {}
self._context_tokens_dirty = False
self._login_session = None
await self._save_account_state()
async def _handle_inbound_session_timeout(self) -> None:
logger.warning(
"weixin_oc(%s): session timed out, clearing login state and waiting for QR login.",
self.meta().id,
)
await self._cleanup_typing_tasks()
self.token = None
self.account_id = None
self._sync_buf = ""
self._context_tokens = {}
self._context_tokens_dirty = False
self._login_session = None
await self._save_account_state()

@dosubot dosubot Bot added size:S This PR changes 10-29 lines, ignoring generated files. area:platform The bug / feature is about IM platform adapter, such as QQ, Lark, Telegram, WebChat and so on. labels May 15, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area:platform The bug / feature is about IM platform adapter, such as QQ, Lark, Telegram, WebChat and so on. size:S This PR changes 10-29 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant