Skip to content

fix(api): retryTransport body lifecycle and Retry-After parsing - #60

Merged
timimsms merged 1 commit into
mainfrom
fix/retry-transport-bodies
Nov 18, 2026
Merged

timimsms merged 1 commit into
mainfrom
fix/retry-transport-bodies

Conversation

@timimsms

Copy link
Copy Markdown
Owner

Summary

Fixes the response-body handling and Retry-After parsing in retryTransport (internal/api/retry.go).

  • Body leak on 429 retry: every response we throw away is now drained (up to 64 KiB, so the connection can be reused) and closed before the next attempt. That includes the 429 + Retry-After path, which used to continue past the close.
  • Closed body on final failure: when retries run out, the last response comes back with its body still open and readable.
  • Retry-After: both forms from RFC 7231 §7.1.3 are parsed: delay-seconds and HTTP-date (via http.ParseTime). The wait is capped at 60s. A date in the past means retry immediately. Values that don't parse fall back to exponential backoff.
  • Context-aware waits: time.Sleep is replaced by a timer + select on req.Context().Done(). Cancelling the context stops the wait and returns ctx.Err().
  • Request replay: the body was already buffered once and resent on each attempt. Each attempt now uses req.Clone with GetBody set, so the caller's request is no longer modified.
  • Tests: new optional sleep/now fields (nil means real time) keep the timing tests fast and deterministic. The old tests that waited in real time (≥1s) now use them. New httptest-based tests cover:
    • a counting body wrapper that checks every intermediate body is closed
    • exhausted retries returning a readable body
    • Retry-After in seconds, as an HTTP-date, and over the cap
    • context cancel stopping a 30s wait
    • a POST body sent intact on all three attempts
    • a table test for parseRetryAfter

Closes #31

Checklist

  • ./scripts/ci.sh passes locally (or go test -race ./... at minimum)
  • Commit messages use conventional prefixes (feat:, fix:, docs:, chore:, ci:, test:)
  • CLI docs regenerated if command help text changed (cu docs markdown --dir docs/site/commands) (no help text changed)
  • Docs updated if user-facing behavior changed (no user-facing docs affected)

🤖 Generated with Claude Code

…try-After

- Drain and close every discarded response before retrying, including the
  429 Retry-After path that previously skipped the close.
- On exhausted retries return the last response with its body still open.
- Parse Retry-After as delay-seconds or HTTP-date, capped at 60s.
- Wait with a timer that aborts on request context cancellation.
- Clone the request per attempt (with GetBody) instead of mutating it.
- Inject sleep/clock in tests so retry tests no longer sleep in real time.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@timimsms
timimsms merged commit b2d4865 into main Nov 18, 2026
16 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.

retry: retryTransport leaks response body on 429 retry and returns a closed body on final failure

1 participant