Skip to content

Commit

Permalink
dht/routes: Fix potential deadlock in FindClosestPeers
Browse files Browse the repository at this point in the history
  • Loading branch information
losfair committed Jul 9, 2018
1 parent bcd3176 commit 10855ab
Showing 1 changed file with 2 additions and 2 deletions.
4 changes: 2 additions & 2 deletions dht/routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,8 @@ func (t *RoutingTable) FindClosestPeers(target peer.ID, count int) (peers []peer
peers = append(peers, e.Value.(peer.ID))
}

bucket.mutex.RUnlock()

for i := 1; len(peers) < count && (bucketID-i >= 0 || bucketID+i < len(t.self.PublicKey)*8); i++ {
if bucketID-i >= 0 {
other := t.Bucket(bucketID - i)
Expand All @@ -205,8 +207,6 @@ func (t *RoutingTable) FindClosestPeers(target peer.ID, count int) (peers []peer
}
}

bucket.mutex.RUnlock()

// Sort peers by XOR distance.
sort.Slice(peers, func(i, j int) bool {
left := peers[i].Xor(target)
Expand Down

0 comments on commit 10855ab

Please sign in to comment.