network: rework shutdown sequence of Server

Close transport and disconnect peers right in the Shutdown(), so that no new
connections would be accepted and so that all the peers would be disconnected
correctly (avoiding the same deadlock as in e2116e4c3f).
This commit is contained in:
Roman Khimov 2020-02-24 15:54:18 +03:00
parent 77624a8847
commit e213e69a33

View file

@ -183,8 +183,12 @@ func (s *Server) Start(errChan chan error) {
// Shutdown disconnects all peers and stops listening. // Shutdown disconnects all peers and stops listening.
func (s *Server) Shutdown() { func (s *Server) Shutdown() {
s.log.Info("shutting down server", zap.Int("peers", s.PeerCount())) s.log.Info("shutting down server", zap.Int("peers", s.PeerCount()))
s.bQueue.discard() s.transport.Close()
s.discovery.Close() s.discovery.Close()
for p := range s.peers {
p.Disconnect(errServerShutdown)
}
s.bQueue.discard()
close(s.quit) close(s.quit)
} }
@ -212,10 +216,6 @@ func (s *Server) run() {
} }
select { select {
case <-s.quit: case <-s.quit:
s.transport.Close()
for p := range s.peers {
p.Disconnect(errServerShutdown)
}
return return
case p := <-s.register: case p := <-s.register:
s.lock.Lock() s.lock.Lock()