Skip to content

The agent that grows with you

fix(memory): one teardown claim shared by session-end shutdown and soft release

kshitijk4poor859b999db558
diff --git a/run_agent.py b/run_agent.py
index a6c73d24925..79c3bf7ce9d 100644
--- a/run_agent.py
+++ b/run_agent.py
@@ -154,6 +154,21 @@ def _quietly(fn: Callable, *args, **kwargs) -> None:
pass
+_MEMORY_TEARDOWN_CLAIM_LOCK = threading.Lock()
+
+
+def _claim_memory_teardown(agent: Any) -> bool:
+ """Once-only claim on an agent's memory-provider teardown, shared by end-of-session shutdown
+ and soft release. A /new cleanup worker left running past its timeout can still be inside
+ ``on_session_end()`` when the same agent is evicted; the loser skips, so extraction and
+ ``shutdown_all()`` never run concurrently or twice."""
+ with _MEMORY_TEARDOWN_CLAIM_LOCK:
+ if getattr(agent, "_memory_teardown_claimed", False):
+ return False
+ agent._memory_teardown_claimed = True
+ return True
+
+
def _call_engine_hook(engine: Any, hook: str, *args, **kwargs) -> None:
"""Invoke an optional context-engine lifecycle hook; failures are logged, never raised."""
if not hasattr(engine, hook):
@@ -874,9 +889,9 @@ class AIAgent(
if getattr(self, "_memory_provider_shutdown", False):
return
self._memory_provider_shutdown = True
- # 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):
+ # One claim shared with release_clients(): whichever path claims first owns extraction and
+ # shutdown_all(); the other skips, so they never overlap or shut providers down twice.
+ if self._memory_manager and _claim_memory_teardown(self):
try:
self._memory_manager.on_session_end(messages or [])
except Exception as e:
@@ -968,10 +983,11 @@ class AIAgent(
# are checked out exclusively, so a live shared manager is never reachable
# here; delegation/review children run skip_memory=True (manager is None).
_mm = getattr(self, "_memory_manager", None)
- if _mm is not None and not getattr(_mm, "_shutting_down", False):
- # Claimed before the (bounded, seconds-long) drain so a close() racing it skips the
- # extraction and second provider shutdown instead of running them mid-teardown.
- self._memory_providers_released = True
+ # Claimed before the (bounded, seconds-long) drain, and shared with shutdown_memory_provider():
+ # a /new cleanup still inside on_session_end() holds the claim, so this eviction leaves the
+ # shutdown to that worker instead of tearing the providers down under its extraction. A manager
+ # already draining (shut down by another owner) is left alone too.
+ if _mm is not None and not getattr(_mm, "_shutting_down", False) and _claim_memory_teardown(self):
_quietly(_mm.shutdown_all)
def close(self) -> None:
diff --git a/tests/gateway/test_agent_cache_memory_lifecycle.py b/tests/gateway/test_agent_cache_memory_lifecycle.py
index 09d2dec6cda..3f9c0d92428 100644
--- a/tests/gateway/test_agent_cache_memory_lifecycle.py
+++ b/tests/gateway/test_agent_cache_memory_lifecycle.py
@@ -3,6 +3,8 @@ end-of-session teardown (``shutdown_memory_provider`` / ``close``)."""
import threading
+import pytest
+
from tests.gateway.test_agent_cache import _make_runner
@@ -125,7 +127,7 @@ class TestAgentCacheMemoryLifecycle:
assert provider.writer.is_alive()
try:
agent.release_clients()
- agent.release_clients() # idempotent: guarded on the manager's _shutting_down
+ agent.release_clients() # idempotent: the teardown claim is once-only
assert not provider.writer.is_alive()
assert events == ["shutdown"]
finally:
@@ -177,3 +179,42 @@ class TestAgentCacheMemoryLifecycle:
assert not provider.writer.is_alive()
finally:
agent.close()
+
+ @pytest.mark.asyncio
+ async def test_eviction_after_timed_out_reset_cleanup_leaves_shutdown_to_it(self, monkeypatch):
+ """/new gives up waiting on its cleanup worker after the timeout and then evicts the same agent.
+ The worker is still inside on_session_end(), so the eviction must not shut the providers
+ down under that extraction, and the providers must be shut down exactly once, after it."""
+ import gateway.slash_commands_session as session_cmds
+ from tests.waiting import wait_until
+
+ events = []
+ agent, provider = self._agent_with_writer_provider("mem-reset-evict-race-test", events)
+ in_session_end, let_finish = threading.Event(), threading.Event()
+
+ def slow_session_end(messages):
+ events.append("on_session_end:start")
+ in_session_end.set()
+ assert let_finish.wait(10)
+ events.append("on_session_end:done")
+
+ provider.on_session_end = slow_session_end
+ agent._session_messages = [{"role": "user", "content": "remember this"}]
+ key = "agent:main:telegram:dm:1"
+ runner = _make_runner()
+ runner._agent_cache[key] = (agent, "sig")
+ # Only a deadline for giving up on the worker: on_session_end blocks until let_finish, so
+ # /new always times out, however slow the runner.
+ monkeypatch.setattr(session_cmds, "_RESET_CLEANUP_TIMEOUT_S", 0.05)
+ try:
+ await runner._cleanup_old_agent_for_reset(key)
+ assert in_session_end.wait(10)
+ runner._evict_cached_agent(key)
+ wait_until(lambda: agent._session_messages == [], what="the soft eviction to finish")
+ assert events == ["on_session_end:start"], "eviction shut providers down mid-extraction"
+ assert provider.writer.is_alive()
+ finally:
+ let_finish.set()
+ runner._housekeeping_executor.shutdown(wait=True)
+ assert events == ["on_session_end:start", "on_session_end:done", "shutdown"]
+ assert not provider.writer.is_alive()