]> Cypherpunks repositories - gostls13.git/commitdiff
compress/gzip: always close bodyReader in Example_compressingReader
authorIan Lance Taylor <iant@golang.org>
Fri, 17 Jun 2022 20:38:07 +0000 (13:38 -0700)
committerGopher Robot <gobot@golang.org>
Wed, 22 Jun 2022 16:24:14 +0000 (16:24 +0000)
For #53362
Fixes #53414

Change-Id: I352164e70c136eed210c7ee4ceba5dc631f81f94
Reviewed-on: https://go-review.googlesource.com/c/go/+/412955
Auto-Submit: Ian Lance Taylor <iant@google.com>
Reviewed-by: Joseph Tsai <joetsai@digital-static.net>
Reviewed-by: Ian Lance Taylor <iant@google.com>
Run-TryBot: Ian Lance Taylor <iant@google.com>
Reviewed-by: Alex Rakoczy <alex@golang.org>
TryBot-Result: Gopher Robot <gobot@golang.org>

src/compress/gzip/example_test.go

index 27aae152d400a9431fbcf9ed5587741b5d424548..1ba4080ea0df8052c664d37fbc945045934f0642 100644 (file)
@@ -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)