Explorar el Código

fix: add 5s SSH connect timeout to prevent hangs on offline nodes

The russh client's default Config has no connection timeout, so SSH
connections to offline nodes hang indefinitely.  Added:
- CONNECT_TIMEOUT constant (5s)
- inactivity_timeout on client config
- tokio::time::timeout wrapper around russh::client::connect

The error message includes the node IP and timeout duration for
easier diagnosis.
Alexander Korolev hace 4 meses
padre
commit
cb4c15ec7a
Se han modificado 1 ficheros con 17 adiciones y 3 borrados
  1. 17 3
      src/ssh.rs

+ 17 - 3
src/ssh.rs

@@ -11,10 +11,15 @@ use tracing::warn;
 
 use crate::config::{Config, Node};
 
+const CONNECT_TIMEOUT: Duration = Duration::from_secs(5);
+
 /// Establishes an SSH session to a node using the config credentials.
 pub async fn ssh_session(config: &Config, node: &Node) -> Result<client::Handle<VerifyingHandler>> {
     let key = load_secret_key(&config.ssh_key_path)?;
-    let ssh_config = russh::client::Config::default();
+    let ssh_config = russh::client::Config {
+        inactivity_timeout: Some(CONNECT_TIMEOUT),
+        ..Default::default()
+    };
     let expected_key = Arc::new(std::sync::Mutex::new(node.host_key.clone()));
 
     if expected_key.lock().unwrap().is_none() {
@@ -25,8 +30,17 @@ pub async fn ssh_session(config: &Config, node: &Node) -> Result<client::Handle<
     }
 
     let handler = VerifyingHandler { expected_key };
-    let mut session =
-        russh::client::connect(Arc::new(ssh_config), (node.ssh_ip.as_str(), 22), handler).await?;
+    let mut session = tokio::time::timeout(
+        CONNECT_TIMEOUT,
+        russh::client::connect(Arc::new(ssh_config), (node.ssh_ip.as_str(), 22), handler),
+    )
+    .await
+    .context(format!(
+        "SSH connect to {}:{} timed out after {}s",
+        node.ssh_ip,
+        22,
+        CONNECT_TIMEOUT.as_secs()
+    ))??;
 
     session
         .authenticate_publickey(