From 69c865201f15c264ed6eaf785f3625c01438a4e7 Mon Sep 17 00:00:00 2001 From: Roland Sherwin Date: Mon, 22 Jan 2024 20:43:01 +0530 Subject: [PATCH 1/2] fix: add delay to make sure we drop the socket --- src/main.rs | 2 +- src/service.rs | 12 ++++++++++-- 2 files changed, 11 insertions(+), 3 deletions(-) diff --git a/src/main.rs b/src/main.rs index de06b38..7456f8b 100644 --- a/src/main.rs +++ b/src/main.rs @@ -288,7 +288,7 @@ pub enum SubCmd { }, } -#[tokio::main] +#[tokio::main(flavor = "current_thread")] async fn main() -> Result<()> { color_eyre::install()?; let args = Cmd::parse(); diff --git a/src/service.rs b/src/service.rs index 745cc56..c2c0fe4 100644 --- a/src/service.rs +++ b/src/service.rs @@ -14,12 +14,13 @@ use service_manager::{ ServiceInstallCtx, ServiceLabel, ServiceManager, ServiceStartCtx, ServiceStopCtx, ServiceUninstallCtx, }; -use std::ffi::OsString; use std::net::SocketAddr; #[cfg(feature = "tcp")] use std::net::TcpListener as SocketBinder; #[cfg(not(feature = "tcp"))] use std::net::UdpSocket as SocketBinder; +use std::time::Duration; +use std::{ffi::OsString, thread::sleep}; use std::path::PathBuf; use sysinfo::{Pid, System, SystemExt}; @@ -163,7 +164,14 @@ impl ServiceControl for NodeServiceManager { fn get_available_port(&self) -> Result { let addr: SocketAddr = "127.0.0.1:0".parse().unwrap(); - Ok(SocketBinder::bind(addr)?.local_addr()?.port()) + let socket = SocketBinder::bind(addr)?; + let port = socket.local_addr()?.port(); + drop(socket); + // Sleep a little while to make sure that we've dropped the socket. + // Without the delay, we may face 'Port already in use' error, when trying to re-use this port. + sleep(Duration::from_secs(1)); + + Ok(port) } fn install(&self, config: ServiceConfig) -> Result<()> { From 41f0b99303b8fff73fae29d8df613783c624ca79 Mon Sep 17 00:00:00 2001 From: Roland Sherwin Date: Mon, 22 Jan 2024 20:43:20 +0530 Subject: [PATCH 2/2] test: force skip validation - enable this once validation passes succesfully --- src/main.rs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/main.rs b/src/main.rs index 7456f8b..94aac96 100644 --- a/src/main.rs +++ b/src/main.rs @@ -391,7 +391,7 @@ async fn main() -> Result<()> { node_path, node_version, peers, - skip_validation, + skip_validation: _, } => { println!("================================================="); println!(" Joining Local Network "); @@ -431,7 +431,7 @@ async fn main() -> Result<()> { node_count: count, peers, safenode_bin_path: node_path, - skip_validation, + skip_validation: true, }; run_network(&mut local_node_registry, &NodeServiceManager {}, options).await?; Ok(()) @@ -499,7 +499,7 @@ async fn main() -> Result<()> { faucet_version, node_path, node_version, - skip_validation, + skip_validation: _, } => { let local_node_reg_path = &get_local_node_registry_path()?; let mut local_node_registry = NodeRegistry::load(local_node_reg_path)?; @@ -534,7 +534,7 @@ async fn main() -> Result<()> { node_count: count, peers: None, safenode_bin_path: node_path, - skip_validation, + skip_validation: true, }; run_network(&mut local_node_registry, &NodeServiceManager {}, options).await?;