From: Damien Neil Date: Thu, 25 Mar 2021 18:11:03 +0000 (-0700) Subject: net/http/httptest: wait for user ConnState hooks X-Git-Tag: go1.17beta1~967 X-Git-Url: http://www.git.cypherpunks.su/?a=commitdiff_plain;h=5cec8b85e5dc75ef21b62efb6bd93f9007385e34;p=gostls13.git net/http/httptest: wait for user ConnState hooks Ensure that user ConnState callbacks have completed before returning from (*httptest.Server).Close. Fixes: #37510 Fixes: #37505 Fixes: #45237 Change-Id: I8fe7baa089fbe4f3836bf6ae9767c7b1270d1331 Reviewed-on: https://go-review.googlesource.com/c/go/+/304829 Trust: Damien Neil Run-TryBot: Damien Neil Reviewed-by: Bryan C. Mills --- diff --git a/src/net/http/httptest/server.go b/src/net/http/httptest/server.go index 65165d9eb3..a02a6d64c3 100644 --- a/src/net/http/httptest/server.go +++ b/src/net/http/httptest/server.go @@ -316,6 +316,13 @@ func (s *Server) wrap() { s.Config.ConnState = func(c net.Conn, cs http.ConnState) { s.mu.Lock() defer s.mu.Unlock() + + // Keep Close from returning until the user's ConnState hook + // (if any) finishes. Without this, the call to forgetConn + // below might send the count to 0 before we run the hook. + s.wg.Add(1) + defer s.wg.Done() + switch cs { case http.StateNew: s.wg.Add(1)