Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

tcp/websocket/quic: Fix cancel memory leak #272

Merged
merged 11 commits into from
Oct 30, 2024
88 changes: 62 additions & 26 deletions src/transport/quic/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,11 @@ use crate::{
PeerId,
};

use futures::{future::BoxFuture, stream::FuturesUnordered, Stream, StreamExt};
use futures::{
future::BoxFuture,
stream::{AbortHandle, FuturesUnordered},
Stream, StreamExt, TryFutureExt,
};
use multiaddr::{Multiaddr, Protocol};
use quinn::{ClientConfig, Connecting, Connection, Endpoint, IdleTimeout};

Expand Down Expand Up @@ -66,6 +70,25 @@ struct NegotiatedConnection {
connection: Connection,
}

#[derive(Debug)]
enum RawConnectionResult {
/// The first successful connection.
Connected {
connection_id: ConnectionId,
address: Multiaddr,
stream: NegotiatedConnection,
},

/// All connection attempts failed.
Failed {
connection_id: ConnectionId,
errors: Vec<(Multiaddr, DialError)>,
},

/// Future was canceled.
Canceled { connection_id: ConnectionId },
}

/// QUIC transport object.
pub(crate) struct QuicTransport {
/// Transport handle.
Expand All @@ -92,21 +115,15 @@ pub(crate) struct QuicTransport {
pending_open: HashMap<ConnectionId, (NegotiatedConnection, Litep2pEndpoint)>,

/// Pending raw, unnegotiated connections.
pending_raw_connections: FuturesUnordered<
BoxFuture<
'static,
Result<
(ConnectionId, Multiaddr, NegotiatedConnection),
(ConnectionId, Vec<(Multiaddr, DialError)>),
>,
>,
>,
pending_raw_connections: FuturesUnordered<BoxFuture<'static, RawConnectionResult>>,

/// Opened raw connection, waiting for approval/rejection from `TransportManager`.
opened_raw: HashMap<ConnectionId, (NegotiatedConnection, Multiaddr)>,

/// Canceled raw connections.
canceled: HashSet<ConnectionId>,

cancel_futures: HashMap<ConnectionId, AbortHandle>,
}

impl QuicTransport {
Expand Down Expand Up @@ -225,6 +242,7 @@ impl TransportBuilder for QuicTransport {
pending_inbound_connections: HashMap::new(),
pending_raw_connections: FuturesUnordered::new(),
pending_connections: FuturesUnordered::new(),
cancel_futures: HashMap::new(),
},
listen_addresses,
))
Expand Down Expand Up @@ -407,12 +425,18 @@ impl Transport for QuicTransport {
})
.collect();

