diff --git a/testnet/stacks-node/src/event_dispatcher.rs b/testnet/stacks-node/src/event_dispatcher.rs index 5a72e4ca0a..58f567b4b7 100644 --- a/testnet/stacks-node/src/event_dispatcher.rs +++ b/testnet/stacks-node/src/event_dispatcher.rs @@ -6,7 +6,9 @@ use std::thread::sleep; use std::time::Duration; use async_h1::client; +use async_std::future::timeout; use async_std::net::TcpStream; +use async_std::task; use clarity::vm::analysis::contract_interface_builder::build_contract_interface; use clarity::vm::costs::ExecutionCost; use clarity::vm::events::{FTEventType, NFTEventType, STXEventType}; @@ -318,6 +320,7 @@ impl EventObserver { }; let backoff = Duration::from_millis((1.0 * 1_000.0) as u64); + let connection_timeout = Duration::from_secs(5); loop { let body = body.clone(); @@ -325,20 +328,25 @@ impl EventObserver { req.append_header("Content-Type", "application/json"); req.set_body(body); - let response = async_std::task::block_on(async { - let stream = match TcpStream::connect(self.endpoint.clone()).await { - Ok(stream) => stream, - Err(err) => { - warn!("Event dispatcher: connection failed - {:?}", err); - return None; - } - }; + let response = task::block_on(async { + let stream = + match timeout(connection_timeout, TcpStream::connect(&self.endpoint)).await { + Ok(Ok(stream)) => stream, + Ok(Err(err)) => { + warn!("Event dispatcher: connection failed - {:?}", err); + return None; + } + Err(_) => { + error!("Event dispatcher: connection attempt timed out"); + return None; + } + }; match client::connect(stream, req).await { Ok(response) => Some(response), Err(err) => { warn!("Event dispatcher: rpc invocation failed - {:?}", err); - return None; + None } } });