From 5b3d9fca9463cb9da1cac20187cdc506a09ad6cc Mon Sep 17 00:00:00 2001 From: Shanin Roman Date: Wed, 4 Sep 2024 17:17:29 +0300 Subject: [PATCH 1/2] fix(p2p): disable sending message when not ready to prevent busy loop Signed-off-by: Shanin Roman --- p2p/src/peer.rs | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/p2p/src/peer.rs b/p2p/src/peer.rs index 109bb183b3e..9b8ff2c0bf4 100644 --- a/p2p/src/peer.rs +++ b/p2p/src/peer.rs @@ -261,7 +261,7 @@ mod run { idle_interval.reset(); ping_interval.reset(); } - result = message_sender.send() => { + result = message_sender.send(), if message_sender.ready() => { if let Err(error) = result { iroha_logger::error!(%error, "Failed to send message to peer."); break; @@ -427,6 +427,11 @@ mod run { } Ok(()) } + + /// Check if message sender has data ready to be sent. + fn ready(&self) -> bool { + !self.queue.is_empty() + } } /// Either message or ping From 7a8ba1bdbcd070647ffec78d09236b19aae0adda Mon Sep 17 00:00:00 2001 From: Shanin Roman <40040452+Erigara@users.noreply.github.com> Date: Thu, 5 Sep 2024 11:56:47 +0300 Subject: [PATCH 2/2] refacrot(p2p): add clarification comment for `message_sender.send()` MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: ⭐️NINIKA⭐️ Signed-off-by: Shanin Roman <40040452+Erigara@users.noreply.github.com> --- p2p/src/peer.rs | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/p2p/src/peer.rs b/p2p/src/peer.rs index 9b8ff2c0bf4..71e0c59094e 100644 --- a/p2p/src/peer.rs +++ b/p2p/src/peer.rs @@ -261,6 +261,11 @@ mod run { idle_interval.reset(); ping_interval.reset(); } + // `message_sender.send()` is safe to be cancelled, it won't advance the queue or write anything if another branch completes first. + // + // We need to conditionally disable it in case there is no data is to be sent, otherwise `message_sender.send()` will complete immediately + // + // The only source of data to be sent is other branches of this loop, so we do not need any async waiting mechanism for waiting for readiness. result = message_sender.send(), if message_sender.ready() => { if let Err(error) = result { iroha_logger::error!(%error, "Failed to send message to peer.");