]> Cypherpunks repositories - gostls13.git/commitdiff
net/http: avoid leaking io.Copy goroutines (and hijacked connections) in TestTranspor...
authorBryan C. Mills <bcmills@google.com>
Fri, 1 Dec 2023 21:27:43 +0000 (16:27 -0500)
committerGopher Robot <gobot@golang.org>
Fri, 1 Dec 2023 22:07:19 +0000 (22:07 +0000)
Fixes #64252 (maybe).

Change-Id: Iba2a403a9347be4206f14acb11591dc2eb7f9fb8
Reviewed-on: https://go-review.googlesource.com/c/go/+/546616
Reviewed-by: Damien Neil <dneil@google.com>
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Auto-Submit: Bryan Mills <bcmills@google.com>

src/net/http/transport_test.go

index 8c09de70ff5e2b6b4618a7d4613e76e36e6e1334..3057024b764baa04791e6a68ad14ad1480b0f905 100644 (file)
@@ -3499,6 +3499,7 @@ func testTransportNoReuseAfterEarlyResponse(t *testing.T, mode testMode) {
                c net.Conn
        }
        var getOkay bool
+       var copying sync.WaitGroup
        closeConn := func() {
                sconn.Lock()
                defer sconn.Unlock()
@@ -3510,7 +3511,10 @@ func testTransportNoReuseAfterEarlyResponse(t *testing.T, mode testMode) {
                        }
                }
        }
-       defer closeConn()
+       defer func() {
+               closeConn()
+               copying.Wait()
+       }()
 
        ts := newClientServerTest(t, mode, HandlerFunc(func(w ResponseWriter, r *Request) {
                if r.Method == "GET" {
@@ -3522,7 +3526,12 @@ func testTransportNoReuseAfterEarlyResponse(t *testing.T, mode testMode) {
                sconn.c = conn
                sconn.Unlock()
                conn.Write([]byte("HTTP/1.1 200 OK\r\nContent-Length: 3\r\n\r\nfoo")) // keep-alive
-               go io.Copy(io.Discard, conn)
+
+               copying.Add(1)
+               go func() {
+                       io.Copy(io.Discard, conn)
+                       copying.Done()
+               }()
        })).ts
        c := ts.Client()