From: Ian Lance Taylor Date: Fri, 17 Jun 2022 20:38:07 +0000 (-0700) Subject: compress/gzip: always close bodyReader in Example_compressingReader X-Git-Tag: go1.19rc1~82 X-Git-Url: http://www.git.cypherpunks.su/?a=commitdiff_plain;h=6bad7e82430bb1eb927a2901f44f9664637db27d;p=gostls13.git compress/gzip: always close bodyReader in Example_compressingReader For #53362 Fixes #53414 Change-Id: I352164e70c136eed210c7ee4ceba5dc631f81f94 Reviewed-on: https://go-review.googlesource.com/c/go/+/412955 Auto-Submit: Ian Lance Taylor Reviewed-by: Joseph Tsai Reviewed-by: Ian Lance Taylor Run-TryBot: Ian Lance Taylor Reviewed-by: Alex Rakoczy TryBot-Result: Gopher Robot --- diff --git a/src/compress/gzip/example_test.go b/src/compress/gzip/example_test.go index 27aae152d4..1ba4080ea0 100644 --- a/src/compress/gzip/example_test.go +++ b/src/compress/gzip/example_test.go @@ -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) @@ -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)