fix(qqofficial): bound outbound HTTP concurrency - #9863
Conversation
astrbot/core/platform/sources/qqofficial - Reuse keep-alive connections, bound active and queued requests, and cancel owned sends during adapter shutdown. - Consolidate retries into one jittered three-attempt budget shared by semantic fallbacks and uploads. - Route chunked uploads and both WebSocket and webhook adapters through the managed transport. astrbot/core/pipeline/respond, docs, tests - Remove message payloads and Base64 image data from send failure logs. - Document plugin network-client lifecycle expectations and cover pooling, overload, retry, and shutdown behavior.
There was a problem hiding this comment.
Hey - I've found 2 issues
Prompt for AI Agents
Please address the comments from this code review:
## Individual Comments
### Comment 1
<location path="astrbot/core/platform/sources/qqofficial/qqofficial_message_event.py" line_range="529" />
<code_context>
+ "QQ send retry budget is exhausted"
+ )
+
+ @_qqofficial_retry(
+ max_attempts=attempts_remaining,
+ retry_errors=_QQOFFICIAL_NETWORK_ERRORS,
+ )
+ async def send_attempt():
+ nonlocal attempts_remaining
</code_context>
<issue_to_address>
**issue (broader_impact):** The logical send retry wrapper retries only `_QQOFFICIAL_NETWORK_ERRORS`, so `botpy.errors.ServerError` and `SequenceNumberError` are no longer retried even though both are classified as retryable send API errors and were handled by the previous `_qqofficial_retry` policy. A transient QQ 5xx or sequence error therefore consumes one budget attempt and immediately falls through to semantic fallback or failure.
**Triggers:** When QQ returns a transient ServerError or SequenceNumberError for a send.
**Suggested fix:** Include the retryable API errors in `retry_errors`, while preserving the shared remaining-attempt budget.
```suggestion
retry_errors=(*_QQOFFICIAL_NETWORK_ERRORS, botpy.errors.ServerError, botpy.errors.SequenceNumberError),
```
</issue_to_address>
### Comment 2
<location path="astrbot/core/platform/sources/qqofficial/qqofficial_platform_adapter.py" line_range="186-190" />
<code_context>
class botClient(Client):
def __init__(self, *args: Any, **kwargs: Any) -> None:
super().__init__(*args, **kwargs)
+ self.http = QQOfficialHttp(
+ timeout=self.http.timeout,
+ is_sandbox=self.http.is_sandbox,
+ )
+ self.api = BotAPI(http=self.http)
self._shutting_down = False
self._active_websockets: set[ManagedBotWebSocket] = set()
</code_context>
<issue_to_address>
**issue (bug_risk):** Both adapters replace the botpy HTTP client with a new `QQOfficialHttp`, but the shown adapter shutdown paths do not close that transport. The aiohttp session and keep-alive connector therefore remain open after adapter shutdown, producing unclosed-session warnings and retaining sockets until garbage collection.
**Triggers:** When either QQ Official adapter is stopped after the managed HTTP session has been created.
**Suggested fix:** Explicitly await `self.http.close()` during each adapter's shutdown/termination path before returning.
</issue_to_address>Sourcery assessment
Needs a human reviewer. 2 findings to address first, and if the transport limits or shared retry budget are wrong, QQ API requests can be rejected or messages can be missed or duplicated; requests already sent and externally delivered cannot be undone by reverting. Reverting restores the previous transport and retry behavior for future requests.
Blocking findings: astrbot/core/platform/sources/qqofficial/qqofficial_message_event.py:529, astrbot/core/platform/sources/qqofficial/qqofficial_platform_adapter.py:190
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.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: c4e91079ba
ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
Centralize shutdown-state validation, collapse duplicate C2C send branches, and reuse the retry fixture without changing concurrency or retry behavior.
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 9ebfe1e511
ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
Retry transient QQ send failures inside the shared budget, route proactive and chunked requests through the bounded transport, and preserve Python 3.10 queue timeout handling. Add focused regression coverage for each reviewed path.
QQ Official outbound requests could amplify plugin-triggered traffic because qq-botpy disabled connection reuse, retried connection resets internally, and allowed a closed HTTP session to be recreated by pending retry tasks. Large failed image payloads could also be copied into logs.
This change gives the WebSocket and webhook adapters one managed outbound lifecycle with bounded concurrency, one retry owner, reusable connections, and fail-closed shutdown behavior.
Modifications / 改动点
QQOfficialHttp, a shared transport for both QQ Official adapter modes:terminate(), and avoid stacked retry layers.flowchart LR P[Plugin-triggered reply] --> B[Bounded request queue] B --> R[Three-attempt retry budget] R --> K[Reusable keep-alive pool] K --> Q[QQ API] S[Adapter shutdown] --> C[Cancel owned requests] C --> X[Close pool permanently]Changed files: 11 total (2 added, 9 modified, 0 deleted).
Screenshots or Test Results / 运行截图或测试结果
Verification steps:
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
Bound QQ Official outbound traffic with a shared reusable transport, coordinated retries, fail-closed shutdown, and safer error logging.
New Features:
Bug Fixes:
Enhancements:
Documentation:
Tests: