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

Commit 8dd1682

Browse files
committed
fix(peermanager): fix disconnect race
Keep all of disconnection in a mutex
1 parent 62715b2 commit 8dd1682

File tree

1 file changed

+6
-20
lines changed

1 file changed

+6
-20
lines changed

peermanager/peermanager.go

+6-20
Original file line numberDiff line numberDiff line change
@@ -77,20 +77,19 @@ func (pm *PeerManager) Connected(p peer.ID, initialEntries []*wantlist.Entry) {
7777

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

82-
if !ok {
83-
// TODO: log error?
83+
if !ok || pq.RefDecrement() {
84+
pm.peerQueuesLk.Unlock()
8485
return
8586
}
8687

87-
if pq.RefDecrement() {
88-
return
89-
}
88+
delete(pm.peerQueues, p)
89+
pm.peerQueuesLk.Unlock()
9090

9191
pq.Shutdown()
9292

93-
pm.remove(p)
9493
}
9594

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

111-
func (pm *PeerManager) get(p peer.ID) (PeerQueue, bool) {
112-
pm.peerQueuesLk.RLock()
113-
pq, ok := pm.peerQueues[p]
114-
pm.peerQueuesLk.RUnlock()
115-
return pq, ok
116-
}
117-
118110
func (pm *PeerManager) getOrCreate(p peer.ID) PeerQueue {
119111
pm.peerQueuesLk.Lock()
120112
pq, ok := pm.peerQueues[p]
@@ -127,12 +119,6 @@ func (pm *PeerManager) getOrCreate(p peer.ID) PeerQueue {
127119
return pq
128120
}
129121

130-
func (pm *PeerManager) remove(p peer.ID) {
131-
pm.peerQueuesLk.Lock()
132-
delete(pm.peerQueues, p)
133-
pm.peerQueuesLk.Unlock()
134-
}
135-
136122
func (pm *PeerManager) iterate(iterateFn func(peer.ID, PeerQueue)) {
137123
pm.peerQueuesLk.RLock()
138124
for p, pq := range pm.peerQueues {

0 commit comments

Comments
 (0)