src/net/http/httptest/server.go | 7 +++++++ diff --git a/src/net/http/httptest/server.go b/src/net/http/httptest/server.go index 65165d9eb3272a7fcaf71daca0e6575681abcf80..a02a6d64c39882c1a32508ba581211f646b6a82f 100644 --- a/src/net/http/httptest/server.go +++ b/src/net/http/httptest/server.go @@ -316,6 +316,13 @@ oldHook := s.Config.ConnState 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)