Do not retry requests with a consumed streaming body - #872
Open
Divyansh-db wants to merge 3 commits into
Open
Conversation
When a request with a streaming body (e.g. Files.upload) receives a retriable HTTP response, the SDK retried by re-sending the same InputStream. The stream was already consumed by the first attempt, so the retry transmitted an empty body — silently uploading a 0-byte file (HTTP 204) or surfacing as a confusing InternalError. InputStream-backed bodies cannot be rewound, so retrying is unsafe once the body has been sent. Skip the retry when the request has a streaming body and an HTTP response was received (which proves the body was transmitted), and surface the original error so the caller can retry with a fresh stream. Transport-level IOErrors (no response received, e.g. a pre-send ConnectException) still retry, since the stream may not have been read.
Divyansh-db
temporarily deployed
to
test-trigger-is
July 30, 2026 21:43 — with
GitHub Actions
Inactive
Divyansh-db
temporarily deployed
to
test-trigger-is
July 30, 2026 21:43 — with
GitHub Actions
Inactive
Divyansh-db
temporarily deployed
to
test-trigger-is
July 31, 2026 08:47 — with
GitHub Actions
Inactive
Divyansh-db
temporarily deployed
to
test-trigger-is
July 31, 2026 08:49 — with
GitHub Actions
Inactive
Divyansh-db
temporarily deployed
to
test-trigger-is
July 31, 2026 08:49 — with
GitHub Actions
Inactive
Contributor
|
If integration tests don't run automatically, an authorized user can run them manually by following the instructions below: Trigger: Inputs:
Checks will be approved automatically on success. |
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Summary
Requests with a streaming body (e.g.
files().upload()) are backed by a single-useInputStream. When such a request received a retriable HTTP response (429/501/503), theretry loop in
ApiClient.executeInnerre-sent the sameInputStream. Because the firstattempt had already consumed the stream, the retry transmitted an empty body, which
manifested as either:
InternalError) surfaced fromFilesImpl.upload.This only occurs when a retriable status interrupts a streaming upload, so it is rare and
intermittent, but when it happens the upload silently corrupts data.
Root cause
Requestholds the streaming body as a singleInputStreamreference. On retry,executeInnerreuses the sameRequest, andCommonsHttpClientwraps that already-consumedstream in a fresh
InputStreamEntity— sending 0 bytes. The uploadPUTis non-idempotent,and
NonIdempotentRequestRetryStrategytreats 429/501/503 as retriable, so a transient 503triggers the empty-body retry.
An
InputStream-backed body cannot be rewound, so retrying is inherently unsafe once the bodyhas been sent.
Fix
In
ApiClient.executeInner, skip the retry when the request has a streaming body and anHTTP response was received (which proves the body was already transmitted), and surface the
original error to the caller so it can retry with a fresh stream.
The guard is deliberately narrow:
is built per attempt — so their retry behavior is unchanged.
IOErrors (no response received, e.g. a pre-sendConnectException)still retry as before, since in that case the stream may not have been read yet.
The only behavior removed is the retry of an already-consumed stream, which never worked. This
lives in the hand-written core (
ApiClient), so it applies to any streaming upload rather thanbeing specific to the Files API.
Tests
doesNotRetryStreamingBodyAfterResponse— a streamingPUTthat gets a 503 now throws thetyped
TemporarilyUnavailable(503) instead of silently retrying with an empty body.retriesNonStreamingBodyOn503— a string-bodied request with the same 503 still retries andsucceeds, confirming the guard does not regress ordinary requests.
Full module test suite passes (1404 tests, 0 failures).