Skip to content

Compress tool outputs, logs, files, and RAG chunks before they reach the LLM. 20% fewer tokens for coding agents, 60-95% fewer tokens for JSON, same answers. Library, proxy, MCP server.

fix(proxy): deduplicate active_relay_tasks decrement in WS session deregister

Adryan Eka Vandraf1a3982fe279
diff --git a/headroom/proxy/handlers/openai.py b/headroom/proxy/handlers/openai.py
index 8b96d0313..0cdbd9f27 100644
--- a/headroom/proxy/handlers/openai.py
+++ b/headroom/proxy/handlers/openai.py
@@ -2061,8 +2061,14 @@ class OpenAIHandlerMixin:
# the registry entry. ``deregister`` is idempotent, so a
# session that never registered is a no-op.
if ws_sessions is not None and session_handle is not None:
- released_tasks = len(session_handle.relay_tasks)
- ws_sessions.deregister(session_id, cause=termination_cause)
+ # Use deregister_and_count so the handle pop and the
+ # relay-task count are read atomically inside the
+ # registry. Capturing ``len(session_handle.relay_tasks)``
+ # separately before ``deregister`` would risk drift if
+ # the registry's bookkeeping ever changes.
+ _deregistered, released_tasks = ws_sessions.deregister_and_count(
+ session_id, cause=termination_cause
+ )
session_duration_ms = (
time.perf_counter() - session_started_at
) * 1000.0
diff --git a/headroom/proxy/ws_session_registry.py b/headroom/proxy/ws_session_registry.py
index b12e88a32..cde4dc919 100644
--- a/headroom/proxy/ws_session_registry.py
+++ b/headroom/proxy/ws_session_registry.py
@@ -138,16 +138,45 @@ class WebSocketSessionRegistry:
Also clears the handle's ``relay_tasks`` list so the registry
stops holding references to coroutine frames after the session
ends.
+
+ The handle is returned with ``relay_tasks`` already cleared, but
+ the caller can still observe the number of tasks the registry
+ released via the ``released_tasks`` attribute set on the handle
+ before the clear. Prefer :meth:`deregister_and_count` for new
+ callers that need that number for Prometheus dec calls — it
+ couples the handle pop and the count read so they cannot drift.
"""
+ handle, _count = self._deregister_internal(session_id, cause)
+ return handle
+
+ def deregister_and_count(
+ self, session_id: str, cause: TerminationCause = "unknown"
+ ) -> tuple[WSSessionHandle | None, int]:
+ """Deregister and return (handle, released_task_count).
+
+ This is the preferred API for handlers that need to decrement a
+ Prometheus gauge by the number of relay tasks that were attached
+ to the session: capturing the count separately (via
+ ``len(handle.relay_tasks)`` before :meth:`deregister`) and then
+ calling :meth:`deregister` risks drift if the registry's
+ bookkeeping is ever changed. ``deregister_and_count`` returns
+ both atomically.
+
+ Returns ``(None, 0)`` when the session was not registered.
+ """
+ return self._deregister_internal(session_id, cause)
+
+ def _deregister_internal(
+ self, session_id: str, cause: TerminationCause
+ ) -> tuple[WSSessionHandle | None, int]:
handle = self._sessions.pop(session_id, None)
if handle is None:
- return None
+ return None, 0
handle.termination_cause = cause
- self._active_relay_tasks = max(
- 0, self._active_relay_tasks - len(handle.relay_tasks)
- )
+ released = len(handle.relay_tasks)
+ self._active_relay_tasks = max(0, self._active_relay_tasks - released)
handle.relay_tasks.clear()
- return handle
+ return handle, released
def attach_tasks(self, session_id: str, tasks: Iterable[_TaskLike]) -> None:
"""Attach relay tasks to an existing session (merge, not replace).