fix(weixin): track fire-and-forget tasks and offload blocking file read - #26049
Open
frank20190926 wants to merge 1 commit into
Open
fix(weixin): track fire-and-forget tasks and offload blocking file read#26049frank20190926 wants to merge 1 commit into
frank20190926 wants to merge 1 commit into
Conversation
Two async correctness issues in the Weixin adapter: 1. **Orphan tasks on disconnect** — _process_message_safe and _maybe_fetch_typing_ticket were spawned via bare asyncio.create_task() with no tracking. On disconnect only the poll loop was cancelled; child tasks continued running against closing sessions. Fix: introduce _child_tasks set + _track_task() helper. disconnect() now cancels all tracked children before closing sessions. Done callback auto-removes completed tasks from the set. 2. **Blocking I/O in the async path** — _send_file called Path(path).read_bytes() synchronously, blocking the event loop for large media files. Fix: use await asyncio.to_thread(Path(path).read_bytes) to offload the synchronous read to a thread pool.
Collaborator
|
Supersedes #11998 (same Weixin fire-and-forget task tracking fix). This PR is more complete — also adds disconnect cleanup of tracked tasks and offloads blocking file I/O in |
Contributor
|
Thanks for addressing two concrete async hazards in the Weixin adapter. The premise is still present on current main: bare child tasks are created at Problems
Suggested changes
The implementation is localized and matches the adapter's existing tracked poll/text-batch lifecycle. This is an automated hermes-sweeper review. |
13 tasks
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.
Bug Description
Two async correctness bugs in the Weixin adapter that could cause silent delivery failures and event loop blocking.
Root Cause
Orphan tasks on disconnect —
_process_message_safeand_maybe_fetch_typing_ticketwere spawned via bareasyncio.create_task()with no tracking. On disconnect, only the poll loop was cancelled; child tasks kept running against closing sessions.Blocking I/O in async path —
_send_filecalledPath(path).read_bytes()synchronously, blocking the event loop while reading large media files into memory.Fix
Introduced
_child_taskstracking set +_track_task()helper.disconnect()now cancels all tracked children before closing sessions. Done callbacks auto-remove completed tasks.Replaced sync
Path(path).read_bytes()withawait asyncio.to_thread(Path(path).read_bytes)to offload blocking file I/O to a thread pool.Test Plan
Risk Assessment
Low — both changes are localized to the Weixin adapter.