Skip to content

Review sandbox: RetryTransport context cancellation and body ownership - #1

Open
yoshi-taka wants to merge 4 commits into
review/pr3383-basefrom
review/pr3383-copilot
Open

yoshi-taka wants to merge 4 commits into
review/pr3383-basefrom
review/pr3383-copilot

Conversation

@yoshi-taka

Copy link
Copy Markdown
Owner

Fork-only review sandbox for integrations#3383. The base is pinned to upstream main (1b9c5c1), so this PR contains the entire proposed upstream diff, not just the latest fix.

This change makes retry delays cancellable, stops attempts after cancellation, skips the final retry delay, and drains/closes discarded response bodies. It also closes unsent request bodies on initial cancellation without draining them; bodies already handed to the underlying transport remain its responsibility, including asynchronous closure.

Regression tests cover cancellation before sending (nil, NoBody, stream and GetBody), ownership after sending, intermediate response closure and preserving the final response. No process-wide HTTP client is modified.

Validation: full unit suite with -race, repeated cancellation tests, go build ./..., go vet ./..., golangci-lint including forcetypeassert on new code, and formatter checks.

AI-assisted implementation; changes have been inspected and tested locally. This sandbox will not be merged. The upstream PR will be updated only after review findings have been addressed.

- Replace `time.Sleep` with context-aware `sleep()` in `RetryTransport.RoundTrip`
- Skip the delay after the final retry attempt (`retry < maxRetries`)

`RateLimitTransport` already uses the existing `sleep(ctx, dur)` helper (transport.go:122), which respects context cancellation. `RetryTransport` was using bare `time.Sleep`, causing it to ignore `ctx.Done()` during retry delays.

This aligns `RetryTransport` with the existing pattern in the codebase.

- Add `TestRetryTransport_cancelled`: verifies context cancellation interrupts retry delay
- Add `TestRetryTransport_no_sleep_after_last_retry`: verifies no delay after final attempt
Drain and close responses discarded before a retry so their connections
and resources are not leaked, and discard a response returned alongside
a transport error which can never reach the caller.

The drain is capped at 4KB to match the new client implementation, so
small error bodies allow connection reuse while oversized bodies are
still closed safely.

Adds tests covering body closure for intermediate responses and for
responses returned together with an error.
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.

1 participant