Skip to content

The agent that grows with you

fix(memory): flush queued writes before a soft release shuts providers down

kshitijk4poor2df98238568c
diff --git a/agent/memory_manager.py b/agent/memory_manager.py
index 32ad72b63a4..729f0f5b594 100644
--- a/agent/memory_manager.py
+++ b/agent/memory_manager.py
@@ -806,6 +806,14 @@ class MemoryManager:
return True
return session_end and self._teardown_claim == "session_end"
+ def release_soft(self) -> None:
+ """Soft release (cache eviction, CLI agent rebuild): stop provider writer threads, which otherwise leak
+ one per evicted agent, WITHOUT on_session_end(): sweeps commit first and a pending session end owns
+ both. Flush first, since shutdown_all()'s short drain would cancel writes the old worker finished."""
+ if self.claim_teardown(session_end=False):
+ self.flush_pending(timeout=10)
+ self.shutdown_all()
+
def shutdown_all(self) -> None:
"""Drain the background executor (bounded), then shut providers down in reverse order."""
self._drain_sync_executor()
diff --git a/run_agent.py b/run_agent.py
index b001265d9d6..86fa471ed1a 100644
--- a/run_agent.py
+++ b/run_agent.py
@@ -874,7 +874,6 @@ class AIAgent(
if getattr(self, "_memory_provider_shutdown", False):
return
self._memory_provider_shutdown = True
- # Shared with release_clients() via the manager's once-only claim (see claim_teardown).
if self._memory_manager and self._memory_manager.claim_teardown(session_end=True):
try:
self._memory_manager.on_session_end(messages or [])
@@ -924,10 +923,9 @@ class AIAgent(
pass
def release_clients(self) -> None:
- """Release LLM clients and child agents WITHOUT tearing down session tool state (gateway cache
- eviction: the session may resume on the same task_id, so processes, sandbox, browser and
- computer-use are kept). The memory manager is drained + shut down WITHOUT ``on_session_end()``
- so evicted provider writer threads don't leak. Idempotent; distinct from ``close()``."""
+ """Release LLM clients, child agents and memory providers WITHOUT tearing down session tool state (gateway
+ cache eviction: the session may resume on the same task_id, so processes, sandbox, browser and computer-use
+ are kept). Idempotent; distinct from ``close()``."""
self._close_active_children(soft=True)
# Retire (don't hard-close) the shared client: eviction runs on the gateway memory-manager thread,
# and a cross-thread close can release TLS FDs under a still-unwinding worker.
@@ -936,18 +934,12 @@ class AIAgent(
# The Codex app-server child is an LLM client, not session tool state: the evicted instance is popped
# from the cache and a rebuilt agent spawns its own child, so an unclosed one leaks for the gateway's life.
_quietly(self._close_codex_session)
- # Stop the memory providers' writer threads (one leaked per evicted agent otherwise), but skip
- # on_session_end(): sweep evictions already committed it via _commit_then_release_soft(), and a
- # pending end-of-session teardown (claim_teardown) owns both extraction and shutdown.
- _mm = getattr(self, "_memory_manager", None)
- if _mm is not None and _mm.claim_teardown(session_end=False):
- _quietly(_mm.shutdown_all)
+ if getattr(self, "_memory_manager", None) is not None:
+ _quietly(self._memory_manager.release_soft)
def close(self) -> None:
"""Release every resource this agent holds (idempotent); each phase is guarded so one failure never
blocks the rest."""
- # close() is the hard owner boundary; shutdown_memory_provider() is idempotent so gateway pre-calls
- # never double-extract.
session_messages = getattr(self, "_session_messages", None)
_quietly(self.shutdown_memory_provider, session_messages if isinstance(session_messages, list) else None)
self._close_task_resources(getattr(self, "session_id", None) or "")
diff --git a/tests/gateway/test_agent_cache_memory_lifecycle.py b/tests/gateway/test_agent_cache_memory_lifecycle.py
index e4e3a2f3132..adfeb440788 100644
--- a/tests/gateway/test_agent_cache_memory_lifecycle.py
+++ b/tests/gateway/test_agent_cache_memory_lifecycle.py
@@ -71,6 +71,30 @@ class TestAgentCacheMemoryLifecycle:
agent.close() # a later hard close neither re-extracts nor shuts down twice
assert events == ["shutdown"]
+ def test_release_clients_lets_queued_writes_finish_before_shutdown(self, monkeypatch):
+ """Soft release must not cancel the retired agent's queued memory writes: on main the worker
+ finished them in the background, and shutdown_all()'s bounded drain alone would abandon them."""
+ import agent.memory_manager as mm
+ from tests.waiting import wait_until
+
+ monkeypatch.setattr(mm, "_SYNC_DRAIN_TIMEOUT_S", 0.0)
+ events = []
+ agent, provider = self._agent_with_writer_provider("mem-release-flush-test", events)
+ manager, gate, flushing = agent._memory_manager, threading.Event(), threading.Event()
+ provider.sync_turn = lambda user, assistant, **kw: (gate.wait(10), events.append(f"sync:{user}"))
+ real_flush = manager.flush_pending
+ manager.flush_pending = lambda timeout=None: (flushing.set(), real_flush(timeout))[1]
+ for turn in ("a", "b", "c"):
+ manager.sync_all(turn, "reply")
+ releaser = threading.Thread(target=agent.release_clients)
+ releaser.start()
+ try:
+ wait_until(lambda: flushing.is_set() or not releaser.is_alive(), what="the soft release to start")
+ finally:
+ gate.set()
+ releaser.join(timeout=10)
+ assert events == ["sync:a", "sync:b", "sync:c", "shutdown"]
+
def test_close_racing_an_in_progress_soft_release_does_not_reextract(self):
"""close() arriving while release_clients() is still inside the provider shutdown must not
start extraction or a second shutdown against the providers being torn down."""