Skip to content

fix: preserve Request bodies for explicit retries - #652

Open
Twiink wants to merge 1 commit into
unjs:mainfrom
Twiink:fix/request-body-retry
Open

Twiink wants to merge 1 commit into
unjs:mainfrom
Twiink:fix/request-body-retry

Conversation

@Twiink

@Twiink Twiink commented Nov 15, 2026 •

Copy link
Copy Markdown

Description

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.

@coderabbitai

coderabbitai Bot commented Nov 15, 2026 •

Copy link
Copy Markdown

Explain Change →

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.

Estimated code review effort: 3 (Moderate) | ~20 minutes

Change: Bug fix

Sequence Diagram(s)

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: Passed 4

Check name Status Explanation
Description Check Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check Passed 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 Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check Passed 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.

Share

Recent review info

Run configuration

  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • 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.

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