diff --git a/protocols/autonat/CHANGELOG.md b/protocols/autonat/CHANGELOG.md index 445b1189689..7e498db6fc3 100644 --- a/protocols/autonat/CHANGELOG.md +++ b/protocols/autonat/CHANGELOG.md @@ -6,6 +6,9 @@ `SeedableRng + rand::Rng`. See [PR 6559](https://github.com/libp2p/rust-libp2p/pull/6559). +- Close v2 server dial-back connections once the client acknowledges the dial-back instead of leaving them open. + See [PR 6528](https://github.com/libp2p/rust-libp2p/pull/6528). + - Fix AutoNAT v2 server preserving the selected address index for multi-address `DialRequest`s. See [PR 6514](https://github.com/libp2p/rust-libp2p/pull/6514). diff --git a/protocols/autonat/src/v2/server/behaviour.rs b/protocols/autonat/src/v2/server/behaviour.rs index 62fd3f8b8cb..0597308e3d1 100644 --- a/protocols/autonat/src/v2/server/behaviour.rs +++ b/protocols/autonat/src/v2/server/behaviour.rs @@ -8,8 +8,8 @@ use either::Either; use libp2p_core::{Endpoint, Multiaddr, transport::PortUse}; use libp2p_identity::PeerId; use libp2p_swarm::{ - ConnectionDenied, ConnectionHandler, ConnectionId, DialFailure, FromSwarm, NetworkBehaviour, - ToSwarm, + CloseConnection, ConnectionDenied, ConnectionHandler, ConnectionId, DialFailure, FromSwarm, + NetworkBehaviour, ToSwarm, dial_opts::{DialOpts, PeerCondition}, dummy, }; @@ -102,13 +102,22 @@ where fn on_connection_handler_event( &mut self, peer_id: PeerId, - _connection_id: ConnectionId, + connection_id: ConnectionId, event: as ConnectionHandler>::ToBehaviour, ) { match event { - Either::Left(Either::Left(Ok(_))) => {} - Either::Left(Either::Left(Err(e))) => { - tracing::debug!("dial back error: {e:?}"); + Either::Left(Either::Left(result)) => { + if let Err(e) = result { + tracing::debug!("dial back error: {e:?}"); + } + // By now the dial-back exchange has concluded: the handler + // waited for the `DialBackResponse` confirming the nonce was + // delivered, or failed. Either way the connection is safe to + // close. + self.pending_events.push_back(ToSwarm::CloseConnection { + peer_id, + connection: CloseConnection::One(connection_id), + }); } Either::Left(Either::Right(v)) => libp2p_core::util::unreachable(v), Either::Right(Either::Left(cmd)) => {