diff --git a/finality-aleph/src/validator_network/outgoing.rs b/finality-aleph/src/validator_network/outgoing.rs index 7091e15a7c..946aa9e32e 100644 --- a/finality-aleph/src/validator_network/outgoing.rs +++ b/finality-aleph/src/validator_network/outgoing.rs @@ -2,7 +2,7 @@ use std::fmt::{Debug, Display, Error as FmtError, Formatter}; use futures::channel::mpsc; use log::{debug, info}; -use tokio::time::{sleep, Duration}; +use tokio::time::{sleep, timeout, Duration}; use crate::validator_network::{ protocols::{ @@ -15,6 +15,7 @@ enum OutgoingError> { Dial(ND::Error), ProtocolNegotiation(PeerAddressInfo, ProtocolNegotiationError), Protocol(PeerAddressInfo, ProtocolError), + TimedOut, } impl> Display for OutgoingError { @@ -32,10 +33,14 @@ impl> Display for OutgoingError "communication with {} failed, protocol error: {}", addr, e ), + TimedOut => write!(f, "dial timeout",), } } } +/// Arbitrarily chosen timeout, should be more than enough. +const DIAL_TIMEOUT: Duration = Duration::from_secs(60); + async fn manage_outgoing>( secret_key: SK, public_key: SK::PublicKey, @@ -45,7 +50,10 @@ async fn manage_outgoing>( data_for_user: mpsc::UnboundedSender, ) -> Result<(), OutgoingError> { debug!(target: "validator-network", "Trying to connect to {}.", public_key); - let stream = dialer.connect(address).await.map_err(OutgoingError::Dial)?; + let stream = timeout(DIAL_TIMEOUT, dialer.connect(address)) + .await + .map_err(|_| OutgoingError::TimedOut)? + .map_err(OutgoingError::Dial)?; let peer_address_info = stream.peer_address_info(); debug!(target: "validator-network", "Performing outgoing protocol negotiation."); let (stream, protocol) = protocol(stream)