Skip to content

Commit

Permalink
transport: Fix pending dials memory leak (#271)
Browse files Browse the repository at this point in the history
The `pending_dials` is populated with a connection ID when initiating
outbound connections.
The state is rightfully cleaned up when the dialing fails, and error
propagated back to the user.
However, the state was not cleared when the dialing succeeded. This was
leading to a subtle memory leak in litep2p.

### Testing Done

I used a custom-patched version of litep2p to log the number of pending
dials. After a few hours, the pending dials for both TCP and WebSocket
connections stabilized at just a few.

```
2024-10-22 17:37:56.252  INFO tokio-runtime-worker litep2p::tcp: status pending_dials=1 pending_inbound_connections=0 pending_connections=1 pending_raw_connections=0 opened_raw=0 cancel_futures=0 pending_open=0
2024-10-22 17:38:26.252  INFO tokio-runtime-worker litep2p::tcp: status pending_dials=0 pending_inbound_connections=0 pending_connections=0 pending_raw_connections=1 opened_raw=0 cancel_futures=1 pending_open=0
2024-10-22 17:38:56.253  INFO tokio-runtime-worker litep2p::tcp: status pending_dials=0 pending_inbound_connections=0 pending_connections=0 pending_raw_connections=0 opened_raw=0 cancel_futures=0 pending_open=0
2024-10-22 17:39:26.253  INFO tokio-runtime-worker litep2p::tcp: status pending_dials=0 pending_inbound_connections=0 pending_connections=0 pending_raw_connections=0 opened_raw=0 cancel_futures=0 pending_open=0
2024-10-22 17:39:56.252  INFO tokio-runtime-worker litep2p::tcp: status pending_dials=0 pending_inbound_connections=0 pending_connections=0 pending_raw_connections=0 opened_raw=0 cancel_futures=0 pending_open=0
2024-10-22 17:40:26.252  INFO tokio-runtime-worker litep2p::tcp: status pending_dials=0 pending_inbound_connections=0 pending_connections=0 pending_raw_connections=1 opened_raw=0 cancel_futures=1 pending_open=0
2024-10-22 17:40:56.252  INFO tokio-runtime-worker litep2p::tcp: status pending_dials=0 pending_inbound_connections=0 pending_connections=0 pending_raw_connections=0 opened_raw=0 cancel_futures=0 pending_open=0
2024-10-22 17:41:26.252  INFO tokio-runtime-worker litep2p::tcp: status pending_dials=0 pending_inbound_connections=0 pending_connections=0 pending_raw_connections=0 opened_raw=0 cancel_futures=0 pending_open=0
```

Closes: #269

Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io>
  • Loading branch information
lexnv authored Oct 24, 2024
1 parent be39efd commit e378e76
Show file tree
Hide file tree
Showing 2 changed files with 2 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/transport/tcp/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -557,6 +557,7 @@ impl Stream for TcpTransport {
Ok(connection) => {
let peer = connection.peer();
let endpoint = connection.endpoint();
self.pending_dials.remove(&connection.connection_id());
self.pending_open.insert(connection.connection_id(), connection);

return Poll::Ready(Some(TransportEvent::ConnectionEstablished {
Expand Down
1 change: 1 addition & 0 deletions src/transport/websocket/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -599,6 +599,7 @@ impl Stream for WebSocketTransport {
Ok(connection) => {
let peer = connection.peer();
let endpoint = connection.endpoint();
self.pending_dials.remove(&connection.connection_id());
self.pending_open.insert(connection.connection_id(), connection);

return Poll::Ready(Some(TransportEvent::ConnectionEstablished {
Expand Down

0 comments on commit e378e76

Please sign in to comment.