From 9d93cbe5480eb660c3f211b50d2d30f8d3f6f089 Mon Sep 17 00:00:00 2001 From: Alexandru Vasile Date: Wed, 23 Oct 2024 16:50:20 +0300 Subject: [PATCH] ping: Fix memory leak of unremoved `pending_opens` Signed-off-by: Alexandru Vasile --- src/protocol/libp2p/ping/mod.rs | 23 +++-------------------- 1 file changed, 3 insertions(+), 20 deletions(-) diff --git a/src/protocol/libp2p/ping/mod.rs b/src/protocol/libp2p/ping/mod.rs index 3d2b5ef16..efe6fad0b 100644 --- a/src/protocol/libp2p/ping/mod.rs +++ b/src/protocol/libp2p/ping/mod.rs @@ -32,7 +32,7 @@ use futures::{future::BoxFuture, stream::FuturesUnordered, StreamExt}; use tokio::sync::mpsc::Sender; use std::{ - collections::{HashMap, HashSet}, + collections::HashSet, time::{Duration, Instant}, }; @@ -72,9 +72,6 @@ pub(crate) struct Ping { /// Connected peers. peers: HashSet, - /// Pending outbound substreams. - pending_opens: HashMap, - /// Pending outbound substreams. pending_outbound: FuturesUnordered>>, @@ -89,7 +86,6 @@ impl Ping { service, tx: config.tx_event, peers: HashSet::new(), - pending_opens: HashMap::new(), pending_outbound: FuturesUnordered::new(), pending_inbound: FuturesUnordered::new(), _max_failures: config.max_failures, @@ -100,8 +96,7 @@ impl Ping { fn on_connection_established(&mut self, peer: PeerId) -> crate::Result<()> { tracing::trace!(target: LOG_TARGET, ?peer, "connection established"); - let substream_id = self.service.open_substream(peer)?; - self.pending_opens.insert(substream_id, peer); + self.service.open_substream(peer)?; self.peers.insert(peer); Ok(()) @@ -191,19 +186,7 @@ impl Ping { self.on_inbound_substream(peer, substream); } Direction::Outbound(substream_id) => { - match self.pending_opens.remove(&substream_id) { - Some(stored_peer) => { - debug_assert!(peer == stored_peer); - self.on_outbound_substream(peer, substream_id, substream); - } - None => { - tracing::warn!( - target: LOG_TARGET, - ?substream_id, - "outbound ping substream ID does not exist", - ); - } - } + self.on_outbound_substream(peer, substream_id, substream); } }, Some(_) => {}