Skip to content

The agent that grows with you

perf(tui-gateway): session.activate builds its payload outside _session_resume_lock

kshitijk4poorc6cb7111a24f
diff --git a/tui_gateway/methods_session.py b/tui_gateway/methods_session.py
index 9190987f883..dfc21ff6f28 100644
--- a/tui_gateway/methods_session.py
+++ b/tui_gateway/methods_session.py
@@ -893,12 +893,15 @@ def _(rid, params: dict) -> dict:
def _(rid, params: dict, session: dict) -> dict:
"""Attach the frontend to a live TUI session without closing the previously focused one."""
sid = str(params.get("session_id") or "")
+ # Only the rebind is atomic with grace expiry; the payload (a DB history read unless
+ # ``omit_messages``) must not hold the process-wide resume lock.
with _session_resume_lock:
if (refusal := _reattach_refusal(rid, sid, session)) is not None:
return refusal
- return _ok(rid, _live_session_payload(
- sid, session, touch=True, transport=current_transport() or _stdio_transport,
- omit_messages=is_truthy_value(params.get("omit_messages", False))))
+ with session["history_lock"]:
+ _rebind_live_transport(sid, session, current_transport() or _stdio_transport)
+ return _ok(rid, _live_session_payload(
+ sid, session, touch=True, omit_messages=is_truthy_value(params.get("omit_messages", False))))
@method("session.delete")
diff --git a/tui_gateway/server.py b/tui_gateway/server.py
index 93c8a0a0b38..82e3c20d6fb 100644
--- a/tui_gateway/server.py
+++ b/tui_gateway/server.py
@@ -2686,6 +2686,17 @@ def _live_visible_history(session: dict, db, in_memory_fallback: list[dict]) ->
return in_memory_fallback
+def _rebind_live_transport(sid: str, session: dict, transport: Transport) -> None:
+ """Point a live session at ``transport`` (caller holds ``history_lock``)."""
+ session["transport"] = transport
+ # Every transport that showed this session (pop-outs resume the same sid); on disconnect the last
+ # viewer becomes the transport instead of the drop sentinel.
+ session.setdefault("viewers", {})[transport] = time.time()
+ # See #83716.
+ if transport is not _detached_ws_transport:
+ _cancel_ws_orphan_reap(sid) # the client is back — a pending ws-orphan reap must not fire
+
+
def _live_session_payload(
sid: str, session: dict, *, cols: int | None = None, touch: bool = False,
transport: Transport | None = None, omit_messages: bool = False) -> dict:
@@ -2693,13 +2704,7 @@ def _live_session_payload(
if cols is not None:
session["cols"] = cols
if transport is not None:
- session["transport"] = transport
- # Every transport that showed this session (pop-outs resume the same sid); on disconnect the last
- # viewer becomes the transport instead of the drop sentinel.
- session.setdefault("viewers", {})[transport] = time.time()
- # See #83716.
- if transport is not _detached_ws_transport:
- _cancel_ws_orphan_reap(sid) # the client is back — a pending ws-orphan reap must not fire
+ _rebind_live_transport(sid, session, transport)
if touch:
# #84417: do not re-fire the live turn's original user text from a stale server-queue
# self-duplicate after settle.