From 448089f452025ec8c655532c758ea6ca9520b6ae Mon Sep 17 00:00:00 2001 From: tv Date: Sun, 22 Nov 2026 12:00:44 +0000 Subject: libp2p: room for inbound streams that come in at once libp2p-stream hands each inbound stream to the consumer through a channel with room for one (Control::accept, mpsc::channel(0), try_send): one arriving while another waits is dropped, unread, logged at debug under libp2p's target alone. Messages sent to a node at once were lost but one, and a proof is two since wire versions side by side (SPEAKS, then WHO): a joiner's WHO went unanswered until RESOLVE_TIMEOUT, 10 s, and nodes_over_libp2p failed now and then. The transport accepts with Control::accept_with_buffer, with room for as many streams as it reads at once (WAITING, READS + STRANGER_READS): beyond them, one is dropped still. messages_sent_at_once_all_come_in sends 30 at once from three peers; without this, 3 to 7 came in. --- CHANGELOG.md | 5 +++++ src/transport/libp2p/mod.rs | 6 +++++- src/transport/libp2p/tests.rs | 37 +++++++++++++++++++++++++++++++++++++ 3 files changed, 47 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index f01a619..560c4af 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,11 @@ it from the release before ([`docs/operations.md`](docs/operations.md), ## Unreleased +* Over libp2p, messages sent to a node at once all come in: of their streams, all but + one were dropped unread, and a joining node's first proof of who it is, two of them, + went unanswered for 10 seconds ([`src/transport/libp2p/mod.rs`](src/transport/libp2p/mod.rs); + [`Cargo.toml`](Cargo.toml), libp2p-stream from a fork until a release has the fix). + * Before 1.0.0, versions read as Cargo reads them: of `0.Y.Z`, a release every node upgrades to together raises `Y`, any other raises `Z`; `scripts/release` refuses a flag day that doesn't ([`docs/operations.md`](docs/operations.md), diff --git a/src/transport/libp2p/mod.rs b/src/transport/libp2p/mod.rs index 24f5f34..2e3e897 100644 --- a/src/transport/libp2p/mod.rs +++ b/src/transport/libp2p/mod.rs @@ -155,6 +155,10 @@ const READS: usize = 32; /// Of peers not admitted (`transport::Admission`), apart: their streams never take /// members' turns. One more is refused, unread (a peer's HELLO is sent again). const STRANGER_READS: usize = 8; +/// Inbound streams waiting for the consumer to take them (`libp2p_stream`), as many as it +/// reads at once: one more is dropped unread, as streams that come in at once otherwise +/// are, all but one (a proof's SPEAKS and WHO, say). +const WAITING: usize = READS + STRANGER_READS; /// Addresses kept per peer, and peers with addresses at most. const ADDRESSES_PER_PEER: usize = 8; /// Addresses known not to be a peer's (`Book::wrong`), per peer at most. @@ -270,7 +274,7 @@ impl Libp2pTransport { let incoming = if config.continuum { Some( control - .accept(PROTOCOL) + .accept_with_buffer(PROTOCOL, WAITING) .map_err(|error| anyhow!("Accepting {PROTOCOL}: {error}"))?, ) } else { diff --git a/src/transport/libp2p/tests.rs b/src/transport/libp2p/tests.rs index 3ecd67a..93f251f 100644 --- a/src/transport/libp2p/tests.rs +++ b/src/transport/libp2p/tests.rs @@ -121,6 +121,43 @@ async fn messages_both_ways_over_one_connection() { assert_eq!(stats.connections.relayed, 0); } +/// Messages sent to a node at once, from several peers, all come in: none of their streams +/// is dropped while another waits to be read (`WAITING`). +#[tokio::test(flavor = "multi_thread", worker_threads = 4)] +async fn messages_sent_at_once_all_come_in() { + const PEERS: usize = 3; + const EACH: usize = 10; + let (a, a_address) = memory_node(); + let a = Arc::new(a); + let mut at_a = received(Arc::clone(&a)); + let mut senders = Vec::new(); + for _ in 0..PEERS { + let (sender, _) = memory_node(); + assert_eq!( + sender.resolve_seed(&a_address.to_string()).await.unwrap(), + a.local_id() + ); + senders.push(Arc::new(sender)); + } + let sends = senders.iter().flat_map(|sender| { + (0..EACH).map(|_| { + let (sender, to) = (Arc::clone(sender), a.local_id().to_owned()); + tokio::spawn(async move { sender.send_message(&to, &status()).await }) + }) + }); + for sent in futures::future::join_all(sends.collect::>()).await { + sent.unwrap().unwrap(); + } + for n in 0..PEERS * EACH { + let got = tokio::time::timeout(Duration::from_secs(5), at_a.recv()).await; + assert!( + matches!(got, Ok(Some(_))), + "{n} of {} came in", + PEERS * EACH + ); + } +} + #[tokio::test] async fn a_seed_without_a_peer_id_is_connected_to_to_find_out() { let (a, a_address) = memory_node(); -- cgit v1.3.1