Repository navigation
fix: bound each upstream attempt by the full attempt deadline - #64
Merged
Merged
Conversation
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.
This was referenced Oct 14, 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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
performance.attempt_timeout_secondsonly reacheshttp.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:i.e. the hang happened in a phase
ResponseHeaderTimeoutcannot see.Fix
Wrap every attempt in a context deadline covering dial + request write + response headers + first event (
internal/gateway/first_event.go).doInferenceAttemptnow takes anattemptTimeoutand arms atime.AfterFuncthat cancels the attempt context with a dedicatederrAttemptTimeout.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 <= 0keeps the previous behavior exactly (gateway.gopassescfg.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 anhttptrace.ClientTrace, carried on aphasedErrorthat still unwraps to the original error so all existingerrors.Ischecks keep working. The attempt timeout is classified as its ownattempt_timeout_<phase>leg instead of the generictransportbucket. 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 classifiedattempt_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/andgo test ./internal/gateway/pass.Notes
performance.attempt_timeout_secondsand is a no-op when it is 0.main(1e2702f); no overlap with the files changed since v1.3.7.