feat(weixin_oc): handle session timeout and clear login state#8196
Conversation
There was a problem hiding this comment.
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 usingself.token) to avoid subsequent logic acting on stale error/connection information. - The new
_api_errcodehelper andSESSION_TIMEOUT_ERRCODEconstant 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.Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.
There was a problem hiding this comment.
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.
| 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() |
There was a problem hiding this comment.
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.
| 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() |
Modifications / 改动点
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.txtandpyproject.toml./ 我确保没有引入新依赖库,或者引入了新依赖库的同时将其添加到
requirements.txt和pyproject.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:
Enhancements: