@@ -317,21 +317,17 @@ func (s *Server) wrap() {
317317 s .mu .Lock ()
318318 defer s .mu .Unlock ()
319319
320- // Keep Close from returning until the user's ConnState hook
321- // (if any) finishes. Without this, the call to forgetConn
322- // below might send the count to 0 before we run the hook.
323- s .wg .Add (1 )
324- defer s .wg .Done ()
325-
326320 switch cs {
327321 case http .StateNew :
328- s .wg .Add (1 )
329322 if _ , exists := s .conns [c ]; exists {
330323 panic ("invalid state transition" )
331324 }
332325 if s .conns == nil {
333326 s .conns = make (map [net.Conn ]http.ConnState )
334327 }
328+ // Add c to the set of tracked conns and increment it to the
329+ // waitgroup.
330+ s .wg .Add (1 )
335331 s .conns [c ] = cs
336332 if s .closed {
337333 // Probably just a socket-late-binding dial from
@@ -358,7 +354,14 @@ func (s *Server) wrap() {
358354 s .closeConn (c )
359355 }
360356 case http .StateHijacked , http .StateClosed :
361- s .forgetConn (c )
357+ // Remove c from the set of tracked conns and decrement it from the
358+ // waitgroup, unless it was previously removed.
359+ if _ , ok := s .conns [c ]; ok {
360+ delete (s .conns , c )
361+ // Keep Close from returning until the user's ConnState hook
362+ // (if any) finishes.
363+ defer s .wg .Done ()
364+ }
362365 }
363366 if oldHook != nil {
364367 oldHook (c , cs )
@@ -378,13 +381,3 @@ func (s *Server) closeConnChan(c net.Conn, done chan<- struct{}) {
378381 done <- struct {}{}
379382 }
380383}
381-
382- // forgetConn removes c from the set of tracked conns and decrements it from the
383- // waitgroup, unless it was previously removed.
384- // s.mu must be held.
385- func (s * Server ) forgetConn (c net.Conn ) {
386- if _ , ok := s .conns [c ]; ok {
387- delete (s .conns , c )
388- s .wg .Done ()
389- }
390- }
0 commit comments