Skip to content

Commit 81af089

Browse files
authored
fix: add timeouts to HTTP client to prevent freezing (#675)
* fix: add timeouts to HTTP client to prevent freezing When running Helios for extended periods, the HTTP client could hang indefinitely on network requests if the beacon node becomes unresponsive or network issues occur. This would cause the consensus sync loop to freeze, preventing the client from following the chain. This fix adds: - 30 second timeout for individual requests - 10 second connection timeout - 60 second idle connection cleanup These timeouts ensure that network issues or unresponsive beacon nodes won't cause Helios to freeze permanently, allowing it to recover and continue syncing. * fix: ensure WASM compatibility for HTTP client timeouts - Use conditional compilation to only apply timeout settings on non-WASM targets - WASM builds use default reqwest client without timeouts (not supported in WASM) - Conditionally import Duration only for non-WASM builds to avoid warnings
1 parent 97aed6e commit 81af089

File tree

1 file changed

+14
-1
lines changed

1 file changed

+14
-1
lines changed

ethereum/src/rpc/http_rpc.rs

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
use std::cmp;
2+
#[cfg(not(target_arch = "wasm32"))]
3+
use std::time::Duration;
24

35
use alloy::primitives::B256;
46
use async_trait::async_trait;
@@ -68,8 +70,19 @@ impl HttpRpc {
6870
#[cfg_attr(target_arch = "wasm32", async_trait(?Send))]
6971
impl<S: ConsensusSpec> ConsensusRpc<S> for HttpRpc {
7072
fn new(rpc: &str) -> Self {
73+
#[cfg(not(target_arch = "wasm32"))]
74+
let client = reqwest::Client::builder()
75+
.timeout(Duration::from_secs(30)) // Add request timeout
76+
.connect_timeout(Duration::from_secs(10)) // Add connection timeout
77+
.pool_idle_timeout(Duration::from_secs(60)) // Clean up idle connections
78+
.build()
79+
.unwrap_or_else(|_| reqwest::Client::new());
80+
81+
#[cfg(target_arch = "wasm32")]
82+
let client = reqwest::Client::new();
83+
7184
HttpRpc {
72-
client: reqwest::Client::new(),
85+
client,
7386
rpc: rpc.trim_end_matches('/').to_string(),
7487
}
7588
}

0 commit comments

Comments
 (0)