Skip to content

fix(gateway): tear down cached agents on stop (salvage #12917) - #16207

Merged
teknium1 merged 1 commit into
mainfrom
hermes/hermes-d7874f79
Apr 26, 2026
Merged

fix(gateway): tear down cached agents on stop (salvage #12917)#16207
teknium1 merged 1 commit into
mainfrom
hermes/hermes-d7874f79

Conversation

@teknium1

Copy link
Copy Markdown
Contributor

Gateway shutdown now shuts down memory providers on idle cached agents, not just mid-turn ones. Closes leak of Hindsight/Honcho/Mnemoria clients + terminal sandboxes + browser daemons at systemd stop.

Salvage of #12917 by @Tranquil-Flow. Single contributor commit cherry-picked onto current main; author attribution preserved via rebase merge.

Root cause

_drain_active_agents snapshots only _running_agents. Agents that had ended their turn and were living in _agent_cache (warm cache / idle TTL sweep pool) never received shutdown_memory_provider() or close(), so their memory-provider network clients, terminal sandboxes, and browser daemons stayed alive until process exit.

Changes

  • gateway/run.py _stop_impl: after _finalize_shutdown_agents(active_agents), sweep _agent_cache under _agent_cache_lock, then call _cleanup_agent_resources on each entry (handles tuple and bare-agent forms).
  • tests/gateway/test_shutdown_cache_cleanup.py: 8 new tests covering memory-provider shutdown ordering, cache-cleared-after, exception resilience, multiple agents, plain/tuple/None entries, and the running+cached overlap case.

Validation

Before After
Shutdown path iterates _running_agents only _running_agents + _agent_cache
Targeted tests 8/8 pass (1.1s)

Closes #12917 (original PR, merged via salvage). Addresses #11205.

@teknium1
teknium1 merged commit bf05b8f into main Apr 26, 2026
10 of 11 checks passed
@teknium1
teknium1 deleted the hermes/hermes-d7874f79 branch April 26, 2026 19:51
@alt-glitch alt-glitch added type/bug Something isn't working P2 Medium — degraded but workaround exists comp/gateway Gateway runner, session dispatch, delivery labels Apr 26, 2026
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 type/bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants