Skip to content

Commit 4ef5738

Browse files
authored
swarm/src/lib: Continue polling network when behaviour is blocked (#2304)
With #2248 a connection task `await`s sending an event to the behaviour before polling for new events from the behaviour [1]. When `Swarm::poll` is unable to deliver an event to a connection task it returns `Poll::Pending` even though (a) polling `Swarm::network` might be able to make progress (`network_not_ready` being `false`) and (b) it does not register a waker to be woken up [2]. In combination this can lead to a deadlock where a connection task waits to send an event to the behaviour and `Swarm::poll` returns `Poll::Pending` failing to send an event to the connection task, not registering a waiker in order to be polled again. With this commit `Swarm::poll` will only return `Poll::Pending`, when failing to deliver an event to a connection task, if the network is unable to make progress (i.e. `network_not_ready` being `true`). In the long-run `Swarm::poll` should likely be redesigned, prioritizing the behaviour over the network, given the former is the control plane and the latter potentially yields new work from the outside. [1]: https://github.com/libp2p/rust-libp2p/blob/ca1b7cf043b4264c69b19fe75de488330a7a1f2f/core/src/connection/pool/task.rs#L224-L232 [2]: https://github.com/libp2p/rust-libp2p/blob/ca1b7cf043b4264c69b19fe75de488330a7a1f2f/swarm/src/lib.rs#L756-L783
1 parent 9750951 commit 4ef5738

File tree

1 file changed

+20
-4
lines changed

1 file changed

+20
-4
lines changed

swarm/src/lib.rs

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -765,7 +765,11 @@ where
765765
if let Some(mut conn) = peer.connection(conn_id) {
766766
if let Some(event) = notify_one(&mut conn, event, cx) {
767767
this.pending_event = Some((peer_id, handler, event));
768-
return Poll::Pending;
768+
if network_not_ready {
769+
return Poll::Pending;
770+
} else {
771+
continue;
772+
}
769773
}
770774
}
771775
}
@@ -775,7 +779,11 @@ where
775779
{
776780
let handler = PendingNotifyHandler::Any(ids);
777781
this.pending_event = Some((peer_id, handler, event));
778-
return Poll::Pending;
782+
if network_not_ready {
783+
return Poll::Pending;
784+
} else {
785+
continue;
786+
}
779787
}
780788
}
781789
}
@@ -843,7 +851,11 @@ where
843851
if let Some(event) = notify_one(&mut conn, event, cx) {
844852
let handler = PendingNotifyHandler::One(connection);
845853
this.pending_event = Some((peer_id, handler, event));
846-
return Poll::Pending;
854+
if network_not_ready {
855+
return Poll::Pending;
856+
} else {
857+
continue;
858+
}
847859
}
848860
}
849861
}
@@ -854,7 +866,11 @@ where
854866
{
855867
let handler = PendingNotifyHandler::Any(ids);
856868
this.pending_event = Some((peer_id, handler, event));
857-
return Poll::Pending;
869+
if network_not_ready {
870+
return Poll::Pending;
871+
} else {
872+
continue;
873+
}
858874
}
859875
}
860876
}

0 commit comments

Comments
 (0)