p2p: wait for goroutine exit, fixes #20558

This commit is contained in:
Martin Holst Swende 2020-01-15 09:07:52 +01:00
parent feda78e052
commit 5ba0dd8880
No known key found for this signature in database
GPG key ID: 683B438C05A5DDF0

View file

@ -864,7 +864,6 @@ 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())
tokens := defaultMaxPendingPeers
@ -872,6 +871,12 @@ func (srv *Server) listenLoop() {
tokens = srv.MaxPendingPeers
}
slots := make(chan struct{}, tokens)
defer func() {
// Wait for a slot. This is to wait for any goroutine(s) doing srv.SetupConn
// to complete before exiting
<-slots
srv.loopWG.Done()
}()
for i := 0; i < tokens; i++ {
slots <- struct{}{}
}