Skip to content

blob/s3blob, blob/azureblob: close the response body on zero-length reads - #3850

Merged
vangent merged 1 commit into
google:masterfrom
iamabhilaksh:blob-zero-length-close
Nov 22, 2026
Merged

vangent merged 1 commit into
google:masterfrom
iamabhilaksh:blob-zero-length-close

Conversation

@iamabhilaksh

@iamabhilaksh iamabhilaksh commented Nov 16, 2026 •

Copy link
Copy Markdown
Contributor

Problem

On a zero-length read, NewRangeReader replaces the response body with http.NoBody and never closes the real body (s3blob.go#L669-L672, azureblob.go#L703-L708). Reader.Close then closes only http.NoBody, so each zero-length read leaves an HTTP connection open.

Change

  • Close the real body before replacing it.
  • New test TestZeroLengthReadClosesBody in each package. It expects 1 body closed.

Impact and risk

  • Impact: each zero-length read leaks one connection and 2 goroutines until the server closes the connection. In a long-running service this adds up to file descriptors, memory and new TLS handshakes.
  • Risk: low. This is one Close on a body that was already discarded. The data returned doesn't change.
  • Go 1.27: S3 now also reuses the connection, because Go drains the 1-byte body on Close. Go 1.27 doesn't fix master, since it only drains bodies that are closed.

Testing

30 zero-length reads of a 1 MiB object, then Close:

  • LocalStack: master closed 0 of 30, goroutines went from 4 to 62. With this change, 30 of 30 closed and 2 goroutines left.
  • Real S3: master went from 3 to 61 goroutines. With this change, 1 left (2 of 2 runs).
  • Real S3 on go1.27: with this change, 0-1 new connections across 30 reads, vs 29 on master.
  • Azurite: master closed 0 of 30, goroutines went from 4 to 62. With this change, 30 of 30 closed and 2 left.
  • The new tests fail on master with closed 0 response bodies, want 1.

Checks (go1.27.3 and go1.26.10): go test -race, go vet, gofmt -s and golangci-lint v2.14.0 pass. go.mod and alldeps are unchanged.

Not changed here: azureblob still requests the whole blob, because Count: 0 means no limit. Requesting a range on an empty blob might fail with 416.

…eads

For a zero-length read, NewRangeReader replaced the response body with
http.NoBody but never closed the real one, so each zero-length read kept
an HTTP connection open. Close it before replacing it.
@iamabhilaksh
iamabhilaksh marked this pull request as ready for review November 16, 2026 12:18
@codecov

codecov Bot commented Nov 22, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 80.94%. Comparing base (bbb3555) to head (64226c0).
⚠️ Report is 6 commits behind head on master.

Additional details and impacted files
@@            Coverage Diff             @@
##           master    #3850      +/-   ##
==========================================
+ Coverage   80.35%   80.94%   +0.59%     
==========================================
  Files         104      104              
  Lines       12571    12620      +49     
==========================================
+ Hits        10101    10215     +114     
+ Misses       2469     2404      -65     
  Partials        1        1              

☔ 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.

@vangent
vangent merged commit 21e8366 into google:master Nov 22, 2026
13 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