self.pending_raw_connections.push(Box::pin(async move {
// Future that will resolve to the first successful connection.
let future = async move {
let mut errors = Vec::with_capacity(num_addresses);

while let Some(result) = futures.next().await {
match result {
Ok((address, connection)) => return Ok((connection_id, address, connection)),
Ok((address, stream)) =>
return RawConnectionResult::Connected {
connection_id,
address,
stream,
},
Err(error) => {
tracing::debug!(
target: LOG_TARGET,
Expand All @@ -425,8 +449,16 @@ impl Transport for QuicTransport {
}
}

Err((connection_id, errors))
}));
RawConnectionResult::Failed {
connection_id,
errors,
}
};

let (fut, handle) = futures::future::abortable(future);
let fut = fut.unwrap_or_else(move |_| RawConnectionResult::Canceled { connection_id });
self.pending_raw_connections.push(Box::pin(fut));
self.cancel_futures.insert(connection_id, handle);

Ok(())
}
Expand All @@ -446,6 +478,7 @@ impl Transport for QuicTransport {
/// Cancel opening connections.
fn cancel(&mut self, connection_id: ConnectionId) {
self.canceled.insert(connection_id);
self.cancel_futures.remove(&connection_id).map(|handle| handle.abort());
}
}

Expand All @@ -470,32 +503,35 @@ impl Stream for QuicTransport {
}

while let Poll::Ready(Some(result)) = self.pending_raw_connections.poll_next_unpin(cx) {
match result {
Ok((connection_id, address, stream)) => {
tracing::trace!(
target: LOG_TARGET,
?connection_id,
?address,
canceled = self.canceled.contains(&connection_id),
"connection opened",
);
tracing::trace!(target: LOG_TARGET, ?result, "raw connection result");

match result {
RawConnectionResult::Connected {
connection_id,
address,
stream,
} =>
if !self.canceled.remove(&connection_id) {
self.opened_raw.insert(connection_id, (stream, address.clone()));

return Poll::Ready(Some(TransportEvent::ConnectionOpened {
connection_id,
address,
}));
}
}
Err((connection_id, errors)) =>
},
RawConnectionResult::Failed {
connection_id,
errors,
} =>
if !self.canceled.remove(&connection_id) {
return Poll::Ready(Some(TransportEvent::OpenFailure {
connection_id,
errors,
}));
},
RawConnectionResult::Canceled { connection_id } => {
self.canceled.remove(&connection_id);
}
}
}

Expand Down
126 changes: 95 additions & 31 deletions src/transport/tcp/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,14 +38,15 @@ use crate::{

use futures::{
future::BoxFuture,
stream::{FuturesUnordered, Stream, StreamExt},
stream::{AbortHandle, FuturesUnordered, Stream, StreamExt},
TryFutureExt,
};
use multiaddr::Multiaddr;
use socket2::{Domain, Socket, Type};
use tokio::net::TcpStream;

use std::{
collections::{HashMap, HashSet},
collections::HashMap,
net::SocketAddr,
pin::Pin,
task::{Context, Poll},
Expand All @@ -70,6 +71,25 @@ struct PendingInboundConnection {
address: SocketAddr,
}

#[derive(Debug)]
enum RawConnectionResult {
/// The first successful connection.
Connected {
connection_id: ConnectionId,
address: Multiaddr,
stream: TcpStream,
},

/// All connection attempts failed.
Failed {
connection_id: ConnectionId,
errors: Vec<(Multiaddr, DialError)>,
},

/// Future was canceled.
Canceled { connection_id: ConnectionId },
}

/// TCP transport.
pub(crate) struct TcpTransport {
/// Transport context.
Expand All @@ -96,21 +116,15 @@ pub(crate) struct TcpTransport {
>,

/// Pending raw, unnegotiated connections.
pending_raw_connections: FuturesUnordered<
BoxFuture<
'static,
Result<
(ConnectionId, Multiaddr, TcpStream),
(ConnectionId, Vec<(Multiaddr, DialError)>),
>,
>,
>,
pending_raw_connections: FuturesUnordered<BoxFuture<'static, RawConnectionResult>>,

/// Opened raw connection, waiting for approval/rejection from `TransportManager`.
opened_raw: HashMap<ConnectionId, (TcpStream, Multiaddr)>,

/// Canceled raw connections.
canceled: HashSet<ConnectionId>,
/// Cancel raw connections futures.
///
/// This is cancelling `Self::pending_raw_connections`.
cancel_futures: HashMap<ConnectionId, AbortHandle>,

/// Connections which have been opened and negotiated but are being validated by the
/// `TransportManager`.
Expand Down Expand Up @@ -277,13 +291,13 @@ impl TransportBuilder for TcpTransport {
config,
context,
dial_addresses,
canceled: HashSet::new(),
opened_raw: HashMap::new(),
pending_open: HashMap::new(),
pending_dials: HashMap::new(),
pending_inbound_connections: HashMap::new(),
pending_connections: FuturesUnordered::new(),
pending_raw_connections: FuturesUnordered::new(),
cancel_futures: HashMap::new(),
},
listen_addresses,
))
Expand Down Expand Up @@ -412,11 +426,17 @@ impl Transport for TcpTransport {
})
.collect();

self.pending_raw_connections.push(Box::pin(async move {
// Future that will resolve to the first successful connection.
let future = async move {
let mut errors = Vec::with_capacity(num_addresses);
while let Some(result) = futures.next().await {
match result {
Ok((address, stream)) => return Ok((connection_id, address, stream)),
Ok((address, stream)) =>
return RawConnectionResult::Connected {
connection_id,
address,
stream,
},
Err(error) => {
tracing::debug!(
target: LOG_TARGET,
Expand All @@ -429,8 +449,16 @@ impl Transport for TcpTransport {
}
}

Err((connection_id, errors))
}));
RawConnectionResult::Failed {
connection_id,
errors,
}
};

let (fut, handle) = futures::future::abortable(future);
let fut = fut.unwrap_or_else(move |_| RawConnectionResult::Canceled { connection_id });
self.pending_raw_connections.push(Box::pin(fut));
self.cancel_futures.insert(connection_id, handle);

Ok(())
}
Expand Down Expand Up @@ -487,7 +515,11 @@ impl Transport for TcpTransport {
}

fn cancel(&mut self, connection_id: ConnectionId) {
self.canceled.insert(connection_id);
// Cancel the future if it exists.
// State clean-up happens inside the `poll_next`.
if let Some(handle) = self.cancel_futures.get(&connection_id) {
handle.abort();
}
}
}

Expand Down Expand Up @@ -523,17 +555,25 @@ impl Stream for TcpTransport {
}

while let Poll::Ready(Some(result)) = self.pending_raw_connections.poll_next_unpin(cx) {
tracing::trace!(target: LOG_TARGET, ?result, "raw connection result");

match result {
Ok((connection_id, address, stream)) => {
tracing::trace!(
target: LOG_TARGET,
?connection_id,
?address,
canceled = self.canceled.contains(&connection_id),
"connection opened",
);
RawConnectionResult::Connected {
connection_id,
address,
stream,
} => {
let Some(handle) = self.cancel_futures.remove(&connection_id) else {
tracing::warn!(
target: LOG_TARGET,
?connection_id,
?address,
"raw connection without a cancel handle",
);
continue;
};

if !self.canceled.remove(&connection_id) {
if !handle.is_aborted() {
self.opened_raw.insert(connection_id, (stream, address.clone()));

return Poll::Ready(Some(TransportEvent::ConnectionOpened {
Expand All @@ -542,13 +582,37 @@ impl Stream for TcpTransport {
}));
}
}
Err((connection_id, errors)) =>
if !self.canceled.remove(&connection_id) {

RawConnectionResult::Failed {
connection_id,
errors,
} => {
let Some(handle) = self.cancel_futures.remove(&connection_id) else {
tracing::warn!(
target: LOG_TARGET,
?connection_id,
?errors,
"raw connection without a cancel handle",
);
continue;
};

if !handle.is_aborted() {
return Poll::Ready(Some(TransportEvent::OpenFailure {
connection_id,
errors,
}));
},
}
}
RawConnectionResult::Canceled { connection_id } => {
if self.cancel_futures.remove(&connection_id).is_none() {
tracing::warn!(
target: LOG_TARGET,
?connection_id,
"raw cancelled connection without a handle",
lexnv marked this conversation as resolved.
Show resolved Hide resolved
);
}
}
}
}

Expand Down
Loading