Skip to content

transport/http: return nil from WriteTo on a closed body - #752

Open
AzarudeenshariffA wants to merge 1 commit into
aws:mainfrom
AzarudeenshariffA:fix-writeto-eof-after-close
Open

AzarudeenshariffA wants to merge 1 commit into
aws:mainfrom
AzarudeenshariffA:fix-writeto-eof-after-close

Conversation

@AzarudeenshariffA

Copy link
Copy Markdown

Fixes #751.

safeWriteToReadCloser.WriteTo returned io.EOF once the body was closed. io.WriterTo has no end-of-stream error, so io.Copy hands that io.EOF back to its caller as a failure.

net/http checks a request body for extra bytes with io.Copy(io.Discard, body). That check can run after client.Do has returned and the SDK has closed the body. When it does, the io.EOF makes Request.write fail and the connection gets closed under a response that's still being read. The issue has the details and how we reproduced it.

This change returns (0, nil) from WriteTo after Close. That's what io.Copy would end up with if it read through Read instead.

Tests: the existing TestNewSafeReadCloser_WriteTo case now expects a nil error after Close, and there's a new case that runs io.Copy(io.Discard, body) on a closed body and expects it to succeed. I haven't run the test suite locally; I'm relying on CI here.

Changelog entry added under .changelog/.

By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.

@AzarudeenshariffA
AzarudeenshariffA requested review from a team as code owners November 14, 2026 14:04
safeWriteToReadCloser.WriteTo returned io.EOF after Close. io.WriterTo has
no end-of-stream error, so io.Copy passes it on as a failure. net/http's
extra-bytes check on a request body (io.Copy(io.Discard, body)) can run
after the SDK has closed the body, and the io.EOF then makes net/http close
the connection while the response is still being read.

Fixes aws#751
@AzarudeenshariffA
AzarudeenshariffA force-pushed the fix-writeto-eof-after-close branch from 3c70b67 to af40147 Compare November 14, 2026 14:06
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.

safeWriteToReadCloser.WriteTo returns io.EOF after Close, which makes net/http close the connection under the response

1 participant