Skip to content

fix(gateway): clean up cached agents on shutdown (#11205) - #12917

Closed
Tranquil-Flow wants to merge 1 commit into
NousResearch:mainfrom
Tranquil-Flow:fix/memory-provider-session-end
Closed

fix(gateway): clean up cached agents on shutdown (#11205)#12917
Tranquil-Flow wants to merge 1 commit into
NousResearch:mainfrom
Tranquil-Flow:fix/memory-provider-session-end

Conversation

@Tranquil-Flow

Copy link
Copy Markdown
Contributor

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 their MemoryProvider.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():

  1. Acquire _agent_cache_lock
  2. Snapshot all entries and clear the cache
  3. Release lock
  4. Call _cleanup_agent_resources() on each agent

The 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

  1. Start gateway with a memory provider configured (e.g. Mnemoria)
  2. Chat with the agent so a session is cached
  3. Stop the gateway
  4. Verify on_session_end() fires for the cached agent (check memory provider logs)

Automated: 8 new tests in tests/gateway/test_shutdown_cache_cleanup.py:

  • Cached agent's shutdown_memory_provider called on stop
  • Cache cleared after shutdown
  • Empty cache — no error
  • Multiple cached agents all cleaned
  • Exception from one agent doesn't prevent cleanup of others
  • Non-tuple cache entries handled
  • None cache entries skipped
  • Double-presence (both running + cached) cleaned at least once

Platform tested

  • macOS (Darwin 24.6.0)

@alt-glitch alt-glitch added type/bug Something isn't working P2 Medium — degraded but workaround exists comp/gateway Gateway runner, session dispatch, delivery tool/memory Memory tool and memory providers labels Apr 22, 2026
@teknium1

Copy link
Copy Markdown
Contributor

Merged via salvage PR #16207 (commit bf05b8f). Your commit was rebase-merged onto current main with authorship preserved in git log. Thanks for the fix!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

comp/gateway Gateway runner, session dispatch, delivery P2 Medium — degraded but workaround exists tool/memory Memory tool and memory providers type/bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug]: MemoryProvider.on_session_end() never called on gateway session expiry

3 participants