Skip to content

OkHttp retries consume RequestBody streams, causing empty payloads on retry for multipart uploads #9136

@Hawra2020

Description

@Hawra2020

OkHttp version 4.12.0

Description

When retryOnConnectionFailure is enabled (default behavior), OkHttp retries failed requests (e.g., due to connection errors) by reusing the same RequestBody. For multipart uploads using a non-repeatable InputStream (e.g., ByteArrayInputStream), the stream is fully consumed during the first attempt. This results in an empty payload (0-byte file) being sent on retry, leading to data loss or incomplete uploads.

This issue affects scenarios like file uploads where the InputStream cannot be reliably reset (e.g., network streams). Workarounds include disabling retries (retryOnConnectionFailure(false)) but they are not ideal when relying on automatic retries.

A common attempt to fix this is calling InputStream.reset() after marking the stream, but this fails because OkHttp's retry mechanism does not provide a hook to reset the stream between attempts.

Steps to Reproduce

  1. Create an OkHttp client with retryOnConnectionFailure(true) (default).
  2. Prepare a multipart RequestBody using a non-repeatable InputStream (e.g., ByteArrayInputStream).
  3. Send a POST request to a mock server (e.g., via WireMock) that simulates a connection failure on the first attempt (e.g., returns an empty response or fault to trigger retry).
  4. Observe the retry request: The InputStream is exhausted, sending an empty body (0 bytes).

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugBug in existing code

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions