diff --git a/swarm/CHANGELOG.md b/swarm/CHANGELOG.md index eac4970e63b..d68330b4853 100644 --- a/swarm/CHANGELOG.md +++ b/swarm/CHANGELOG.md @@ -13,6 +13,9 @@ callback. - Update the `libp2p-core` dependency to `0.21`, fixing [1584](https://github.com/libp2p/rust-libp2p/issues/1584). +- Fix connections being kept alive by `OneShotHandler` when not handling any + requests [PR 1698](https://github.com/libp2p/rust-libp2p/pull/1698). + # 0.20.1 [2020-07-08] - Documentation updates. diff --git a/swarm/src/protocols_handler/one_shot.rs b/swarm/src/protocols_handler/one_shot.rs index 079cc815376..4f5a0288eca 100644 --- a/swarm/src/protocols_handler/one_shot.rs +++ b/swarm/src/protocols_handler/one_shot.rs @@ -161,11 +161,6 @@ where _: Self::OutboundOpenInfo, ) { self.dial_negotiated -= 1; - - if self.dial_negotiated == 0 && self.dial_queue.is_empty() { - self.keep_alive = KeepAlive::Until(Instant::now() + self.config.keep_alive_timeout); - } - self.events_out.push(out.into()); } @@ -221,6 +216,10 @@ where } } else { self.dial_queue.shrink_to_fit(); + + if self.dial_negotiated == 0 && self.keep_alive.is_yes() { + self.keep_alive = KeepAlive::Until(Instant::now() + self.config.keep_alive_timeout); + } } Poll::Pending @@ -245,3 +244,30 @@ impl Default for OneShotHandlerConfig { } } +#[cfg(test)] +mod tests { + use super::*; + + use futures::executor::block_on; + use futures::future::poll_fn; + use libp2p_core::upgrade::DeniedUpgrade; + use void::Void; + + #[test] + fn do_not_keep_idle_connection_alive() { + let mut handler: OneShotHandler<_, DeniedUpgrade, Void> = OneShotHandler::new( + SubstreamProtocol::new(DeniedUpgrade{}), + Default::default(), + ); + + block_on(poll_fn(|cx| { + loop { + if let Poll::Pending = handler.poll(cx) { + return Poll::Ready(()) + } + } + })); + + assert!(matches!(handler.connection_keep_alive(), KeepAlive::Until(_))); + } +}