diff --git a/protocols/kad/CHANGELOG.md b/protocols/kad/CHANGELOG.md index 6903932c..db0b9f0d 100644 --- a/protocols/kad/CHANGELOG.md +++ b/protocols/kad/CHANGELOG.md @@ -11,7 +11,10 @@ - Expose kbucket range on `KademliaEvent::RoutingUpdated` (see [PR 2087]). +- Remove false `debug_assert` on `connected_peers` (see [PR 2120]). + [PR 2087]: https://github.com/libp2p/rust-libp2p/pull/2087 +[PR 2120]: https://github.com/libp2p/rust-libp2p/pull/2120 # 0.30.0 [2021-04-13] diff --git a/protocols/kad/src/behaviour.rs b/protocols/kad/src/behaviour.rs index 56601b39..6d01950a 100644 --- a/protocols/kad/src/behaviour.rs +++ b/protocols/kad/src/behaviour.rs @@ -1042,15 +1042,23 @@ where )); }, kbucket::InsertResult::Pending { disconnected } => { - debug_assert!(!self.connected_peers.contains(disconnected.preimage())); let address = addresses.first().clone(); self.queued_events.push_back(NetworkBehaviourAction::GenerateEvent( KademliaEvent::PendingRoutablePeer { peer, address } )); - self.queued_events.push_back(NetworkBehaviourAction::DialPeer { - peer_id: disconnected.into_preimage(), - condition: DialPeerCondition::Disconnected - }) + + // `disconnected` might already be in the process of re-connecting. + // In other words `disconnected` might have already re-connected but + // is not yet confirmed to support the Kademlia protocol via + // [`KademliaHandlerEvent::ProtocolConfirmed`]. + // + // Only try dialing peer if not currently connected. + if !self.connected_peers.contains(disconnected.preimage()) { + self.queued_events.push_back(NetworkBehaviourAction::DialPeer { + peer_id: disconnected.into_preimage(), + condition: DialPeerCondition::Disconnected + }) + } }, } }