Skip to content

Commit b28cdb3

Browse files
authored
protocols/identify: Fix race condition in discover_peer_after_disconnect (#2744)
**Summary** of the plot of the `discover_peer_after_disconnect` test: 1. `swarm2` connects to `swarm1`. 2. `swarm2` requests an identify response from `swarm1`. 3. `swarm1` sends the response to `swarm2`. 4. `swarm2` disconnects from `swarm1`. 5. `swarm2` tries to disconnect. **Problem** `libp2p-identify` sets `KeepAlive::No` when it identified the remote. Thus `swarm1` might identify` `swarm2` before `swarm2` identified `swarm1`. `swarm1` then sets `KeepAlive::No` and thus closes the connection to `swarm2` before `swarm2` identified `swarm1`. In such case the unit test `discover_peer_after_disconnect hangs indefinitely. **Solution** Add an initial delay to `swarm1` requesting an identification from `swarm2`, thus ensuring `swarm2` is always able to identify `swarm1`.
1 parent 6db5712 commit b28cdb3

File tree

1 file changed

+9
-1
lines changed

1 file changed

+9
-1
lines changed

protocols/identify/src/identify.rs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -518,6 +518,7 @@ mod tests {
518518
use libp2p::tcp::TcpConfig;
519519
use libp2p_core::{identity, muxing::StreamMuxerBox, transport, upgrade, PeerId, Transport};
520520
use libp2p_swarm::{Swarm, SwarmEvent};
521+
use std::time::Duration;
521522

522523
fn transport() -> (
523524
identity::PublicKey,
@@ -700,7 +701,14 @@ mod tests {
700701

701702
let mut swarm1 = {
702703
let (pubkey, transport) = transport();
703-
let protocol = Identify::new(IdentifyConfig::new("a".to_string(), pubkey.clone()));
704+
let protocol = Identify::new(
705+
IdentifyConfig::new("a".to_string(), pubkey.clone())
706+
// `swarm1` will set `KeepAlive::No` once it identified `swarm2` and thus
707+
// closes the connection. At this point in time `swarm2` might not yet have
708+
// identified `swarm1`. To give `swarm2` enough time, set an initial delay on
709+
// `swarm1`.
710+
.with_initial_delay(Duration::from_secs(10)),
711+
);
704712

705713
Swarm::new(transport, protocol, pubkey.to_peer_id())
706714
};

0 commit comments

Comments
 (0)