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:
parent
019edc9035
commit
ddc3c3ab35
@ -643,11 +643,15 @@ out:
|
|||||||
// handleListPeers sends a lice of all currently active peers to the original
|
// handleListPeers sends a lice of all currently active peers to the original
|
||||||
// caller.
|
// caller.
|
||||||
func (s *server) handleListPeers(msg *listPeersMsg) {
|
func (s *server) handleListPeers(msg *listPeersMsg) {
|
||||||
|
s.peersMtx.RLock()
|
||||||
|
|
||||||
peers := make([]*peer, 0, len(s.peersByID))
|
peers := make([]*peer, 0, len(s.peersByID))
|
||||||
for _, peer := range s.peersByID {
|
for _, peer := range s.peersByID {
|
||||||
peers = append(peers, peer)
|
peers = append(peers, peer)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
s.peersMtx.RUnlock()
|
||||||
|
|
||||||
msg.resp <- peers
|
msg.resp <- peers
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -795,7 +799,7 @@ func (s *server) OpenChannel(peerID int32, nodeKey *btcec.PublicKey,
|
|||||||
|
|
||||||
// Peers returns a slice of all active peers.
|
// Peers returns a slice of all active peers.
|
||||||
func (s *server) Peers() []*peer {
|
func (s *server) Peers() []*peer {
|
||||||
resp := make(chan []*peer)
|
resp := make(chan []*peer, 1)
|
||||||
|
|
||||||
s.queries <- &listPeersMsg{resp}
|
s.queries <- &listPeersMsg{resp}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user