Skip to content

Don't retry a request whose body comes from an IO - #371

Merged
mame merged 1 commit into
ruby:masterfrom
orien:no-retry-consumed-request-body
Nov 27, 2026
Merged

mame merged 1 commit into
ruby:masterfrom
orien:no-retry-consumed-request-body

Conversation

@orien

@orien orien commented Nov 27, 2026 •

Copy link
Copy Markdown
Contributor

Context

Net::HTTP automatically retries an idempotent request after a connection error (max_retries, 1 by default). The retry sends the same request object again.

When the request body comes from an IO, the first attempt may already have read it, so the retry sends only what is left. With chunked transfer encoding the leftover is still a well-formed body, so the server accepts it and can store a truncated or empty body while the client sees a success response.

Change

A request whose body comes from an IO is no longer retried automatically. The original error is raised, so the caller can rewind the IO and retry itself.

  • This covers a body set with body_stream= and any IO value given to set_form.
  • Requests with a string body, a form without IO values, or no body are retried as before.
  • The max_retries= documentation describes the new rule.

Considerations

  • The check looks at where the body comes from, not at whether the IO was read. A request that fails before any body byte is sent, for example on a stale keep-alive connection, was retried safely before and now raises.
  • Tracking whether the IO was actually read would keep those retries, at the cost of new state on the request object. I kept the smaller change; I'm happy to switch if that is preferred.

A retry re-sends the same request object. When the body comes from
body_stream, or from an IO in form data, the first attempt may have
read that IO, so the retry sends only what is left. With chunked
framing that remainder is a complete message, and the server can store a
truncated or empty body while the client sees a success response.

Never retry such requests automatically; raise the original error so the
caller can rewind the IO and retry itself.
@orien orien changed the title Don't retry a request whose body was read from an IO Don't retry a request whose body comes from an IO Nov 27, 2026
@orien orien changed the title Don't retry a request whose body comes from an IO Don't retry a request whose body comes from an IO Nov 27, 2026
@orien
orien marked this pull request as ready for review November 27, 2026 06:40
@orien

orien commented Nov 27, 2026

Copy link
Copy Markdown
Contributor Author

/cc @mame

@mame mame left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you! Retrying only when the IO hasn't been read yet would make the behavior harder to predict, so let's consider it when someone actually needs it.

@mame
mame merged commit e686ee5 into ruby:master Nov 27, 2026
26 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.

2 participants