Skip to content

The agent that grows with you

fix(gateway): evict torn-down SessionDB handles from every gateway per-home cache (#123618, salvage #123619)

teknium145610ca78ce5
diff --git a/gateway/platforms/api_server.py b/gateway/platforms/api_server.py
index f42381067bc..d89c44006f7 100644
--- a/gateway/platforms/api_server.py
+++ b/gateway/platforms/api_server.py
@@ -1864,12 +1864,23 @@ class APIServerAdapter(OpenAICompatRoutesMixin, BasePlatformAdapter):
with self._session_db_cache_lock:
if self._session_db_cache_closed:
return None
- db = self._session_dbs.get(key)
+ db = self._cached_session_db_locked(key)
if db is None:
db = acquire(home / "state.db")
self._session_dbs[key] = db
return db
+ def _cached_session_db_locked(self, key: str) -> Optional[Any]:
+ """Caller holds ``_session_db_cache_lock``. A profile unserve/delete tears the home's
+ generation down through ``hermes_state_registry.close_all_under`` (clearing
+ ``_shared_registry_owned``) without telling this cache; serving that handle would keep
+ raising ``StateDbReplacedError`` after a recreate, so drop it and let the caller reopen."""
+ db = self._session_dbs.get(key)
+ if db is not None and getattr(db, "_shared_registry_owned", True) is False:
+ del self._session_dbs[key]
+ return None
+ return db
+
def _close_cached_session_dbs(self) -> None:
"""Close SessionDB handles owned by this adapter's profile cache."""
with self._session_db_cache_lock:
@@ -1908,14 +1919,14 @@ class APIServerAdapter(OpenAICompatRoutesMixin, BasePlatformAdapter):
home = get_hermes_home()
key = str(home)
with self._session_db_cache_lock:
- cached = self._session_dbs.get(key)
+ cached = self._cached_session_db_locked(key)
if cached is not None:
return cached
if self._session_db_lock is None:
self._session_db_lock = asyncio.Lock()
async with self._session_db_lock:
with self._session_db_cache_lock:
- cached = self._session_dbs.get(key)
+ cached = self._cached_session_db_locked(key)
if cached is not None:
return cached
return await asyncio.to_thread(self._open_and_cache_session_db, home)
diff --git a/tests/gateway/test_session_db_handle_sharing.py b/tests/gateway/test_session_db_handle_sharing.py
index 59c5cb1b19f..6784f0edf98 100644
--- a/tests/gateway/test_session_db_handle_sharing.py
+++ b/tests/gateway/test_session_db_handle_sharing.py
@@ -161,21 +161,22 @@ def test_unavailable_store_handle_does_not_resurrect_a_second_open(store):
def test_a_handle_the_registry_tore_down_is_reopened_through_the_registry(store, home):
"""Profile unserve and delete force-close the profile's generation (``close_all_under``).
- The store's cache kept serving that dead object. Its self-heal then reopened a writer the
- registry does not know about, so the agent's ``acquire`` got a second writer on the same file,
- and after a delete + recreate every call raised ``StateDbReplacedError`` until restart.
+ Both gateway caches kept serving that dead object: the store's SessionDB and the runner's
+ async wrapper around it. The store's self-heal then reopened a writer the registry does not
+ know about, so the agent's ``acquire`` got a second writer on the same file, and after a
+ delete + recreate every call raised ``StateDbReplacedError`` until restart.
"""
import hermes_state_registry as registry
- profile = home / "profiles" / "work"
- profile.mkdir(parents=True)
- (profile / "config.yaml").write_text("{}\n", encoding="utf-8")
- path = profile / "state.db"
- first = store._open_session_db_for_active_scope(db_path=path)
+ runner = _runner_with(store)
+ first = store._db
first.create_session("before-unserve", source="telegram")
- assert registry.close_all_under(profile) == 1
+ first_wrapper = runner._open_session_db_for_active_scope()
+ assert first_wrapper._db is first
+ path = Path(first.db_path)
+ assert registry.close_all_under(home) == 1
- second = store._open_session_db_for_active_scope(db_path=path)
+ second = store._db
assert second is not first
second.create_session("after-unserve", source="telegram")
@@ -185,19 +186,28 @@ def test_a_handle_the_registry_tore_down_is_reopened_through_the_registry(store,
assert acquired is second, "one file, one writer: the agent must share the store's handle"
finally:
registry.release(acquired)
+ second_wrapper = runner._open_session_db_for_active_scope()
+ assert second_wrapper is not first_wrapper and second_wrapper._db is second
-def test_the_runner_wrapper_follows_the_reopened_store_handle(store, home):
- """The runner caches an async wrapper around the store's handle; a torn-down inner handle must
- not keep being served through it."""
+def test_api_server_profile_cache_reopens_a_handle_the_registry_tore_down(home):
+ """The API adapter's per-home cache serves routed profiles under their runtime scope; the
+ same ``close_all_under`` must evict its entry too."""
import hermes_state_registry as registry
+ from gateway.platforms.api_server import APIServerAdapter
- runner = _runner_with(store)
- first = runner._open_session_db_for_active_scope()
- assert first is not None and first._db is store._db
- registry.close_all_under(home)
-
- second = runner._open_session_db_for_active_scope()
+ adapter = APIServerAdapter.__new__(APIServerAdapter)
+ adapter._session_dbs = {}
+ adapter._session_db_cache_lock = threading.Lock()
+ adapter._session_db_cache_closed = False
+ profile = home / "profiles" / "work"
+ profile.mkdir(parents=True)
+ first = adapter._open_and_cache_session_db(profile)
+ assert registry.close_all_under(profile) == 1
- assert second is not first
- assert second._db is store._db and second._db is not first._db
+ second = adapter._open_and_cache_session_db(profile)
+ try:
+ assert second is not first
+ second.create_session("after-unserve", source="api_server")
+ finally:
+ registry.close_all_under(profile)