From ddc3c3ab35ceb8c6308441e57559b8d0c648fb09 Mon Sep 17 00:00:00 2001 From: Olaoluwa Osuntokun Date: Tue, 24 Jan 2017 16:52:44 -0800 Subject: [PATCH] server: ensure handleListPeers is threadsafe MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Before this commit there was the possibility of a race occurring between a call to the “lispers” cli command and the normal operation of peers being connected and disconnected. With this commit, we now ensure such a race doesn’t occur by properly acquiring the lock for peersByID before accessing it. --- server.go | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/server.go b/server.go index 4c2fb007..d207f2e8 100644 --- a/server.go +++ b/server.go @@ -643,11 +643,15 @@ out: // handleListPeers sends a lice of all currently active peers to the original // caller. func (s *server) handleListPeers(msg *listPeersMsg) { + s.peersMtx.RLock() + peers := make([]*peer, 0, len(s.peersByID)) for _, peer := range s.peersByID { peers = append(peers, peer) } + s.peersMtx.RUnlock() + msg.resp <- peers } @@ -795,7 +799,7 @@ func (s *server) OpenChannel(peerID int32, nodeKey *btcec.PublicKey, // Peers returns a slice of all active peers. func (s *server) Peers() []*peer { - resp := make(chan []*peer) + resp := make(chan []*peer, 1) s.queries <- &listPeersMsg{resp}