fix(gateway): clean up cached agents on shutdown (#11205) - #12917
Closed
Tranquil-Flow wants to merge 1 commit into
Closed
fix(gateway): clean up cached agents on shutdown (#11205)#12917Tranquil-Flow wants to merge 1 commit into
Tranquil-Flow wants to merge 1 commit into
Conversation
This was referenced Apr 23, 2026
Contributor
1 task
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 changed and why
On gateway shutdown,
_stop_impl()called_finalize_shutdown_agents()which only cleaned up agents actively mid-turn (from_drain_active_agents()). Idle agents sitting in_agent_cache(LRU cache) were never cleaned up, so theirMemoryProvider.on_session_end()hooks never fired — causing potential memory/resource leaks from providers that persist state on session end.Fix: Add an explicit cache sweep in
_stop_impl()after_finalize_shutdown_agents():_agent_cache_lock_cleanup_agent_resources()on each agentThe sweep is positioned before adapter disconnection so memory providers can still make network calls (e.g. persist memories) before adapters tear down. Lock is held only during snapshot+clear, not during slow cleanup iteration — matching the pattern in
_sweep_idle_cached_agents.Closes #11205
How to test
on_session_end()fires for the cached agent (check memory provider logs)Automated: 8 new tests in
tests/gateway/test_shutdown_cache_cleanup.py:shutdown_memory_providercalled on stopPlatform tested