The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(gateway): unserve releases the profile's gateway.lock last
Tekniumd5cc629396fe
diff --git a/gateway/run_profile_reconcile.py b/gateway/run_profile_reconcile.pyindex fb0baa9f45f..a4a4a66a919 100644--- a/gateway/run_profile_reconcile.py+++ b/gateway/run_profile_reconcile.py@@ -371,32 +371,34 @@ class GatewayProfileReconcileMixin:# this process exits. Raising here aborted the reconcile for every other profile.logger.error("[MULTIPLEX] Profile '%s' unrouted, but a turn outlived its Stop; ownership retained", name)return- # Its session authority and reservation go before the store handles: the authority owns the- # state.db writer, and the next restart must not try to reserve a home that no longer exists.- release_profile_home(self, home)- with _log_suppressed(logging.DEBUG, "profile handle release failed", exc_info=True):- from hermes_state_registry import close_all_under- close_all_under(home)- with _log_suppressed(logging.DEBUG, "memory-store release failed", exc_info=True):- from plugins.memory import import_provider_module # catalog plugin since it left core- import_provider_module("holographic", "store").MemoryStore.release_all_under(home)- # The gateway process is a second holder the CLI-side delete cannot reach: its routed- # log files (logs/.__agent.lock, logs/.__errors.lock) and its scoped MCP servers'- # mcp-stderr.log handle live HERE, so unserve must release them in this process too- # (#130244) — the same two calls ``delete_profile`` makes for the deleting process.- # Both calls BLOCK (the log release stops/joins the QueueListener, which can be inside- # a ConcurrentRotatingFileHandler emit waiting on another process's rotation lock; the- # MCP shutdown waits on future.result for the MCP loop), so they run off the loop in a- # worker thread — otherwise every other served profile's events stall behind them.- # ``asyncio.to_thread`` copies the context, so the surrounding- # ``_profile_runtime_scope`` home/secret override stays visible in the worker.- with _log_suppressed(logging.DEBUG, "scoped MCP shutdown failed", exc_info=True):- from hermes_constants import hermes_home_key- from tools.mcp_tool_lifecycle import shutdown_mcp_servers- await asyncio.to_thread(shutdown_mcp_servers, scope=hermes_home_key(home))- with _log_suppressed(logging.DEBUG, "profile log handler release failed", exc_info=True):- from hermes_logging import release_profile_log_handlers- await asyncio.to_thread(release_profile_log_handlers, home)+ # The reservation (gateway.lock) is released LAST: a deleter waiting on it may rmtree the+ # home as soon as it goes, so every handle this process holds there must close first.+ try:+ with _log_suppressed(logging.DEBUG, "profile handle release failed", exc_info=True):+ from hermes_state_registry import close_all_under+ close_all_under(home)+ with _log_suppressed(logging.DEBUG, "memory-store release failed", exc_info=True):+ from plugins.memory import import_provider_module # catalog plugin since it left core+ import_provider_module("holographic", "store").MemoryStore.release_all_under(home)+ # The gateway process is a second holder the CLI-side delete cannot reach: its routed+ # log files (logs/.__agent.lock, logs/.__errors.lock) and its scoped MCP servers'+ # mcp-stderr.log handle live HERE, so unserve must release them in this process too+ # (#130244) — the same two calls ``delete_profile`` makes for the deleting process.+ # Both calls BLOCK (the log release stops/joins the QueueListener, which can be inside+ # a ConcurrentRotatingFileHandler emit waiting on another process's rotation lock; the+ # MCP shutdown waits on future.result for the MCP loop), so they run off the loop in a+ # worker thread — otherwise every other served profile's events stall behind them.+ # ``asyncio.to_thread`` copies the context, so the surrounding+ # ``_profile_runtime_scope`` home/secret override stays visible in the worker.+ with _log_suppressed(logging.DEBUG, "scoped MCP shutdown failed", exc_info=True):+ from hermes_constants import hermes_home_key+ from tools.mcp_tool_lifecycle import shutdown_mcp_servers+ await asyncio.to_thread(shutdown_mcp_servers, scope=hermes_home_key(home))+ with _log_suppressed(logging.DEBUG, "profile log handler release failed", exc_info=True):+ from hermes_logging import release_profile_log_handlers+ await asyncio.to_thread(release_profile_log_handlers, home)+ finally:+ release_profile_home(self, home)logger.info("[MULTIPLEX] Profile '%s' unserved — %d adapter(s) stopped and unrouted", name, len(adapters))diff --git a/tests/gateway/test_unserve_release_order.py b/tests/gateway/test_unserve_release_order.pynew file mode 100644index 00000000000..bf091b54e0d--- /dev/null+++ b/tests/gateway/test_unserve_release_order.py@@ -0,0 +1,35 @@+"""Unserve releases a profile's gateway.lock only after its handles in that home close (N97)."""+from types import SimpleNamespace++import pytest+++async def test_unserve_holds_the_profile_lock_until_its_handles_close(tmp_path, monkeypatch):+ from gateway.run_profile_reconcile import GatewayProfileReconcileMixin+ from gateway.runtime_ownership import OwnershipConflict, exclusive_maintenance, process_ownership++ home = tmp_path / 'gone'+ process_ownership.reserve([home])+ deleter_could_start = []++ def close_all_under(directory):+ # A `hermes profile delete` waiting on the lock rmtree's the home the moment it wins.+ try:+ with exclusive_maintenance([home]):+ deleter_could_start.append(True)+ except OwnershipConflict:+ deleter_could_start.append(False)+ return 0++ monkeypatch.setattr('hermes_state_registry.close_all_under', close_all_under)+ runner = SimpleNamespace(+ _profile_failed_platforms={}, _profile_adapters={}, _served_profile_homes={'gone': home},+ _served_profile_signatures={}, _agent_cache={}, _evict_cached_agent=lambda key: None,+ config=SimpleNamespace(_runtime_profile_homes=(('gone', str(home)),)))+ try:+ await GatewayProfileReconcileMixin._unserve_profile(runner, 'gone', home)+ assert deleter_could_start == [False], 'gateway.lock released while store handles were open'+ assert not process_ownership.owns(home)+ finally:+ process_ownership.release(home)