Skip to content

Commit

Permalink
fix(p2p): disable sending message when not ready to prevent busy loop (
Browse files Browse the repository at this point in the history
…#5032)

Signed-off-by: Shanin Roman <[email protected]>
  • Loading branch information
Erigara authored Sep 5, 2024
1 parent 510dc37 commit 995da4e
Showing 1 changed file with 11 additions and 1 deletion.
12 changes: 11 additions & 1 deletion p2p/src/peer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -261,7 +261,12 @@ mod run {
idle_interval.reset();
ping_interval.reset();
}
result = message_sender.send() => {
// `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.");
break;
Expand Down Expand Up @@ -427,6 +432,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
Expand Down

0 comments on commit 995da4e

Please sign in to comment.