diff --git a/backend/src/net/tor.rs b/backend/src/net/tor.rs index 7fa105337..5f5fe89ff 100644 --- a/backend/src/net/tor.rs +++ b/backend/src/net/tor.rs @@ -313,6 +313,7 @@ impl TorControllerInner { } Err(e) => { tracing::info!("Failed to reconnect to tor control socket: {}", e); + tracing::info!("Trying again in one second"); } } tokio::time::sleep(Duration::from_secs(1)).await; @@ -321,7 +322,7 @@ impl TorControllerInner { self.connection.replace(new_connection); // swap empty map for owned old service map - let old_services = std::mem::replace(&mut self.services, BTreeMap::new()); + let old_services = std::mem::take(&mut self.services); // re add all of the services on the new control socket for ((package_id, interface_id), (tor_key, tor_cfg, ipv4)) in old_services { @@ -360,9 +361,9 @@ impl TorControllerInner { pub async fn tor_health_check(client: &Client, tor_controller: &TorController) { tracing::debug!("Attempting to self-check tor address"); - let onion = tor_controller.embassyd_onion().await; + let onion_addr = tor_controller.embassyd_onion().await; let result = client - .post(format!("http://{}/rpc/v1", onion)) + .post(format!("http://{}/rpc/v1", onion_addr)) .body( json!({ "jsonrpc": "2.0", @@ -374,32 +375,36 @@ pub async fn tor_health_check(client: &Client, tor_controller: &TorController) { ) .send() .await; - match result { - // if success, do nothing - Ok(_) => { - tracing::debug!( - "Successfully verified main tor address liveness at {}", - onion - ) - } - // if failure, disconnect tor control port, and restart tor controller - Err(e) => { - tracing::error!("Unable to reach self over tor: {}", e); - loop { - match tor_controller.replace().await { - Ok(restarted) => { - if restarted { - tracing::error!("Tor has been recently restarted, refusing to restart"); - } - break; - } - Err(e) => { - tracing::error!("Unable to restart tor: {}", e); - tracing::debug!("{:?}", e); + if let Err(e) = result { + let mut num_attempt = 1; + tracing::error!( + "Unable to reach self over tor, we will retry now..."); + tracing::error!("The first TOR error: {}", e); + + loop { + tracing::debug!("TOR Reconnecting retry number: {num_attempt}"); + + match tor_controller.replace().await { + Ok(restarted) => { + if restarted { + tracing::error!("Tor has been recently restarted, refusing to restart again right now..."); } + break; + } + Err(e) => { + tracing::error!("TOR retry error: {}", e); + tracing::error!("Unable to restart tor on attempt {num_attempt}...Retrying"); + + num_attempt += 1; + continue; } } } + } else { + tracing::debug!( + "Successfully verified main tor address liveness at {}", + onion_addr + ) } }