The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(tui_gateway): stop the previous notification poller on agent rebuild
ranggaoscar652e4b9194b5
diff --git a/tests/tui_gateway/test_tui_gateway_server.py b/tests/tui_gateway/test_tui_gateway_server.pyindex ae3b3d01329..d04edd4e8ad 100644--- a/tests/tui_gateway/test_tui_gateway_server.py+++ b/tests/tui_gateway/test_tui_gateway_server.py@@ -4917,6 +4917,52 @@ def _session(agent=None, **extra):}+def test_session_rebuild_stops_previous_notification_poller(monkeypatch):+ """Rebuilding the agent for a live session re-enters _start_session_services; the previous+ poller's stop event must be set before the record's ``_notif_stop`` is replaced (#138806).+ Otherwise the orphaned poller keeps draining the process-global completion queue for the+ session (two live loops per rebuild) until the session itself is finalized — close can only+ signal the newest event."""+ monkeypatch.setattr(server, "_notify_session_boundary", lambda *a, **k: None)+ sess = _session()+ server._sessions["sid_rebuild"] = sess+ try:+ server._start_session_services("sid_rebuild", "session-key", sess)+ first_stop = sess["_notif_stop"]+ assert not first_stop.is_set() # a fresh session really starts its poller running++ # Agent rebuild for the SAME session (in-session /model switch, resume, late MCP refresh).+ server._start_session_services("sid_rebuild", "session-key", sess)+ second_stop = sess["_notif_stop"]+ assert second_stop is not first_stop+ assert first_stop.is_set() # the previous poller was stopped, not orphaned++ # The old loop wakes at most every 0.5s; give it its real deadline to exit.+ first_thread = next(+ thread+ for _stop, thread in list(server._notification_pollers)+ if thread.name == "tui-notif-poller-sid_rebuild")+ deadline = time.time() + 3.0+ while first_thread.is_alive() and time.time() < deadline:+ time.sleep(0.05)+ assert not first_thread.is_alive(), "the stopped previous poller never exited"+ live = [+ thread+ for _stop, thread in list(server._notification_pollers)+ if thread.name == "tui-notif-poller-sid_rebuild" and thread.is_alive()+ ]+ assert len(live) == 1, f"expected exactly one live poller, got {len(live)}"++ # Session close can only signal the newest stop event — which now stops the only live poller.+ second_stop.set()+ live[0].join(timeout=3.0)+ assert not live[0].is_alive()+ finally:+ for stop, thread in list(server._notification_pollers):+ stop.set()+ thread.join(timeout=1.0)++def test_session_close_commits_memory_and_fires_finalize_hook(monkeypatch):calls = {"hooks": []}diff --git a/tui_gateway/session_registry.py b/tui_gateway/session_registry.pyindex 538d5b6691f..46f7752b855 100644--- a/tui_gateway/session_registry.py+++ b/tui_gateway/session_registry.py@@ -134,9 +134,14 @@ def _wire_session_agent(sid: str, key: str, agent) -> bool:def _start_session_services(sid: str, key: str, current: dict) -> None:- """Start the notification poller and fire the session-reset boundary hook."""+ """Start the notification poller (exactly one live per session) and fire the session-reset boundary hook."""with _sessions_lock:if (rec := _sessions.get(sid)) is not None:+ # An agent rebuild re-enters here for the SAME session: stop the previous poller before+ # replacing its stop event, or the orphaned old loop keeps draining the process-global+ # completion queue (and close can only signal the newest event) — #138806.+ if (old_stop := rec.get("_notif_stop")) is not None:+ old_stop.set()rec["_notif_stop"] = _start_notification_poller(sid, rec)_notify_session_boundary("on_session_reset", key, _session_source(current))