Skip to content

Commit

Permalink
net/http: avoid leaking goroutines when TestServerGracefulClose retries
Browse files Browse the repository at this point in the history
If the call to ReadString returns an error, the closure in
testServerGracefulClose will return an error and retry the test with a
longer timeout. If that happens, we need to wait for the conn.Write
goroutine to complete so that we don't leak connections across tests.

Updates #57084.
Fixes #62643.

Change-Id: Ia86c1bbd0a5e5d0aeccf4dfeb994c19d1fb10b00
Reviewed-on: https://go-review.googlesource.com/c/go/+/528398
Auto-Submit: Bryan Mills <[email protected]>
Reviewed-by: Than McIntosh <[email protected]>
TryBot-Result: Gopher Robot <[email protected]>
Run-TryBot: Bryan Mills <[email protected]>
Reviewed-by: Damien Neil <[email protected]>
  • Loading branch information
Bryan C. Mills authored and gopherbot committed Sep 15, 2023
1 parent 08cdfd0 commit 561a507
Showing 1 changed file with 8 additions and 5 deletions.
13 changes: 8 additions & 5 deletions src/net/http/serve_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3135,12 +3135,19 @@ func testServerGracefulClose(t *testing.T, mode testMode) {
if err != nil {
return err
}
defer conn.Close()
writeErr := make(chan error)
go func() {
_, err := conn.Write(req)
writeErr <- err
}()
defer func() {
conn.Close()
// Wait for write to finish. This is a broken pipe on both
// Darwin and Linux, but checking this isn't the point of
// the test.
<-writeErr
}()

br := bufio.NewReader(conn)
lineNum := 0
for {
Expand All @@ -3156,10 +3163,6 @@ func testServerGracefulClose(t *testing.T, mode testMode) {
t.Errorf("Response line = %q; want a 401", line)
}
}
// Wait for write to finish. This is a broken pipe on both
// Darwin and Linux, but checking this isn't the point of
// the test.
<-writeErr
return nil
})
}
Expand Down

0 comments on commit 561a507

Please sign in to comment.