Skip to content

The agent that grows with you

fix(memory): close() after a soft release skips extraction and re-shutdown

kshitijk4poorefc8d91fb24b
diff --git a/run_agent.py b/run_agent.py
index 5367e84ae0a..0c6b5f10d1b 100644
--- a/run_agent.py
+++ b/run_agent.py
@@ -874,7 +874,9 @@ class AIAgent(
if getattr(self, "_memory_provider_shutdown", False):
return
self._memory_provider_shutdown = True
- if self._memory_manager:
+ # A soft release (release_clients) may already have shut the providers down; extracting
+ # against shut-down providers would only fail, and a second shutdown() is not guaranteed safe.
+ if self._memory_manager and not getattr(self, "_memory_providers_released", False):
try:
self._memory_manager.on_session_end(messages or [])
except Exception as e:
@@ -968,6 +970,7 @@ class AIAgent(
_mm = getattr(self, "_memory_manager", None)
if _mm is not None and not getattr(_mm, "_shutting_down", False):
_quietly(_mm.shutdown_all)
+ self._memory_providers_released = True
def close(self) -> None:
"""Release every resource this agent holds (idempotent); each phase is guarded so one failure never
diff --git a/tests/gateway/test_agent_cache.py b/tests/gateway/test_agent_cache.py
index 4930e3cb972..2068de489b8 100644
--- a/tests/gateway/test_agent_cache.py
+++ b/tests/gateway/test_agent_cache.py
@@ -670,6 +670,16 @@ class TestAgentCacheIdleResume:
finally:
agent.close()
+ def test_close_after_release_clients_does_not_reextract_or_reshutdown(self):
+ """A soft-released agent that is later close()d (e.g. a reset racing an eviction) must not run
+ end-of-session extraction against, or shut down a second time, providers already shut down."""
+ events = []
+ agent, provider = self._agent_with_writer_provider("mem-release-then-close-test", events)
+ agent.release_clients()
+ agent.close()
+ assert events == ["shutdown"]
+ assert not provider.writer.is_alive()
+
def test_gateway_soft_evict_commits_memory_before_provider_shutdown(self):
"""The sweep eviction path must run end-of-session extraction BEFORE release_clients()
shuts the providers down (a shut-down provider can no longer commit)."""