Skip to content

The agent that grows with you

fix(agent): prevent shared OpenAI client FD-recycle corruption from stale stream watchdog

JonthanaHanhe51abb266dbe
diff --git a/agent/chat_completion_helpers.py b/agent/chat_completion_helpers.py
index 3edcddec827..3689b4d5be8 100644
--- a/agent/chat_completion_helpers.py
+++ b/agent/chat_completion_helpers.py
@@ -3492,13 +3492,9 @@ def interruptible_streaming_api_call(agent, api_kwargs: dict, *, on_first_delta=
# already worker-owned-closed by _close_request_client_once
# above; the next attempt builds a fresh one. The shared
# _anthropic_client is never closed from inside a request.
- if agent.api_mode != "anthropic_messages":
- try:
- agent._replace_primary_openai_client(
- reason="stream_mid_tool_retry_pool_cleanup"
- )
- except Exception:
- pass
+ # #70773: same FD-recycle corruption vector for OpenAI.
+ # The shared client will be replaced lazily by
+ # _ensure_primary_openai_client on the next attempt.
continue
# SSE error events from proxies (e.g. OpenRouter sends
@@ -3557,13 +3553,9 @@ def interruptible_streaming_api_call(agent, api_kwargs: dict, *, on_first_delta=
# above; next attempt builds fresh), so the shared
# _anthropic_client is never closed from inside a
# request — only the OpenAI-wire primary is refreshed.
- if agent.api_mode != "anthropic_messages":
- try:
- agent._replace_primary_openai_client(
- reason="stream_retry_pool_cleanup"
- )
- except Exception:
- pass
+ # #70773: same FD-recycle corruption vector for OpenAI.
+ # The shared client will be replaced lazily by
+ # _ensure_primary_openai_client on the next attempt.
continue
# Retries exhausted. Log the final failure with
# full diagnostic detail (chain, headers,
@@ -3806,10 +3798,15 @@ def interruptible_streaming_api_call(agent, api_kwargs: dict, *, on_first_delta=
# FD-recycle corruption vector. Nothing further is needed.
pass
else:
- try:
- agent._replace_primary_openai_client(reason="stale_stream_pool_cleanup")
- except Exception:
- pass
+ # #70773: same FD-recycle corruption vector as #67142.
+ # The shared OpenAI client's connection pool must NOT be
+ # closed from this watchdog/poll thread — worker threads
+ # from previous stale-killed attempts may still be
+ # unwinding their SSL BIOs. The request-local client is
+ # already closed above via _close_request_client_once.
+ # The shared client will be replaced lazily by
+ # _ensure_primary_openai_client on the next request.
+ pass
# Reset the timer so we don't kill repeatedly while
# the inner thread processes the closure.
last_chunk_time["t"] = time.time()