fix: clean up qq official websocket shutdown#7395
Merged
Conversation
Contributor
There was a problem hiding this comment.
Hey - I've found 1 issue, and left some high level feedback:
- In
ManagedBotWebSocket.close, consider delegating tosuper().close()after disabling reconnection so any base-class cleanup logic still runs instead of duplicating only the low-level connection close. - It may be safer for
botClient.bot_connectto no-op early ifis_shutting_downis already true, to avoid establishing new websocket sessions during or after shutdown has been initiated.
Prompt for AI Agents
Please address the comments from this code review:
## Overall Comments
- In `ManagedBotWebSocket.close`, consider delegating to `super().close()` after disabling reconnection so any base-class cleanup logic still runs instead of duplicating only the low-level connection close.
- It may be safer for `botClient.bot_connect` to no-op early if `is_shutting_down` is already true, to avoid establishing new websocket sessions during or after shutdown has been initiated.
## Individual Comments
### Comment 1
<location path="astrbot/core/platform/sources/qqofficial/qqofficial_platform_adapter.py" line_range="133-141" />
<code_context>
+ self._active_websockets.add(websocket)
+ try:
+ await websocket.ws_connect()
+ except (Exception, KeyboardInterrupt, SystemExit) as e:
+ if not self.is_shutting_down:
+ await websocket.on_error(e)
+ finally:
+ self._active_websockets.discard(websocket)
</code_context>
<issue_to_address>
**suggestion (bug_risk):** Catching KeyboardInterrupt/SystemExit here without re-raising may interfere with process-level shutdown.
Including `KeyboardInterrupt` and `SystemExit` here means this block may suppress normal termination and leave the process in an inconsistent shutdown state, especially if `on_error` doesn’t re-raise. Please either exclude these exceptions from the tuple or explicitly re-raise them after handling so process shutdown behaves as expected.
```suggestion
websocket = ManagedBotWebSocket(session, self._connection, self)
self._active_websockets.add(websocket)
try:
await websocket.ws_connect()
except Exception as e:
if not self.is_shutting_down:
await websocket.on_error(e)
finally:
self._active_websockets.discard(websocket)
```
</issue_to_address>Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.
Contributor
There was a problem hiding this comment.
Code Review
This pull request introduces a ManagedBotWebSocket class and updates the botClient in the QQ Official platform adapter to improve websocket lifecycle management and ensure a clean shutdown. It also includes minor log message refinements across several modules. Feedback was provided regarding the exception handling in the bot_connect method, specifically advising against catching KeyboardInterrupt and SystemExit to avoid interfering with standard process termination signals.
astrbot/core/platform/sources/qqofficial/qqofficial_platform_adapter.py
Outdated
Show resolved
Hide resolved
Co-authored-by: sourcery-ai[bot] <58596630+sourcery-ai[bot]@users.noreply.github.com>
NekyuuYa
pushed a commit
to NekyuuYa/AstrBot-R
that referenced
this pull request
Apr 8, 2026
* fix: clean up qq official websocket shutdown fixes: AstrBotDevs#7259 * fix: do not catch KeyboardInterrupt/SystemExit Co-authored-by: sourcery-ai[bot] <58596630+sourcery-ai[bot]@users.noreply.github.com> --------- Co-authored-by: sourcery-ai[bot] <58596630+sourcery-ai[bot]@users.noreply.github.com>
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.
fixes: #7259
closes: #7265
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
Improve QQ Official platform WebSocket shutdown handling to avoid unnecessary reconnections during client termination and align shutdown logging messages.
Bug Fixes:
Enhancements: