Repository navigation
fix(model-refresh): refresh HTTP client on transport errors - #502
linusmixson wants to merge 4 commits into
Conversation
|
@codex review |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 7e45502853
ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
| if previous is not None: | ||
| await _close_client(previous) |
There was a problem hiding this comment.
Avoid closing active shared client during transport recovery
refresh_http_client replaces the global client and immediately closes the previous one, but other request paths (for example app/core/clients/proxy.py at get_http_client().session) keep a per-request reference to that old session. If model refresh triggers this path while user traffic is in flight, those requests can fail mid-call with session/connection-closed transport errors unrelated to their own upstream health. This regression was introduced by runtime client rotation and needs graceful draining (or deferred close) instead of immediate teardown.
Useful? React with 👍 / 👎.
|
Thanks for digging into this @linusmixson — the failure mode you describe (codex-lb stuck after VPN flap / sleep without a process restart) is a real and frequently reported problem, and the fix shape (transport-error tagging + one-shot HTTP client rotation + retry) is in the right direction. A couple of things to address before I merge: P1 (codex review): old client closed while still in flightThe codex bot review caught a real concern that I'd want fixed before merge:
Would you mind reworking
Even better, a true "drain" (track in-flight requests on the client and only close after they complete) — but a deferred close on a timer is probably the right tradeoff here vs. the bookkeeping cost of true drain. Open to either. If you'd rather not tackle the drain in this PR, an alternative is to scope the rotation to the model-fetcher only — give the model-refresh path its own dedicated CI: ty type-check failingThe PR is currently red on
You can reproduce locally with: Smaller things, not blockers
Once the P1 (deferred close or scoped rotation) and the ty errors are addressed, this should be ready. Thanks again 🙏 |
|
@Soju06 Can do; thanks for looking this over. |
Retry model fetches after transport failures by refreshing the shared HTTP client. - Add transport-error classification to model fetch failures. - Refresh the shared HTTP client once on transport errors, then retry the fetch with the same account. - Keep the retry path from cascading into repeated reconnect attempts. - Add unit coverage for transport error detection, HTTP client refresh replacement, and failover retry behavior. Agent: GPT-5.4; GPT-5.5
Agent: GPT-5.4-mini; GPT-5.5
|
Model's comments:
@Soju06: requesting rereview. |
|
@Soju06 ping (sorry) |
Summary
This PR makes model refresh recover from transport-level upstream failures by rebuilding the shared
aiohttpclient and retrying the fetch once.Handled failures now include compact error details, while unexpected exceptions still keep stack traces.
Observed Bug Evidence
Running codex-lb via
uvxin low-reliability network environments (VPN, laptop sleep/wake, etc.) often resulted in the software entering a state in which the upstream connection was lost and could not be regained without restarting the process. These present changes make codex-lb behave seamlessly under the same condtions.Root Cause
The model refresh path uses the shared
aiohttp.ClientSession.When the network changes underneath the process, the model fetch can fail before an HTTP response is received, surfacing as transport exceptions.
Previously, model refresh treated these the same as other model-fetch failures. It did not rebuild the shared HTTP client, so a bad/stale session or connector could continue to be reused after the network came back.
Fix
Model-fetch transport exceptions are now wrapped as ModelFetchError with transport_error=True.
When the scheduler sees the first transport error in a refresh cycle, it refreshes the shared HTTP client, closes the previous client, and retries the model fetch once.
HTTP status errors, auth failures, and invalid upstream responses stay on the existing paths.
The recovery logs now include the original transport error tersely, so failures are easier to diagnose without turning expected retry paths into stack traces.
Test Plan
uv run ruff check app/core/openai/model_refresh_scheduler.py tests/unit/test_model_refresh_scheduler.pyuv run pytest tests/unit/test_model_refresh_scheduler.pyuv run pytest tests/unit/test_http_client.pyuv run pytest tests/unit/test_model_refresh_scheduler.pygit diff --check