Skip to content

Commit

Permalink
p2p: wait for listener goroutines on shutdown (ethereum#20569)
Browse files Browse the repository at this point in the history
* p2p: wait for goroutine exit, fixes ethereum#20558

* p2p: wait for all slots on exit

Co-authored-by: Martin Holst Swende <[email protected]>
  • Loading branch information
2 people authored and karalabe committed Jan 16, 2020
1 parent 1ee754b commit fcafa0b
Showing 1 changed file with 11 additions and 1 deletion.
12 changes: 11 additions & 1 deletion p2p/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -864,9 +864,9 @@ func (srv *Server) maxDialedConns() int {
// listenLoop runs in its own goroutine and accepts
// inbound connections.
func (srv *Server) listenLoop() {
defer srv.loopWG.Done()
srv.log.Debug("TCP listener up", "addr", srv.listener.Addr())

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

// Wait for slots to be returned on exit. This ensures all connection goroutines
// are down before listenLoop returns.
defer srv.loopWG.Done()
defer func() {
for i := 0; i < cap(slots); i++ {
<-slots
}
}()

for {
// Wait for a free slot before accepting.
<-slots
Expand All @@ -891,6 +900,7 @@ func (srv *Server) listenLoop() {
continue
} else if err != nil {
srv.log.Debug("Read error", "err", err)
slots <- struct{}{}
return
}
break
Expand Down

0 comments on commit fcafa0b

Please sign in to comment.