Skip to content

codex: stop re-sending a request after the response header timeout - #160

Merged
raine merged 2 commits into
raine:mainfrom
rikbrown:fix/codex-header-timeout
Sep 19, 2026
Merged

raine merged 2 commits into
raine:mainfrom
rikbrown:fix/codex-header-timeout

Conversation

@rikbrown

Copy link
Copy Markdown
Contributor

The failure

Behind a pooling proxy, one deployment logged 85 failed Codex requests in a day. Every one was the same: HTTP 502, elapsed 250,5xx ms, "Timed out waiting 60000ms for Codex response headers". Every one was a large-context request (~2 MB body) to a high-effort model. Smaller models on the same proxy never failed, and short requests to the same model never failed — their headers arrive in seconds. The largest head latency that succeeded under the 60s bound was 55.8s.

Claude Code retries a 502 silently, so the user sees a 10-20 minute stall, not an error.

The mechanism

start_post_http wraps send() in tokio::time::timeout(header_timeout_ms) and, on expiry, returns a statusless CodexError whose message begins "Timed out waiting". is_retryable_transport_error matches any statusless error containing that substring, so stream_codex_http_events treats it as a transport failure and re-sends. With MAX_BUFFERED_TRANSPORT_RETRIES = 3 that is four sends: 4 x 60,000ms plus 1,500 + 3,000 + 6,000ms of backoff = 250,500ms, which is the elapsed figure in every log line.

The re-sends cannot succeed. The request reached Codex, which withholds the response head until the model produces its first output, so a re-send restarts the reasoning from scratch and the new attempt runs out of time exactly like the last one. It also re-uploads the request body, which is the expensive part of a large-context turn.

What changed

Commit 1 makes the bound configurable as codex.headerTimeoutMs / CCP_CODEX_HEADER_TIMEOUT_MS (env, file, default precedence; values below 1000ms ignored rather than clamped) and moves the default from 60s to 300s. IMAGE_HEADER_TIMEOUT_MS was already 300s for the same reason.

Commit 2 gives the timeout error a detail marker, http_response_headers, alongside the existing http_response_body and http_response_sse markers, and makes is_retryable_transport_error refuse it. That covers the streaming path (retryable_http_stream_error delegates to it) and the buffered path (post_codex_with_transport). The status and message the client sees are unchanged: still 502, still the same text, just once. header_timeout_retries is removed — it was carried through the constructors and the test helper behind #[allow(dead_code)] and never read.

Left alone

  • Retries for connection failures before any bytes reach the server, and for retryable upstream statuses (429/5xx/529). Only the header timeout is reclassified.
  • No retry is kept for a timeout that fires while the request body is still being written. From inside tokio::time::timeout that case is indistinguishable from the rest, and the server may already have the body in full.
  • The body idle timeout and its http_response_body errors.
  • retryable_live_start_codex_error in mod.rs. live_stream_response already returns immediately for CodexErrorOrigin::Http, so the provider layer never multiplied these attempts.

Tests

  • http_stream_does_not_resend_after_a_header_timeout — an upstream that accepts the connection, reads the request and never answers. Asserts the error status, message and detail, that the call returns before the first retry backoff would even have elapsed, and that the server accepted exactly one connection. Reverting the classifier change fails it.
  • header_timeout_is_not_retried_by_either_classifier — direct assertion on is_retryable_transport_error and retryable_http_stream_error.
  • codex_header_timeout_defaults_overrides_and_floors — default, file value, env precedence, and that a sub-floor value falls back to the default rather than being clamped.

cargo fmt --all -- --check, cargo clippy --all-targets -- -D warnings and cargo test --all pass on each commit separately.

🤖 Generated with Claude Code

The HTTP transport hardcoded a 60s wait for the Codex response headers.
That figure suits a request whose head comes back as soon as the backend
accepts it, but Codex withholds the head until the model produces its
first output, so the wait tracks how much reasoning the request asks for.
A large request to a high-effort model routinely needs longer than 60s,
and the timeout firing fails the request outright.

`codex.headerTimeoutMs` and `CCP_CODEX_HEADER_TIMEOUT_MS` set the bound,
with the usual environment, file, default precedence. The default moves
to 300_000ms, matching IMAGE_HEADER_TIMEOUT_MS, which was already raised
to five minutes for the same reason. Values below 1000ms are ignored
rather than clamped, so a typo does not hide behind a working default.
Running out of patience for the Codex response headers produced a
statusless error whose message begins "Timed out waiting", and
is_retryable_transport_error classifies any statusless error with that
substring as a transport failure. stream_codex_http_events therefore
re-sent the whole request three more times, so one timeout cost
4 * header_timeout plus 10.5s of backoff before the client saw a 502.

Re-sending cannot help. The request reached Codex, which withholds the
response head until the model produces its first output, so a re-send
starts that reasoning from scratch and the new attempt runs out of time
exactly like the last one. It also pays to upload the request body again,
which is the expensive part of a large-context turn. Clients that retry a
502 quietly turn this into a long silent stall.

The timeout error now carries a detail marker and the classifier refuses
it, which covers both the streaming and the buffered HTTP paths. Retries
still apply to what they were built for: connection failures before any
bytes reach the server, and retryable upstream statuses. There is no
retry for a timeout that fires while the body is still being written,
because that case is indistinguishable from the rest from inside the
timeout, and the request may already have been received in full.

header_timeout_retries goes with it. It was carried through the
constructors and the test helper but never read.
@rikbrown
rikbrown force-pushed the fix/codex-header-timeout branch from 0914542 to e51f12e Compare September 19, 2026 19:52
rikbrown added a commit to rikbrown/claude-code-proxy that referenced this pull request Sep 19, 2026
Carries the header-timeout fix from raine#160: a response
header timeout now fails once instead of being re-sent up to four times.

Co-Authored-By: Claude Fable 5.1 <[email protected]>
@raine
raine force-pushed the fix/codex-header-timeout branch from e51f12e to c48d47c Compare September 19, 2026 20:34
@raine
raine merged commit 505febd into raine:main Sep 19, 2026
2 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants