diff --git a/swarm.go b/swarm.go index cc7eb978a6d894b8a73bc77f7627439fe692d815..5366bdcc6f7ba12d45fdb9e5b9fd5c7f21a0da5f 100644 --- a/swarm.go +++ b/swarm.go @@ -116,6 +116,11 @@ func NewSwarm(ctx context.Context, local peer.ID, peers peerstore.Peerstore, bwc } func (s *Swarm) teardown() error { + // Wait for the context to be canceled. + // This allows other parts of the swarm to detect that we're shutting + // down. + <-s.ctx.Done() + // Prevents new connections and/or listeners from being added to the swarm. s.listeners.Lock() diff --git a/swarm_listen.go b/swarm_listen.go index 570acfedae9686768967ed1e842eadbd17b405a1..f1cfa9e7f1a3adb4050181b5227588555e89f7c1 100644 --- a/swarm_listen.go +++ b/swarm_listen.go @@ -79,6 +79,7 @@ func (s *Swarm) AddListenAddr(a ma.Multiaddr) error { c, err := list.Accept() if err != nil { if s.ctx.Err() == nil { + // only log if the swarm is still running. log.Errorf("swarm listener accept error: %s", err) } return @@ -88,9 +89,13 @@ func (s *Swarm) AddListenAddr(a ma.Multiaddr) error { go func() { defer s.refs.Done() _, err := s.addConn(c, network.DirInbound) - if err != nil { - // Probably just means that the swarm has been closed. - log.Warningf("add conn failed: ", err) + switch err { + case nil: + case ErrSwarmClosed: + // ignore. + return + default: + log.Warningf("add conn %s failed: ", err) return } }()