Skip to content

fix: Set GetBody on uploads for HTTP/2 retry#4318

Merged
gmlewis merged 2 commits into
google:masterfrom
JamBalaya56562:fix/2113-upload-getbody
Jun 22, 2026
Merged

fix: Set GetBody on uploads for HTTP/2 retry#4318
gmlewis merged 2 commits into
google:masterfrom
JamBalaya56562:fix/2113-upload-getbody

Conversation

@JamBalaya56562

Copy link
Copy Markdown
Contributor

Fixes #2113

Problem

NewUploadRequest passed the body to http.NewRequestWithContext without setting Request.GetBody. Unlike the JSON NewRequest path (where the Go stdlib auto-populates GetBody for *bytes.Buffer), the upload path wraps the reader in uploadRequestBodyReader to hide concrete types, so GetBody stayed nil. As a result net/http2 could not rewind the body and asset uploads failed with:

http2: Transport: cannot retry err ... after Request.Body was written; define Request.GetBody to avoid this error

when a stream was refused (REFUSED_STREAM / GOAWAY / idle-connection close). This has been reported by several downstream tools (ghr, goreleaser, go-release-action).

Fix

Set req.GetBody when the reader implements both io.Seeker (to record the body's starting offset) and io.ReaderAt. GetBody returns an independent io.SectionReader (re-wrapped in uploadRequestBodyReader to preserve the existing race-safety behaviour), so each call yields a fresh copy without disturbing the original body's read position — honoring the http.Request.GetBody contract.

This is satisfied by *os.File (used by UploadReleaseAsset), *bytes.Reader and *strings.Reader. Readers that cannot provide an independent, rewindable view are intentionally left without GetBody so large uploads are never buffered in memory.

This is a backwards-compatible change (no signature changes).

Tests

  • TestNewUploadRequest_setsGetBodyForSeekableReader — asserts GetBody is set, returns identical bytes on repeated calls, and does not consume the original req.Body.
  • TestNewUploadRequest_noGetBodyWithoutReaderAt — asserts GetBody stays nil for a non-seekable reader and for a seek-only reader without io.ReaderAt.

go test -race ./github/, go vet, and golangci-lint all pass.

NewUploadRequest passed a body to http.NewRequestWithContext without
setting Request.GetBody, so net/http2 could not rewind the body and
failed asset uploads with "cannot retry ... after Request.Body was
written" when a stream was refused (REFUSED_STREAM/GOAWAY).

Set GetBody when the reader implements both io.Seeker (to capture the
body's starting offset) and io.ReaderAt, returning an independent
io.SectionReader so GetBody yields a fresh copy without disturbing the
original body's read position. Readers that cannot provide an
independent, rewindable view are left without GetBody to avoid
buffering large uploads in memory.

Fixes google#2113
@gmlewis gmlewis changed the title fix: Set GetBody on uploads for HTTP/2 retry fix: Set GetBody on uploads for HTTP/2 retry Jun 22, 2026
@codecov

codecov Bot commented Jun 22, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 97.47%. Comparing base (8111735) to head (373b0b5).

Additional details and impacted files
@@           Coverage Diff           @@
##           master    #4318   +/-   ##
=======================================
  Coverage   97.47%   97.47%           
=======================================
  Files         192      192           
  Lines       19312    19320    +8     
=======================================
+ Hits        18824    18832    +8     
  Misses        270      270           
  Partials      218      218           

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@gmlewis gmlewis left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Thank you, @JamBalaya56562!

Comment thread github/github.go
@gmlewis gmlewis added the NeedsReview PR is awaiting a review before merging. label Jun 22, 2026
Pass a closed *os.File (via openTestFile) to NewUploadRequest so the
io.SeekCurrent probe fails, covering the previously untested error
branch that returns the seek error.

@gmlewis gmlewis left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Excellent. Thank you, @JamBalaya56562!
LGTM.
Awaiting second LGTM+Approval from any other contributor to this repo before merging.

cc: @stevehipwell - @alexandear - @Not-Dhananjay-Mishra

@stevehipwell stevehipwell left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

LGTM

@gmlewis gmlewis removed the NeedsReview PR is awaiting a review before merging. label Jun 22, 2026
@gmlewis

gmlewis commented Jun 22, 2026

Copy link
Copy Markdown
Collaborator

Thank you, @stevehipwell!
Merging.

@gmlewis gmlewis merged commit 216f315 into google:master Jun 22, 2026
14 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.

http2 failure in UploadAsset

3 participants