Skip to content

The agent that grows with you

fix(api): preserve profile context for disconnect reapers

NanPan33a47679ba21
diff --git a/gateway/platforms/api_server.py b/gateway/platforms/api_server.py
index dc97d7f7912..b1ad438bc98 100644
--- a/gateway/platforms/api_server.py
+++ b/gateway/platforms/api_server.py
@@ -14,7 +14,7 @@ import hmac
import itertools
import json
from contextlib import contextmanager, nullcontext, suppress
-from contextvars import ContextVar
+from contextvars import ContextVar, copy_context
from functools import wraps
import logging
import os
@@ -580,7 +580,7 @@ def _reap_disconnected_agent_processes(
is_still_current = _epoch_still_current
from gateway.run import _reap_gateway_turn_processes
threading.Thread(
- target=_reap_gateway_turn_processes, args=(process_task_id, process_baseline),
+ target=copy_context().run, args=(_reap_gateway_turn_processes, process_task_id, process_baseline),
kwargs={"source": source, "is_still_current": is_still_current},
name=f"api-turn-reaper-{process_task_id[:12]}", daemon=True).start()
@@ -4006,7 +4006,7 @@ class APIServerAdapter(OpenAICompatRoutesMixin, BasePlatformAdapter):
return False
async def disconnect(self) -> None:
- """Stop the aiohttp server and release every owned resource, including the ResponseStore
+ """Stop the aiohttp web server and release every owned resource, including the ResponseStore
connection (the reconnect loop builds a fresh adapter per retry; leaked fds hit EMFILE).
Without this, every adapter instance leaks 2 file descriptors (the database file and its WAL