Skip to content

fix: clean up qq official websocket shutdown#7395

Merged
Soulter merged 2 commits intomasterfrom
fix/qqofficial
Apr 6, 2026
Merged

fix: clean up qq official websocket shutdown#7395
Soulter merged 2 commits intomasterfrom
fix/qqofficial

Conversation

@Soulter
Copy link
Copy Markdown
Member

@Soulter Soulter commented Apr 6, 2026

fixes: #7259
closes: #7265

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

Improve QQ Official platform WebSocket shutdown handling to avoid unnecessary reconnections during client termination and align shutdown logging messages.

Bug Fixes:

  • Prevent QQ Official WebSocket from attempting to reconnect while the client is shutting down, avoiding noisy or incorrect reconnection behavior on termination.

Enhancements:

  • Introduce a managed WebSocket wrapper that tracks active QQ Official WebSocket connections for coordinated shutdown.
  • Add a dedicated shutdown routine on the QQ Official bot client to close active WebSocket connections before closing the client.
  • Simplify and standardize shutdown log messages for QQ Official adapters and the Web UI by removing the "graceful" wording.

@dosubot dosubot bot added the size:M This PR changes 30-99 lines, ignoring generated files. label Apr 6, 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 found 1 issue, and left some high level feedback:

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

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 6, 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 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.

Co-authored-by: sourcery-ai[bot] <58596630+sourcery-ai[bot]@users.noreply.github.com>
@Soulter Soulter merged commit 2a3d93a into master Apr 6, 2026
6 of 7 checks passed
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>
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:M This PR changes 30-99 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

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

1 participant