Skip to content
This repository has been archived by the owner on Feb 1, 2023. It is now read-only.

Commit

Permalink
fix(peermanager): fix disconnect race
Browse files Browse the repository at this point in the history
Keep all of disconnection in a mutex
  • Loading branch information
hannahhoward committed Feb 20, 2019
1 parent 62715b2 commit 8dd1682
Showing 1 changed file with 6 additions and 20 deletions.
26 changes: 6 additions & 20 deletions peermanager/peermanager.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,20 +77,19 @@ func (pm *PeerManager) Connected(p peer.ID, initialEntries []*wantlist.Entry) {

// Disconnected is called to remove a peer from the pool.
func (pm *PeerManager) Disconnected(p peer.ID) {
pq, ok := pm.get(p)
pm.peerQueuesLk.Lock()
pq, ok := pm.peerQueues[p]

if !ok {
// TODO: log error?
if !ok || pq.RefDecrement() {
pm.peerQueuesLk.Unlock()
return
}

if pq.RefDecrement() {
return
}
delete(pm.peerQueues, p)
pm.peerQueuesLk.Unlock()

pq.Shutdown()

pm.remove(p)
}

// SendMessage is called to send a message to all or some peers in the pool;
Expand All @@ -108,13 +107,6 @@ func (pm *PeerManager) SendMessage(entries []*bsmsg.Entry, targets []peer.ID, fr
}
}

func (pm *PeerManager) get(p peer.ID) (PeerQueue, bool) {
pm.peerQueuesLk.RLock()
pq, ok := pm.peerQueues[p]
pm.peerQueuesLk.RUnlock()
return pq, ok
}

func (pm *PeerManager) getOrCreate(p peer.ID) PeerQueue {
pm.peerQueuesLk.Lock()
pq, ok := pm.peerQueues[p]
Expand All @@ -127,12 +119,6 @@ func (pm *PeerManager) getOrCreate(p peer.ID) PeerQueue {
return pq
}

func (pm *PeerManager) remove(p peer.ID) {
pm.peerQueuesLk.Lock()
delete(pm.peerQueues, p)
pm.peerQueuesLk.Unlock()
}

func (pm *PeerManager) iterate(iterateFn func(peer.ID, PeerQueue)) {
pm.peerQueuesLk.RLock()
for p, pq := range pm.peerQueues {
Expand Down

0 comments on commit 8dd1682

Please sign in to comment.