Skip to content

Open-source AI teammates in Slack, Teams, Discord, Telegram and Mattermost. Self-host it, with any agent harness and any model.

Drain before releasing the lease on shutdown, so connected agents never read offline (#1028)

Abel Dantas91ab8cdf1237
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 84cd32898..45cb66b85 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -44,6 +44,14 @@ version of their own to them without also giving them a release of their own.
### [Unreleased]
+#### Fixed
+- **Agents no longer read offline while their replica shuts down.** With several
+ replicas, a stopping replica released its lease before its 5 s drain, so for
+ up to about 6 s the other replicas counted its connected agents as gone: `@role`
+ mentions did not reach the holder, rooms got false "offline" notices, and stop
+ and reset commands were refused. It now drains first and closes its sockets
+ as it releases.
+
### [0.31.0] - 2026-10-20
#### Added
diff --git a/core/switch_core/main.py b/core/switch_core/main.py
index 39b58fbbd..c065026ed 100644
--- a/core/switch_core/main.py
+++ b/core/switch_core/main.py
@@ -2104,14 +2104,18 @@ async def _shutdown(
# Before uvicorn closes the sockets, so the controllers' are recorded as
# closed by the server.
controllers.begin_shutdown()
- # Also before the sockets close, and not left to the lifespan's teardown:
+ # Serve through the drain still owning the work and the lease, so the
+ # agents and controllers connected here read online everywhere while the
+ # Service moves new requests to the other replicas.
+ if drain_seconds:
+ await asyncio.sleep(drain_seconds)
+ # Before the sockets close, and not left to the lifespan's teardown:
# uvicorn runs that only once every connection has closed, and a stream
# still open when the process is killed would mean it never runs. Every
# other process reads this one's machines offline from here, and has taken
- # over the consumers, bridges and connectors it owned.
+ # over the consumers, bridges and connectors it owned, so the sockets close
+ # straight after and their clients reconnect to a process that holds them.
await release()
- if drain_seconds:
- await asyncio.sleep(drain_seconds)
server.should_exit = True
await connector_lifecycle.stop_all()
await collab_lifecycle.stop_all()
diff --git a/core/tests/switch_core/test_main_teams_app.py b/core/tests/switch_core/test_main_teams_app.py
index 6de0f25af..bb83befa0 100644
--- a/core/tests/switch_core/test_main_teams_app.py
+++ b/core/tests/switch_core/test_main_teams_app.py
@@ -146,9 +146,13 @@ async def test_the_package_carries_the_environments_app_name() -> None:
async def test_shutdown_drains_before_it_closes_anything(
monkeypatch: pytest.MonkeyPatch,
) -> None:
- """Readiness fails and the lease is released before uvicorn stops
- accepting, and with several replicas the process keeps serving for the
- drain window while the Service moves its traffic elsewhere."""
+ """Readiness fails first, and with several replicas the process keeps
+ serving, still owning its work and its lease, for the drain window while
+ the Service moves its traffic elsewhere. Only then is the lease released,
+ and the sockets close right after it: once the lease reads stopped, every
+ other process counts this one's agent and controller connections as gone,
+ so a socket left open past it is an agent that reads offline while it is
+ still connected here."""
calls: list[str] = []
class _Stoppable:
@@ -204,7 +208,7 @@ async def test_shutdown_drains_before_it_closes_anything(
assert calls[:5] == [
"not ready",
"controllers told",
- "lease released",
"slept 5",
+ "lease released",
"sockets closing",
]