Skip to content

The agent that grows with you

fix(gateway): unserve keeps a stuck profile's ownership without aborting reconcile

Tekniumee7c2597d610
diff --git a/gateway/run_profile_reconcile.py b/gateway/run_profile_reconcile.py
index 9de5da59033..6b37f30c170 100644
--- a/gateway/run_profile_reconcile.py
+++ b/gateway/run_profile_reconcile.py
@@ -329,9 +329,8 @@ class GatewayProfileReconcileMixin:
"""
from gateway.run import _profile_runtime_scope, _write_runtime_status_quiet
from gateway.run_runtime import release_profile_home, unserve_profile_runtime
- if getattr(self, 'session_authorities', None) is not None:
- if not await unserve_profile_runtime(self, home):
- raise TimeoutError('Profile workers did not stop; ownership retained')
+ # Retire before teardown; a False verdict (writer outlived its Stop) is honoured below.
+ retired = getattr(self, 'session_authorities', None) is None or await unserve_profile_runtime(self, home)
pending = (getattr(self, "_profile_failed_platforms", None) or {}).pop(name, None) or {}
tasks = [t for t in pending.values() if isinstance(t, asyncio.Task) and not t.done()]
for task in tasks:
@@ -359,6 +358,12 @@ class GatewayProfileReconcileMixin:
for key in [k for k in list(cache or {}) if str(k).startswith(prefix)]:
with _log_suppressed(logging.DEBUG, "agent eviction failed for %s", key, exc_info=True):
self._evict_cached_agent(key)
+ if not retired:
+ # A writer outlived its Stop and may still write this home: the profile leaves the
+ # served set (ingress is gone above) but keeps its reservation and store handles until
+ # 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)
diff --git a/tests/gateway/test_runtime_worker_shutdown.py b/tests/gateway/test_runtime_worker_shutdown.py
index c593c48d967..a99929787a1 100644
--- a/tests/gateway/test_runtime_worker_shutdown.py
+++ b/tests/gateway/test_runtime_worker_shutdown.py
@@ -112,3 +112,28 @@ async def test_whole_runtime_settle_logs_late_work_and_finishes_shutdown(monkeyp
finally:
late.set()
await asyncio.gather(task, return_exceptions=True)
+
+
+async def test_unserve_keeps_ownership_without_aborting_the_reconcile(tmp_path, monkeypatch):
+ """A profile whose writer outlived its Stop is unrouted but keeps its reservation and store
+ handles; the reconcile is not aborted, so the other removed profile is still unserved."""
+ from gateway.run_profile_reconcile import GatewayProfileReconcileMixin
+ stuck, gone = tmp_path / 'stuck', tmp_path / 'gone'
+ async def unserve(runner, home):
+ return home != stuck
+ released, closed = [], []
+ monkeypatch.setattr(run_runtime, 'unserve_profile_runtime', unserve)
+ monkeypatch.setattr(run_runtime, 'release_profile_home', lambda runner, home: released.append(home))
+ monkeypatch.setattr('hermes_state_registry.close_all_under', lambda home: closed.append(home))
+ runner = SimpleNamespace(session_authorities=object(), _profile_failed_platforms={}, _profile_adapters={},
+ _served_profile_homes={'stuck': stuck, 'gone': gone}, _served_profile_signatures={}, _agent_cache={},
+ _evict_cached_agent=lambda key: None, served_profile_names=lambda: sorted(runner._served_profile_homes),
+ _live_resource_claims=lambda active: {}, _record_served_profiles=lambda active, homes: None)
+ monkeypatch.setattr('hermes_cli.profiles.profiles_to_serve', lambda **kwargs: [])
+ runner._unserve_profile = GatewayProfileReconcileMixin._unserve_profile.__get__(runner)
+ result = await GatewayProfileReconcileMixin._apply_profile_changes(
+ runner, {}, [], ['stuck', 'gone'], [], reason='watcher')
+ assert result['removed'] == ['stuck', 'gone']
+ assert released == [gone] and closed == [gone], 'ownership of the stuck profile was released'
+ assert runner._served_profile_homes == {}