Skip to content

fix: bound each upstream attempt by the full attempt deadline - #64

Merged
jasonxu114514 merged 1 commit into
jasonxu114514:mainfrom
mcheiyue:fix/attempt-deadline
Oct 15, 2026
Merged

jasonxu114514 merged 1 commit into
jasonxu114514:mainfrom
mcheiyue:fix/attempt-deadline

Conversation

@mcheiyue

Copy link
Copy Markdown
Contributor

Problem

performance.attempt_timeout_seconds only reaches http.Transport.ResponseHeaderTimeout. That timer starts after the request body is written and stops at the response headers, so an exit that wedges during dial or request write runs with no timer at all and can consume the entire request budget by itself. The attempts that follow are then fired against an already-expired context.

Observed shape on a production deployment (v1.3.7): a single 504 request showed attempts=8, where legs 1–7 failed fast (first-event / 429 / transport) and leg 8 hung from ~113s to the 300s request budget with no leg recorded at all — the budget-exhaustion guard skips recording once the parent context is dead. The upstream error was:

Post "https://opencode.ai/zen/v1/chat/completions": context deadline exceeded [phase=headers]

i.e. the hang happened in a phase ResponseHeaderTimeout cannot see.

Fix

Wrap every attempt in a context deadline covering dial + request write + response headers + first event (internal/gateway/first_event.go).

  • doInferenceAttempt now takes an attemptTimeout and arms a time.AfterFunc that cancels the attempt context with a dedicated errAttemptTimeout.
  • The deadline is released atomically the moment a response is committed: commitDeadline() decides "deadline already fired" vs "commit the response" under one mutex, so the timer can never cut an established stream. This closes a stop-the-timer race at the first-event boundary — a stream whose first event arrived just as the deadline fired would otherwise be cancelled mid-flight.
  • attemptTimeout <= 0 keeps the previous behavior exactly (gateway.go passes cfg.Performance.AttemptTimeout(...), which is 0 when unset).

Diagnostics (part of the same change)

Each failed attempt is tagged with the phase it died in — dial / write / headers / first_event — via an httptrace.ClientTrace, carried on a phasedError that still unwraps to the original error so all existing errors.Is checks keep working. The attempt timeout is classified as its own attempt_timeout_<phase> leg instead of the generic transport bucket. A hung exit is now attributable from the request log alone.

Tests

internal/gateway/first_event_test.go (new):

  • TestAttemptDeadlineCutsHungHeaders — a server that never sends headers is cut at the deadline and classified attempt_timeout_headers.
  • TestAttemptDeadlineReleasesAfterFirstEvent — the safety boundary: once the first event arrives the deadline must not leak into the established stream.
  • TestClassifyAttemptError, TestPhasedErrorUnwrapKeepsIs, TestAttemptTracePhase.

go build ./..., go vet ./internal/gateway/ and go test ./internal/gateway/ pass.

Notes

  • No config key is added; the behavior is gated by the existing performance.attempt_timeout_seconds and is a no-op when it is 0.
  • Rebased on current main (1e2702f); no overlap with the files changed since v1.3.7.

performance.attempt_timeout_seconds only maps to
http.Transport.ResponseHeaderTimeout, which starts after the request
body is written and stops at the response headers. An exit that wedges
during dial or request-write hangs with no timer running and can consume
the entire request budget on its own; the attempts that follow are then
fired against an already-expired context.

Wrap every attempt in a context deadline covering dial + request write +
response headers + first event. Release it atomically the moment a
response is committed so the deadline can never cut an established
stream (a stop-the-timer race at the first-event boundary is closed by
deciding "deadline fired" vs "commit response" under one lock).

Also tag each failed attempt with the phase it died in (dial / write /
headers / first_event) so a hung exit is attributable from the request
log alone, and classify the attempt timeout as its own leg instead of
the generic transport bucket.

Adds unit tests for the deadline cut, the release-after-first-event
boundary, the phase classification and error unwrapping.
@jasonxu114514
jasonxu114514 merged commit 0569f95 into jasonxu114514:main Oct 15, 2026
jasonxu114514 added a commit that referenced this pull request Oct 15, 2026
attempt_timeout_seconds only reached the transport's
ResponseHeaderTimeout, which starts after the request body is written, so
an exit wedged in dial or request write could consume the whole request
budget. When the option is set, each attempt now also carries a context
deadline covering dial, write, headers and, when enabled, the first SSE
event; a committed stream is never cut. Legs record the phase an attempt
died in. A timeout after the upstream already answered is slow generation,
not a broken proxy, so it does not evict the exit.

Co-authored-by: mcheiyue <[email protected]>
SK-DEV-AI added a commit to SK-DEV-AI/opencode2api that referenced this pull request Oct 15, 2026
…ver-key usage)

Takes upstream f8e996d/be91f86/0204ca1 (client fingerprint + inference
endpoint + genuine tools), jasonxu114514#59 anon cooldown lockup, jasonxu114514#63 token floor,
jasonxu114514#64 whole-attempt deadline, jasonxu114514#62 tool images, jasonxu114514#58 server-key usage
split, jasonxu114514#65/jasonxu114514#66 models surface, plus our merged jasonxu114514#76/jasonxu114514#77/jasonxu114514#78.

Kept local (fork-owned, still no upstream equivalent): zero-delivery
replay + length rescue + SSE heartbeat + stop/tail census (stream
lanes), offline no_upstream_contact probe guard, single-marshal Zen
shaping with paid-model gate, tcp4 dial + TLS resumption, RandomID
math/rand fallback, cancel-detached refresh fix (PR jasonxu114514#80 superseded by
maintainer feedback; kept locally pending lifetime-context rework).
Telemetry takes upstream's session-burn + server-key split with their
LRU session cap (better than our smallest-first); CloneMap takes
upstream's merged comment. Metrics ring test retargeted at upstream's
concrete ring types. doUpstream keeps upstream's 3-value signature;
replay uses a new doUpstreamCounted wrapper for continuous legs.
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