Skip to content

Commit fcafa0b

Browse files
fjlholiman
authored andcommitted
p2p: wait for listener goroutines on shutdown (#20569)
* p2p: wait for goroutine exit, fixes #20558 * p2p: wait for all slots on exit Co-authored-by: Martin Holst Swende <martin@swende.se>
1 parent 1ee754b commit fcafa0b

1 file changed

Lines changed: 11 additions & 1 deletion

File tree

‎p2p/server.go‎

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -864,9 +864,9 @@ func (srv *Server) maxDialedConns() int {
864864
// listenLoop runs in its own goroutine and accepts
865865
// inbound connections.
866866
func (srv *Server) listenLoop() {
867-
defer srv.loopWG.Done()
868867
srv.log.Debug("TCP listener up", "addr", srv.listener.Addr())
869868

869+
// The slots channel limits accepts of new connections.
870870
tokens := defaultMaxPendingPeers
871871
if srv.MaxPendingPeers > 0 {
872872
tokens = srv.MaxPendingPeers
@@ -876,6 +876,15 @@ func (srv *Server) listenLoop() {
876876
slots <- struct{}{}
877877
}
878878

879+
// Wait for slots to be returned on exit. This ensures all connection goroutines
880+
// are down before listenLoop returns.
881+
defer srv.loopWG.Done()
882+
defer func() {
883+
for i := 0; i < cap(slots); i++ {
884+
<-slots
885+
}
886+
}()
887+
879888
for {
880889
// Wait for a free slot before accepting.
881890
<-slots
@@ -891,6 +900,7 @@ func (srv *Server) listenLoop() {
891900
continue
892901
} else if err != nil {
893902
srv.log.Debug("Read error", "err", err)
903+
slots <- struct{}{}
894904
return
895905
}
896906
break

0 commit comments

Comments
 (0)