Skip to content

fix: 修复qq Websoket适配器的重复回复问题#7265

Open
Thelia-Lzr wants to merge 3 commits intoAstrBotDevs:masterfrom
Thelia-Lzr:master
Open

fix: 修复qq Websoket适配器的重复回复问题#7265
Thelia-Lzr wants to merge 3 commits intoAstrBotDevs:masterfrom
Thelia-Lzr:master

Conversation

@Thelia-Lzr
Copy link
Copy Markdown
Contributor

@Thelia-Lzr Thelia-Lzr commented Apr 1, 2026

fix #7259

Modifications / 改动点

  • qqofficial_platform_adapterbotClient加了一个_handler_enabled标记
  • 在消息入口on_*增加判断,如果_handler_enabled为false则不给予响应
  • 关闭适配器时使得_handler_enabled变为false,即使无法完全清理监听,也可以使得残留监听不会干扰业务
  • 坦白承认这个关闭方法实在太不优雅(),因为残留的监听器仍然在占用资源
  • This is NOT a breaking change. / 这不是一个破坏性变更。

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

  • 为astrbot新增一个qq_websocket适配器
  • 反复开关尝试复现原先的bug
  • 应该会发现没有了()

QQ20260401-194153


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

Guard QQ official platform websocket handlers with an enable flag to avoid processing messages after the adapter is terminated.

Bug Fixes:

  • Prevent QQ official platform message handlers from running after the adapter has been terminated.

Enhancements:

  • Add an internal handler-enabled flag on the QQ official client and associated logging when enabling or disabling handlers.

Copilot AI review requested due to automatic review settings April 1, 2026 11:48
@auto-assign auto-assign bot requested review from LIghtJUNction and anka-afk April 1, 2026 11:48
@dosubot dosubot bot added the size:S This PR changes 10-29 lines, ignoring generated files. label Apr 1, 2026
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:

  • 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.
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.

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.

@dosubot dosubot bot added the area:platform The bug / feature is about IM platform adapter, such as QQ, Lark, Telegram, WebChat and so on. label Apr 1, 2026
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 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.


await self.client.close()
logger.info("QQ 官方机器人接口 适配器已被优雅地关闭")
logger.info("QQ 官方机器人接口 适配器已被不太优雅地关闭")
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

日志消息“不太优雅地关闭”显得不够正式。建议使用更专业的表述,例如“已停止”或“已关闭”。

Suggested change
logger.info("QQ 官方机器人接口 适配器已被不太优雅地关闭")
logger.info("QQ 官方机器人接口 适配器已停止")

Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

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_enabled flag to the botClient used by qqofficial_platform_adapter.
  • Guard all on_*_message_create entrypoints to early-return when handlers are disabled.
  • Toggle the flag on adapter run() (enable) and terminate() (disable), with debug logging around state changes.


await self.client.close()
logger.info("QQ 官方机器人接口 适配器已被优雅地关闭")
logger.info("QQ 官方机器人接口 适配器已被不太优雅地关闭")
Copy link

Copilot AI Apr 1, 2026

Choose a reason for hiding this comment

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

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.

Suggested change
logger.info("QQ 官方机器人接口 适配器已被不太优雅地关闭")
logger.info("QQ 官方机器人接口适配器已关闭")
logger.debug("QQ 官方机器人接口适配器已被不太优雅地关闭(使用了非完全优雅的关闭路径)")

Copilot uses AI. Check for mistakes.
@Thelia-Lzr Thelia-Lzr changed the title fix: add _handler_enabled to qqofficial_platform_adapter fix: 修复qq Websoket适配器的重复回复问题 Apr 1, 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.

[Bug]适配器重复回复内容

2 participants