protocols/identify: Fix race condition in discover_peer_after_disconnect #2744
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Description
Summary of the plot of the
discover_peer_after_disconnect
test:swarm2
connects toswarm1
.swarm2
requests an identify response fromswarm1
.swarm1
sends the response toswarm2
.swarm2
disconnects fromswarm1
.swarm2
tries to disconnect.Problem
libp2p-identify
setsKeepAlive::No
when it identified the remote. Thusswarm1
mightidentify
before
swarm2identified
swarm1.
swarm1then sets
KeepAlive::Noand thus closes the connection to
swarm2before
swarm2identified
swarm1. In such case the unit test
discover_peer_after_disconnect hangs indefinitely.Solution
Add an initial delay to
swarm1
requesting an identification fromswarm2
, thus ensuringswarm2
is always able to identify
swarm1
.Links to any relevant issues
Fixes #2743
Open Questions
We could as well introduce
libp2p-ping
and setPingConfig::with_keep_alive
. I think the fix here is sufficient though.As another alternative, instead of setting
KeepAlive::No
once we identified the remote, we could as well setKeepAlive::Until(Instant::now() + Duration::from_secs(10))
, thus giving the remote a chance to identify us.Change checklist