From edbcb88d45ec5912eabd1b8502af9eaf48dfdd99 Mon Sep 17 00:00:00 2001 From: Richard Liu Date: Mon, 10 Dec 2018 01:44:57 -0800 Subject: [PATCH] fix p2p error related to beacon chain (#142) * fix p2p error related to beacon chain * add log --- p2p/host/hostv1/hostv1.go | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/p2p/host/hostv1/hostv1.go b/p2p/host/hostv1/hostv1.go index f9976e6f0..1144a7672 100644 --- a/p2p/host/hostv1/hostv1.go +++ b/p2p/host/hostv1/hostv1.go @@ -13,14 +13,14 @@ import ( type HostV1 struct { self p2p.Peer listener net.Listener - quit chan bool + quit chan struct{} } // New creates a HostV1 func New(self p2p.Peer) *HostV1 { h := &HostV1{ self: self, - quit: make(chan bool, 1), + quit: make(chan struct{}, 1), } return h } @@ -46,12 +46,14 @@ func (host *HostV1) BindHandlerAndServe(handler p2p.StreamHandler) { } backoff := p2p.NewExpBackoff(250*time.Millisecond, 15*time.Second, 2.0) for { // Keep listening + conn, err := host.listener.Accept() select { case <-host.quit: + // If we've already received quit signal, simply ignore the error and return + log.Info("Quit host", "addr", net.JoinHostPort(host.self.IP, host.self.Port)) return default: { - conn, err := host.listener.Accept() if err != nil { log.Error("Error listening on port.", "port", port, "err", err) @@ -94,6 +96,6 @@ func (host *HostV1) SendMessage(peer p2p.Peer, message []byte) (err error) { // Close closes the host func (host *HostV1) Close() error { - host.quit <- true + host.quit <- struct{}{} return host.listener.Close() }