From 84e150a2dffaab3b6c7d5ae3a5eb701d70206516 Mon Sep 17 00:00:00 2001 From: Eugene Kim Date: Mon, 4 Feb 2019 18:42:39 -0800 Subject: [PATCH] Handle peer channel closure gracefully MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Without this, the foundPeers() goroutine will enter a tight infinite loop when the DHT query ends and the peer channel gets closed, as “<-s.peerChan” will immediately return a zero-valued PeerInfo. --- api/service/discovery/service.go | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/api/service/discovery/service.go b/api/service/discovery/service.go index f65ab440c..fc02b2b01 100644 --- a/api/service/discovery/service.go +++ b/api/service/discovery/service.go @@ -80,7 +80,11 @@ func (s *Service) StopService() { func (s *Service) foundPeers() { for { select { - case peer := <-s.peerChan: + case peer, ok := <-s.peerChan: + if !ok { + log.Debug("end of info", "peer", peer.ID) + return + } if peer.ID != s.Host.GetP2PHost().ID() && len(peer.ID) > 0 { log.Debug("Found Peer", "peer", peer.ID, "addr", peer.Addrs, "len", len(peer.ID)) p := p2p.Peer{PeerID: peer.ID, Addrs: peer.Addrs}