fix(qqbot): track inbound dispatch tasks so received messages aren't dropped - #41298
Open
Vesna-9 wants to merge 1 commit into
Open
fix(qqbot): track inbound dispatch tasks so received messages aren't dropped#41298Vesna-9 wants to merge 1 commit into
Vesna-9 wants to merge 1 commit into
Conversation
…dropped ## What does this PR do? The QQ Bot adapter spawned its inbound-message handler with a bare `asyncio.create_task(self._on_message(t, d))` in `_dispatch_payload`. The asyncio event loop only keeps a *weak* reference to a task created that way, so with no strong reference held anywhere the garbage collector is free to cancel the handler mid-flight — and when it does, the received QQ message is silently dropped with no error. Every other branch in `_dispatch_payload` (Resume, Identify, interaction, server-reconnect close) already routes through the adapter's `_create_task` helper; only the message branch did not. This routes the message branch through `_create_task` and makes that helper hold each task in the inherited `self._background_tasks` set until it finishes (discarding it via a done-callback), exactly as `BasePlatformAdapter` already does for its own spawned tasks. As a side effect it also fixes a test-safety gap: `_create_task` was created specifically so `_dispatch_payload` can be called synchronously outside `asyncio.run()` without raising "no running event loop", but the message branch bypassed it — so a message op was the one payload type that could not be dispatched the way the existing synchronous tests dispatch every other op. ## Related Issue N/A ## Type of Change - [x] 🐛 Bug fix (non-breaking change that fixes an issue) ## Changes Made - `gateway/platforms/qqbot/adapter.py`: route the inbound-message dispatch (`C2C_MESSAGE_CREATE`, `GROUP_AT_MESSAGE_CREATE`, `DIRECT_MESSAGE_CREATE`, `GUILD_MESSAGE_CREATE`, `GUILD_AT_MESSAGE_CREATE`) through `self._create_task` instead of a bare `asyncio.create_task`. - `gateway/platforms/qqbot/adapter.py`: change `_create_task` from a `@staticmethod` into an instance method that registers each spawned task in `self._background_tasks` and removes it again with an `add_done_callback` discard, so a still-running handler can't be garbage-collected. All call sites already invoked it as `self._create_task(...)`, so they are unchanged. - `tests/gateway/test_qqbot.py`: add two tests in `TestDispatchPayload` — `test_message_dispatch_without_event_loop_does_not_raise` (a message op dispatched synchronously must not raise) and `test_message_dispatch_task_is_tracked` (the spawned handler task is held in `_background_tasks` while it runs and discarded on completion). ## How to Test 1. Run the adapter test file: `pytest tests/gateway/test_qqbot.py -q` — 163 passed. 2. To confirm the tests catch the bug, revert just the dispatch line back to `asyncio.create_task(self._on_message(t, d))`: both new tests fail — `test_message_dispatch_without_event_loop_does_not_raise` raises `RuntimeError: no running event loop`, and `test_message_dispatch_task_is_tracked` asserts `len(_background_tasks) == 1` but finds `0` because the bare task was never tracked. ## Checklist ### Code - [x] I've read the Contributing Guide - [x] My commit messages follow Conventional Commits (`fix(scope):`, `feat(scope):`, etc.) - [x] I searched for existing PRs to make sure this isn't a duplicate - [x] My PR contains **only** changes related to this fix/feature (no unrelated commits) - [x] I've run `pytest tests/gateway/test_qqbot.py -q` and all tests pass - [x] I've added tests for my changes (required for bug fixes, strongly encouraged for features) - [x] I've tested on my platform: macOS 15 (Darwin 25.5.0) ### Documentation & Housekeeping - [x] I've updated relevant documentation (README, `docs/`, docstrings) — updated the `_create_task` docstring - [x] I've updated `cli-config.yaml.example` if I added/changed config keys — N/A - [x] I've updated `CONTRIBUTING.md` or `AGENTS.md` if I changed architecture or workflows — N/A - [x] I've considered cross-platform impact (Windows, macOS) per the compatibility guide — pure asyncio, no platform-specific code - [x] I've updated tool descriptions/schemas if I changed tool behavior — N/A
teknium1
reviewed
Jul 14, 2026
teknium1
left a comment
Contributor
There was a problem hiding this comment.
Thanks for isolating the untracked QQ inbound dispatch path. The current-main premise is valid: gateway/platforms/qqbot/adapter.py:857 still uses a bare task while adjacent branches use _create_task.
Problems
gateway/platforms/qqbot/adapter.py:810returns without closingcorowhen no loop exists. The changed message branch passes_on_message(...)at line 858, and_on_messageis async on current main (gateway/platforms/qqbot/adapter.py:932), leaving an unawaited coroutine in the synchronous path. The existing base implementation closes this case atgateway/platforms/base.py:3067-3074.
Suggested changes
- Close the coroutine before returning from the no-loop branch, and make
test_message_dispatch_without_event_loop_does_not_raiseexercise an async handler rather than a synchronousMock.
Automated hermes-sweeper review.
| loop = asyncio.get_running_loop() | ||
| return loop.create_task(coro) | ||
| except RuntimeError: | ||
| return None |
Contributor
There was a problem hiding this comment.
coro was already created by the caller (including the new _on_message(...) path). Close it before returning here; otherwise synchronous dispatch leaves an unawaited coroutine. BasePlatformAdapter uses this exact cleanup pattern at gateway/platforms/base.py:3067-3074.
19 tasks
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.
What does this PR do?
The QQ Bot adapter spawned its inbound-message handler with a bare
asyncio.create_task(self._on_message(t, d))in_dispatch_payload. Theasyncio event loop only keeps a weak reference to a task created that way, so
with no strong reference held anywhere the garbage collector is free to cancel
the handler mid-flight — and when it does, the received QQ message is silently
dropped with no error. Every other branch in
_dispatch_payload(Resume,Identify, interaction, server-reconnect close) already routes through the
adapter's
_create_taskhelper; only the message branch did not.This routes the message branch through
_create_taskand makes that helperhold each task in the inherited
self._background_tasksset until it finishes(discarding it via a done-callback), exactly as
BasePlatformAdapteralreadydoes for its own spawned tasks. As a side effect it also fixes a test-safety
gap:
_create_taskwas created specifically so_dispatch_payloadcan becalled synchronously outside
asyncio.run()without raising "no running eventloop", but the message branch bypassed it — so a message op was the one payload
type that could not be dispatched the way the existing synchronous tests
dispatch every other op.
Related Issue
N/A
Type of Change
Changes Made
gateway/platforms/qqbot/adapter.py: route the inbound-message dispatch(
C2C_MESSAGE_CREATE,GROUP_AT_MESSAGE_CREATE,DIRECT_MESSAGE_CREATE,GUILD_MESSAGE_CREATE,GUILD_AT_MESSAGE_CREATE) throughself._create_taskinstead of a bare
asyncio.create_task.gateway/platforms/qqbot/adapter.py: change_create_taskfrom a@staticmethodinto an instance method that registers each spawned task inself._background_tasksand removes it again with anadd_done_callbackdiscard, so a still-running handler can't be garbage-collected. All call
sites already invoked it as
self._create_task(...), so they are unchanged.tests/gateway/test_qqbot.py: add two tests inTestDispatchPayload—test_message_dispatch_without_event_loop_does_not_raise(a message opdispatched synchronously must not raise) and
test_message_dispatch_task_is_tracked(the spawned handler task is held in
_background_taskswhile it runs anddiscarded on completion).
How to Test
pytest tests/gateway/test_qqbot.py -q— 163 passed.asyncio.create_task(self._on_message(t, d)): both new tests fail —test_message_dispatch_without_event_loop_does_not_raiseraisesRuntimeError: no running event loop, andtest_message_dispatch_task_is_trackedasserts
len(_background_tasks) == 1but finds0because the bare task wasnever tracked.
Checklist
Code
fix(scope):,feat(scope):, etc.)pytest tests/gateway/test_qqbot.py -qand all tests passDocumentation & Housekeeping
docs/, docstrings) — updated the_create_taskdocstringcli-config.yaml.exampleif I added/changed config keys — N/ACONTRIBUTING.mdorAGENTS.mdif I changed architecture or workflows — N/A