Skip to content

The agent that grows with you

fix(tui_gateway): the control-publish lock is per session, so one stalled client can't delay others

Teknium6aca02a7be19
diff --git a/tests/tui_gateway/test_session_control.py b/tests/tui_gateway/test_session_control.py
index fd69b4a7d9c..08f597ccc42 100644
--- a/tests/tui_gateway/test_session_control.py
+++ b/tests/tui_gateway/test_session_control.py
@@ -421,6 +421,39 @@ class TestUpdatePublication:
if event == "session.control.update"]
assert updates[-1] == ["newer criterion"]
+ def test_a_stalled_client_write_never_delays_another_sessions_publish(self, server, session, monkeypatch):
+ """Ordering is per session: while one session's socket write stalls (up to the transport's write
+ timeout), an unrelated session's control update and tool completions still go out."""
+ sid, key, entry = session
+ _save_goal(key)
+ other_sid, other_key = f"sid-control-{uuid.uuid4().hex}", f"control-{uuid.uuid4().hex}"
+ other = {**entry, "session_key": other_key, "history_lock": threading.Lock()}
+ server._sessions[other_sid] = other
+ _save_goal(other_key)
+ stalled, release, emitted = threading.Event(), threading.Event(), []
+
+ def emit(event, event_sid, payload=None):
+ if event_sid == sid:
+ stalled.set()
+ release.wait(5)
+ emitted.append(event_sid)
+ monkeypatch.setattr(server, "_emit", emit)
+ slow = threading.Thread(target=server._publish_session_control_snapshot, args=(sid, entry))
+ slow.start()
+ try:
+ assert stalled.wait(5)
+ fast = threading.Thread(target=server._publish_session_control_snapshot, args=(other_sid, other))
+ fast.start()
+ fast.join(1)
+ assert not fast.is_alive(), "another session's stalled write blocked this publish"
+ assert emitted == [other_sid]
+ finally:
+ release.set()
+ slow.join(5)
+ server._sessions.pop(other_sid, None)
+ from hermes_cli.goals import GoalManager
+ GoalManager(other_key).clear()
+
@pytest.mark.parametrize("name,arg", [("goal", "pause"), ("loop", "pause")])
def test_typed_slash_via_command_dispatch_publishes_the_new_state(self, server, session, monkeypatch, name, arg):
"""/goal and /loop never reach the slash worker (``_PENDING_INPUT_COMMANDS``) — the built-in path must
diff --git a/tui_gateway/methods_session_control.py b/tui_gateway/methods_session_control.py
index 8f6a76aebae..456132b9c00 100644
--- a/tui_gateway/methods_session_control.py
+++ b/tui_gateway/methods_session_control.py
@@ -217,8 +217,17 @@ _SESSION_CONTROL_SLASHES = frozenset({"goal", "heartbeat", "loop", "subgoal"})
# Tool completions publish from the agent thread while control actions and slash commands publish from
# RPC threads: the snapshot read, the revision compare and the emit happen as one step, so an update read
-# before a user's change can never be emitted after it.
-_PUBLISH_LOCK = threading.Lock()
+# before a user's change can never be emitted after it. The lock is the SESSION's: a stalled socket write
+# (up to the transport's write timeout) only holds back that session's own later updates, never another's.
+_PUBLISH_LOCK_GUARD = threading.Lock()
+
+
+def _session_publish_lock(session: dict) -> threading.Lock:
+ lock = session.get("control_publish_lock")
+ if lock is None:
+ with _PUBLISH_LOCK_GUARD:
+ lock = session.setdefault("control_publish_lock", threading.Lock())
+ return lock
def _publish_session_control_snapshot(sid: str, session: dict | None, *, only_if_present: bool = False,
@@ -231,7 +240,7 @@ def _publish_session_control_snapshot(sid: str, session: dict | None, *, only_if
if not session or not (session_key := str(session.get("session_key") or "")):
return
try:
- with _PUBLISH_LOCK:
+ with _session_publish_lock(session):
with _session_profile_runtime_scope(session):
control = _snapshot_control(session_key)
if (only_if_present and not control["revision"]) or (