server: ensure handleListPeers is threadsafe

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.
This commit is contained in:
Olaoluwa Osuntokun 2017-01-24 16:52:44 -08:00
parent 019edc9035
commit ddc3c3ab35
No known key found for this signature in database
GPG Key ID: 9CC5B105D03521A2

@ -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}