Conversation
There was a problem hiding this comment.
Hey - I've left some high level feedback:
- The
_handler_enabledchecks are duplicated across allon_*handlers; consider factoring this into a shared helper or decorator to keep the guard logic in one place and reduce the chance of inconsistent behavior in future handlers. - Since
_handler_enabledis accessed from outside the class (run/terminate), it may be clearer to expose explicitenable_handlers()/disable_handlers()methods onbotClientinstead of mutating a pseudo-private attribute from the adapter. - The log message "适配器已被不太优雅地关闭" may be confusing in production logs; consider keeping a neutral message for operational clarity and perhaps move the commentary into a code comment instead.
Prompt for AI Agents
Please address the comments from this code review:
## Overall Comments
- The `_handler_enabled` checks are duplicated across all `on_*` handlers; consider factoring this into a shared helper or decorator to keep the guard logic in one place and reduce the chance of inconsistent behavior in future handlers.
- Since `_handler_enabled` is accessed from outside the class (`run`/`terminate`), it may be clearer to expose explicit `enable_handlers()` / `disable_handlers()` methods on `botClient` instead of mutating a pseudo-private attribute from the adapter.
- The log message "适配器已被不太优雅地关闭" may be confusing in production logs; consider keeping a neutral message for operational clarity and perhaps move the commentary into a code comment instead.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 a _handler_enabled flag to the botClient class within the QQ Official platform adapter to manage message processing during the bot's lifecycle. Review feedback suggests simplifying the flag checks by using direct attribute access instead of getattr, removing a redundant assignment in the run method, and refining the termination log message for a more professional tone.
astrbot/core/platform/sources/qqofficial/qqofficial_platform_adapter.py
Outdated
Show resolved
Hide resolved
astrbot/core/platform/sources/qqofficial/qqofficial_platform_adapter.py
Outdated
Show resolved
Hide resolved
|
|
||
| await self.client.close() | ||
| logger.info("QQ 官方机器人接口 适配器已被优雅地关闭") | ||
| logger.info("QQ 官方机器人接口 适配器已被不太优雅地关闭") |
There was a problem hiding this comment.
Pull request overview
This PR addresses issue #7259 (duplicate replies after repeatedly stopping/starting the QQ Official adapter) by introducing a runtime flag that disables inbound message handler processing when the adapter is terminated, preventing “zombie” handlers from continuing to enqueue events.
Changes:
- Add a
_handler_enabledflag to thebotClientused byqqofficial_platform_adapter. - Guard all
on_*_message_createentrypoints to early-return when handlers are disabled. - Toggle the flag on adapter
run()(enable) andterminate()(disable), with debug logging around state changes.
|
|
||
| await self.client.close() | ||
| logger.info("QQ 官方机器人接口 适配器已被优雅地关闭") | ||
| logger.info("QQ 官方机器人接口 适配器已被不太优雅地关闭") |
There was a problem hiding this comment.
The terminate log message was changed to "已被不太优雅地关闭" at INFO level. This wording isn’t very actionable for users/operators and may cause confusion/alarm in normal logs. Consider keeping the INFO message neutral (e.g., "已关闭") and moving the "not elegant"/workaround detail to DEBUG so production logs remain clear.
| logger.info("QQ 官方机器人接口 适配器已被不太优雅地关闭") | |
| logger.info("QQ 官方机器人接口适配器已关闭") | |
| logger.debug("QQ 官方机器人接口适配器已被不太优雅地关闭(使用了非完全优雅的关闭路径)") |
fix #7259
Modifications / 改动点
qqofficial_platform_adapter的botClient加了一个_handler_enabled标记on_*增加判断,如果_handler_enabled为false则不给予响应_handler_enabled变为false,即使无法完全清理监听,也可以使得残留监听不会干扰业务坦白承认这个关闭方法实在太不优雅(),因为残留的监听器仍然在占用资源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
Guard QQ official platform websocket handlers with an enable flag to avoid processing messages after the adapter is terminated.
Bug Fixes:
Enhancements: