Skip to content

The agent that grows with you

fix(api): sweep overflowed run SSE transports

doniocode10963689dcbb
diff --git a/gateway/platforms/api_server_runs.py b/gateway/platforms/api_server_runs.py
index 1b8644562e1..680ae29aa77 100644
--- a/gateway/platforms/api_server_runs.py
+++ b/gateway/platforms/api_server_runs.py
@@ -526,7 +526,13 @@ def _retire_live_run(self, run_id: str) -> None:
def _drop_run_transport(self, run_id: str) -> None:
- _forget_run(self, run_id, self._run_streams, self._run_streams_created)
+ _forget_run(
+ self,
+ run_id,
+ self._run_streams,
+ self._run_streams_created,
+ self._run_stream_subscribers,
+ )
async def _resolve_live_session_id(self, session_id: str) -> str:
@@ -1277,7 +1283,13 @@ def _sweep_orphaned_runs_once(self, now: Optional[float] = None) -> None:
if now is None:
now = time.time()
for run_id, created_at in list(self._run_streams_created.items()):
- if now - created_at <= self._RUN_STREAM_TTL or run_id in self._run_stream_subscribers:
+ stream = self._run_streams.get(run_id)
+ has_live_subscribers = (
+ bool(stream.subscribers)
+ if isinstance(stream, _RunStream)
+ else run_id in self._run_stream_subscribers
+ )
+ if now - created_at <= self._RUN_STREAM_TTL or has_live_subscribers:
continue
logger.debug("[api_server] sweeping expired run transport %s", run_id)
task = self._active_run_tasks.get(run_id)
diff --git a/tests/gateway/test_api_server_runs.py b/tests/gateway/test_api_server_runs.py
index a22534bac2f..b90fc712761 100644
--- a/tests/gateway/test_api_server_runs.py
+++ b/tests/gateway/test_api_server_runs.py
@@ -887,6 +887,40 @@ def test_run_stream_bounds_replay_evicts_slow_clients_and_seals_terminal():
)
+def test_sweep_expires_overflowed_subscriber_but_retains_active_stream(adapter):
+ """The sweeper trusts live stream membership over stale handler bookkeeping."""
+ from gateway.platforms.api_server_runs import _RunStream
+
+ overflowed_run = "run_overflowed_subscriber"
+ overflowed_stream = _RunStream()
+ overflowed_subscriber, _ = overflowed_stream.attach()
+ active_run = "run_active_subscriber"
+ active_stream = _RunStream()
+ active_stream.attach()
+
+ for run_id, stream in (
+ (overflowed_run, overflowed_stream),
+ (active_run, active_stream),
+ ):
+ adapter._run_streams[run_id] = stream
+ adapter._run_streams_created[run_id] = 0
+ adapter._run_stream_subscribers.add(run_id)
+
+ for index in range(_RunStream.SUBSCRIBER_QUEUE_LIMIT + 1):
+ overflowed_stream.put_nowait(
+ {"event": "message.delta", "delta": str(index)}
+ )
+ assert overflowed_subscriber not in overflowed_stream.subscribers
+
+ adapter._sweep_orphaned_runs_once(adapter._RUN_STREAM_TTL + 1)
+
+ assert overflowed_run not in adapter._run_streams
+ assert overflowed_run not in adapter._run_streams_created
+ assert overflowed_run not in adapter._run_stream_subscribers
+ assert adapter._run_streams[active_run] is active_stream
+ assert active_run in adapter._run_stream_subscribers
+
+
@pytest.mark.asyncio
async def test_run_events_response_boundary_preserves_events_and_cleans_transport(adapter):
"""The response boundary neither loses terminal events nor leaks subscribers."""