fix(gateway): track background task references in GatewayRunner#3254
Merged
fix(gateway): track background task references in GatewayRunner#3254
Conversation
Asyncio tasks created with create_task() but never stored can be garbage collected mid-execution. Add self._background_tasks set to hold references, with add_done_callback cleanup. Tracks: - /background command task - session-reset memory flush task - session-resume memory flush task Cancel all pending tasks in stop(). Update test fixtures that construct GatewayRunner via object.__new__() to include the new _background_tasks attribute. Cherry-picked from PR #3167 by memosr. The original PR also deleted the DM topic auto-skill loading code — that deletion was excluded from this salvage as it removes a shipped feature (#2598).
23 tasks
StreamOfRon
pushed a commit
to StreamOfRon/hermes-agent
that referenced
this pull request
Mar 29, 2026
…Research#3254) Asyncio tasks created with create_task() but never stored can be garbage collected mid-execution. Add self._background_tasks set to hold references, with add_done_callback cleanup. Tracks: - /background command task - session-reset memory flush task - session-resume memory flush task Cancel all pending tasks in stop(). Update test fixtures that construct GatewayRunner via object.__new__() to include the new _background_tasks attribute. Cherry-picked from PR NousResearch#3167 by memosr. The original PR also deleted the DM topic auto-skill loading code — that deletion was excluded from this salvage as it removes a shipped feature (NousResearch#2598). Co-authored-by: memosr.eth <96793918+memosr@users.noreply.github.com>
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
Salvage of PR #3167 by @memosr, cherry-picked onto current main.
What this fixes
Fire-and-forget
asyncio.create_task()calls without stored references are vulnerable to garbage collection silently canceling the task mid-execution. This adds aself._background_tasksset toGatewayRunnerusing the standardadd_done_callback(discard)pattern.Tasks now tracked:
/backgroundcommand taskCleanup on shutdown:
stop()cancels all pending background tasks before clearing adapters.What was excluded from the original PR
The original PR also deleted ~30 lines of DM topic auto-skill loading code (shipped in #2598) without explanation. That deletion was excluded from this salvage.
Test fixes
Three test files construct
GatewayRunnerviaobject.__new__()bypassing__init__. Updated their fixtures to include the new_background_tasksattribute.Gateway suite: 1461 passed, 21 skipped.
Closes #3167