fix: store asyncio task references to prevent GC mid-execution#3267
Merged
fix: store asyncio task references to prevent GC mid-execution#3267
Conversation
Python's asyncio event loop holds only weak references to tasks. Without a strong reference, the garbage collector can destroy a task while it's awaiting I/O — silently dropping messages. Python 3.12+ made this more aggressive. Audit of all gateway platform adapters found 6 untracked create_task calls across 6 files: Per-message tasks (tracked via _background_tasks set from base class): - gateway/platforms/webhook.py: handle_message task - gateway/platforms/sms.py: handle_message task - gateway/platforms/signal.py: SSE response aclose task Long-running infrastructure tasks (stored in named instance vars): - gateway/platforms/slack.py: Socket Mode handler (_socket_mode_task) - gateway/platforms/discord.py: bot client (_bot_task) - gateway/platforms/whatsapp.py: message poll loop (_poll_task, 2 sites) All other adapters (telegram, mattermost, matrix, email, homeassistant, dingtalk) already tracked their tasks correctly. Salvaged from PR #3160 by memosr — expanded from 1 file to 6.
StreamOfRon
pushed a commit
to StreamOfRon/hermes-agent
that referenced
this pull request
Mar 29, 2026
…esearch#3267) Python's asyncio event loop holds only weak references to tasks. Without a strong reference, the garbage collector can destroy a task while it's awaiting I/O — silently dropping messages. Python 3.12+ made this more aggressive. Audit of all gateway platform adapters found 6 untracked create_task calls across 6 files: Per-message tasks (tracked via _background_tasks set from base class): - gateway/platforms/webhook.py: handle_message task - gateway/platforms/sms.py: handle_message task - gateway/platforms/signal.py: SSE response aclose task Long-running infrastructure tasks (stored in named instance vars): - gateway/platforms/slack.py: Socket Mode handler (_socket_mode_task) - gateway/platforms/discord.py: bot client (_bot_task) - gateway/platforms/whatsapp.py: message poll loop (_poll_task, 2 sites) All other adapters (telegram, mattermost, matrix, email, homeassistant, dingtalk) already tracked their tasks correctly. Salvaged from PR NousResearch#3160 by memosr — expanded from 1 file to 6.
teknium1
added a commit
that referenced
this pull request
Mar 29, 2026
Replace per-request aiohttp.ClientSession() in every WhatsApp adapter method with a single persistent self._http_session, matching the pattern used by Mattermost, HomeAssistant, and SMS adapters. Changes: - Create self._http_session in connect(), close in disconnect() - All bridge HTTP calls (send, edit, send-media, typing, get_chat_info, poll_messages) now use the shared session - Explicitly cancel _poll_task on disconnect() instead of relying solely on self._running = False - Health-check sessions in connect() remain ephemeral (persistent session not yet created at that point) - Remove per-method ImportError guards for aiohttp (always available when gateway runs via [messaging] extras) Salvaged from PR #1851 by Himess. The _poll_task storage was already on main from PR #3267; this adds the disconnect cancellation and the persistent session. Tests: 4 new tests for session close, already-closed skip, poll task cancellation, and done-task skip.
teknium1
added a commit
that referenced
this pull request
Mar 29, 2026
Replace per-request aiohttp.ClientSession() in every WhatsApp adapter method with a single persistent self._http_session, matching the pattern used by Mattermost, HomeAssistant, and SMS adapters. Changes: - Create self._http_session in connect(), close in disconnect() - All bridge HTTP calls (send, edit, send-media, typing, get_chat_info, poll_messages) now use the shared session - Explicitly cancel _poll_task on disconnect() instead of relying solely on self._running = False - Health-check sessions in connect() remain ephemeral (persistent session not yet created at that point) - Remove per-method ImportError guards for aiohttp (always available when gateway runs via [messaging] extras) Salvaged from PR #1851 by Himess. The _poll_task storage was already on main from PR #3267; this adds the disconnect cancellation and the persistent session. Tests: 4 new tests for session close, already-closed skip, poll task cancellation, and done-task skip.
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.
Summary
Python's asyncio event loop holds only weak references to tasks. Without a strong reference, the GC can destroy a task while it's awaiting I/O — silently dropping messages. Python 3.12+ made this more aggressive (docs, SO discussion).
Full audit of all 12 gateway platform adapters found 6 untracked
create_taskcalls across 6 files:Per-message tasks (tracked via
_background_tasksset from base class)handle_messagetaskhandle_messagetaskaclosetaskLong-running infrastructure tasks (stored in named instance vars)
self._socket_mode_taskself._bot_taskself._poll_task(2 call sites)All other adapters (telegram, mattermost, matrix, email, homeassistant, dingtalk) already tracked their tasks correctly.
Salvaged from #3160 by @memosr — expanded from 1 file to 6.
Test plan