Skip to content

Commit

Permalink
compress/gzip: always close bodyReader in Example_compressingReader
Browse files Browse the repository at this point in the history
For golang#53362
Fixes golang#53414

Change-Id: I352164e70c136eed210c7ee4ceba5dc631f81f94
Reviewed-on: https://go-review.googlesource.com/c/go/+/412955
Auto-Submit: Ian Lance Taylor <[email protected]>
Reviewed-by: Joseph Tsai <[email protected]>
Reviewed-by: Ian Lance Taylor <[email protected]>
Run-TryBot: Ian Lance Taylor <[email protected]>
Reviewed-by: Alex Rakoczy <[email protected]>
TryBot-Result: Gopher Robot <[email protected]>
  • Loading branch information
ianlancetaylor authored and gopherbot committed Jun 22, 2022
1 parent 606c6c3 commit 6bad7e8
Showing 1 changed file with 4 additions and 1 deletion.
5 changes: 4 additions & 1 deletion src/compress/gzip/example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,10 @@ func Example_compressingReader() {
// httpWriter is the body of the HTTP request, as an io.Writer.
bodyReader, httpWriter := io.Pipe()

// Make sure that bodyReader is always closed, so that the
// goroutine below will always exit.
defer bodyReader.Close()

// gzipWriter compresses data to httpWriter.
gzipWriter := gzip.NewWriter(httpWriter)

Expand Down Expand Up @@ -197,7 +201,6 @@ func Example_compressingReader() {

// Note that passing req to http.Client.Do promises that it
// will close the body, in this case bodyReader.
// That ensures that the goroutine will exit.
resp, err := ts.Client().Do(req)
if err != nil {
log.Fatal(err)
Expand Down

0 comments on commit 6bad7e8

Please sign in to comment.