You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
fix: preserve Request bodies for explicit retries - #652
A prebuilt Request with a body cannot recover from a transient error even with an explicit retry count. Native fetch consumes the request body on the first attempt, so the second attempt throws that the Request has already been used before another HTTP request is sent. A string URL with an options body succeeds in the same scenario.
Reserve a Request clone before an attempt when an explicit numeric retry count is still positive and the options do not replace the body. Use it for a later attempt, and cancel an unused saved body when processing finishes. The cleanup does not wait on the other branch of the cloned stream. Error hooks can still replace the request, and already consumed inputs continue to produce a normalized FetchError.
Eleven native-fetch HTTP tests cover POST and PUT JSON bodies, multipart data, a connection closed after receiving the body, retry exhaustion, disabled/default retries, an overriding body, a replacement request from an error hook, and already consumed inputs. The five replay regressions fail on the unchanged source.
Validation
Full Vitest suite with coverage: 39 passed.
Project lint, build, and TypeScript checks: passed.
Original HTTP reproduction: a JSON PUT Request now reaches the server twice with identical bodies and recovers from the first 503; string-URL and bodyless-Request controls pass.
git diff --check: passed.
The problem also reproduces in published versions 1.5.1 and 2.0.0-alpha.3. #633 and #635 address Request method selection; their diffs do not preserve a consumed body. This patch keeps the existing default retry policy and only reserves bodies for explicit retry counts.
No actionable comments were generated in the recent review.
Walkthrough
The change adds request-body preservation for eligible retries in $fetchRaw. It also adds tests for retry behavior, request overrides, and consumed request bodies.
Changes: Request body retry flow
Layer / File(s)
Summary
Request body retry lifecycle src/fetch.ts, test/request-retry.test.ts
$fetchRaw clones a Request body when numeric retry is greater than zero and no options body is set. It uses the clone for retries only if the request object is unchanged, then cancels an unused unlocked clone. Tests cover JSON and multipart bodies, network failures, retry counts, body overrides, request replacement, and consumed requests.
sequenceDiagram
participant Caller
participant Fetch as $fetchRaw
participant Server as HTTP server
participant ErrorHook as onError
Caller->>Fetch: Send request
Fetch->>Server: Send request body
Server-->>Fetch: Return fetch failure
Fetch->>ErrorHook: Pass cloned request when eligible
ErrorHook-->>Fetch: Select request for retry
Fetch->>Server: Send retry request body
Loading
Suggested reviewers:pi0
Merge Risk: Minimal · up to 7ac9d
Retries of prebuilt Request objects now resend their bodies when an explicit positive retry count is set, and default behavior is unchanged. No merge-blocking risk was identified.
Pre-merge checks: 4
Check name
Status
Explanation
Description Check
Check skipped - CodeRabbit’s high-level summary is enabled.
Title check
The title "fix: preserve Request bodies for explicit retries" names the main change. The objectives say the PR keeps bodies of prebuilt Request objects across retries when an explicit numeric retry co…
Linked Issues check
Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check
Check skipped because no linked issues were found for this pull request.
Full details: Title check
Explanation
The title "fix: preserve Request bodies for explicit retries" names the main change. The objectives say the PR keeps bodies of prebuilt Request objects across retries when an explicit numeric retry count is positive. The raw summary confirms that $fetchRaw clones a Request body when numeric retry is above zero, and that a test suite covers body preservation and retries. The title is concise, uses a conventional fix: prefix, and has no file lists or vague terms.
Finishing Touches
Generate unit tests (beta)
Create a new PR
Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.
File: No configuration file from this repository applied
Commit: Unavailable
Layers: defaults
Review profile: CHILL
Plan: Advanced
Run ID: 45bc3dc8-fbaf-4fcb-abf7-f91fa64d5fd2
Commits
Reviewing files that changed from the base of the PR and between 1dbc37f and 7ac9de4.
Files selected for processing: 2
src/fetch.ts
test/request-retry.test.ts
Included review availability: This review used your included allowance. Your plan provides up to 8 included reviews per hour; 6 remain after this review.
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
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.
Description
A prebuilt
Requestwith a body cannot recover from a transient error even with an explicit retry count. Native fetch consumes the request body on the first attempt, so the second attempt throws that the Request has already been used before another HTTP request is sent. A string URL with an options body succeeds in the same scenario.Reserve a Request clone before an attempt when an explicit numeric retry count is still positive and the options do not replace the body. Use it for a later attempt, and cancel an unused saved body when processing finishes. The cleanup does not wait on the other branch of the cloned stream. Error hooks can still replace the request, and already consumed inputs continue to produce a normalized FetchError.
Eleven native-fetch HTTP tests cover POST and PUT JSON bodies, multipart data, a connection closed after receiving the body, retry exhaustion, disabled/default retries, an overriding body, a replacement request from an error hook, and already consumed inputs. The five replay regressions fail on the unchanged source.
Validation
git diff --check: passed.The problem also reproduces in published versions 1.5.1 and 2.0.0-alpha.3. #633 and #635 address Request method selection; their diffs do not preserve a consumed body. This patch keeps the existing default retry policy and only reserves bodies for explicit retry counts.