From 4fc617a8a54e6eaf20f618eedfc23266711ba800 Mon Sep 17 00:00:00 2001 From: Roland Sherwin Date: Mon, 29 Jan 2024 20:06:19 +0530 Subject: [PATCH 1/8] chore(manager): use TcpListener to check free port --- sn_node_manager/Cargo.toml | 4 +--- sn_node_manager/src/service.rs | 19 +++++++++---------- 2 files changed, 10 insertions(+), 13 deletions(-) diff --git a/sn_node_manager/Cargo.toml b/sn_node_manager/Cargo.toml index 3ebba832fe..55a46d6b6c 100644 --- a/sn_node_manager/Cargo.toml +++ b/sn_node_manager/Cargo.toml @@ -14,9 +14,7 @@ path="src/main.rs" name="safenode-manager" [features] -default = ["quic"] -quic = [] -tcp = [] +default = [] [dependencies] clap = { version = "4.4.6", features = ["derive", "env"]} diff --git a/sn_node_manager/src/service.rs b/sn_node_manager/src/service.rs index 1a770201f4..9e4b947cfd 100644 --- a/sn_node_manager/src/service.rs +++ b/sn_node_manager/src/service.rs @@ -14,14 +14,13 @@ use service_manager::{ ServiceInstallCtx, ServiceLabel, ServiceManager, ServiceStartCtx, ServiceStopCtx, ServiceUninstallCtx, }; -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::path::PathBuf; -use std::time::Duration; -use std::{ffi::OsString, thread::sleep}; +use std::{ + ffi::OsString, + net::{SocketAddr, TcpListener}, + path::PathBuf, + thread::sleep, + time::Duration, +}; use sysinfo::{Pid, System, SystemExt}; // The UDP port might fail to unbind even when dropped and this can cause the safenode process to throw errors. @@ -154,7 +153,7 @@ impl ServiceControl for NodeServiceManager { } fn is_port_free(&self, port: u16) -> bool { - let socket = SocketBinder::bind(("127.0.0.1", port)); + let socket = TcpListener::bind(("127.0.0.1", port)); let is_free = socket.is_ok(); drop(socket); // Sleep a little while to make sure that we've dropped the socket. @@ -173,7 +172,7 @@ impl ServiceControl for NodeServiceManager { fn get_available_port(&self) -> Result { let addr: SocketAddr = "127.0.0.1:0".parse().unwrap(); - let socket = SocketBinder::bind(addr)?; + let socket = TcpListener::bind(addr)?; let port = socket.local_addr()?.port(); drop(socket); // Sleep a little while to make sure that we've dropped the socket. From 946712d4e11e809c336c70d5a5c3a1aa4d777120 Mon Sep 17 00:00:00 2001 From: Roland Sherwin Date: Mon, 29 Jan 2024 20:07:14 +0530 Subject: [PATCH 2/8] chore(manager): remove port unbinding delay --- sn_node_manager/src/service.rs | 11 ----------- 1 file changed, 11 deletions(-) diff --git a/sn_node_manager/src/service.rs b/sn_node_manager/src/service.rs index 9e4b947cfd..aa74180d8a 100644 --- a/sn_node_manager/src/service.rs +++ b/sn_node_manager/src/service.rs @@ -18,14 +18,9 @@ use std::{ ffi::OsString, net::{SocketAddr, TcpListener}, path::PathBuf, - thread::sleep, - time::Duration, }; use sysinfo::{Pid, System, SystemExt}; -// The UDP port might fail to unbind even when dropped and this can cause the safenode process to throw errors. -const PORT_UNBINDING_DELAY: Duration = Duration::from_secs(3); - #[derive(Debug, PartialEq)] pub struct ServiceConfig { pub data_dir_path: PathBuf, @@ -156,9 +151,6 @@ impl ServiceControl for NodeServiceManager { let socket = TcpListener::bind(("127.0.0.1", port)); let is_free = socket.is_ok(); 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(PORT_UNBINDING_DELAY); is_free } @@ -175,9 +167,6 @@ impl ServiceControl for NodeServiceManager { let socket = TcpListener::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(PORT_UNBINDING_DELAY); Ok(port) } From 6a8f88f6ae279762bc8186d2bd67bd10c3912db1 Mon Sep 17 00:00:00 2001 From: Roland Sherwin Date: Mon, 29 Jan 2024 20:18:52 +0530 Subject: [PATCH 3/8] chore: testing 1 --- sn_node_manager/src/service.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/sn_node_manager/src/service.rs b/sn_node_manager/src/service.rs index aa74180d8a..f9745e137b 100644 --- a/sn_node_manager/src/service.rs +++ b/sn_node_manager/src/service.rs @@ -21,6 +21,7 @@ use std::{ }; use sysinfo::{Pid, System, SystemExt}; +/// 1 #[derive(Debug, PartialEq)] pub struct ServiceConfig { pub data_dir_path: PathBuf, From a11005b7e082283c234530b57ffa6719259095ec Mon Sep 17 00:00:00 2001 From: Roland Sherwin Date: Mon, 29 Jan 2024 20:19:09 +0530 Subject: [PATCH 4/8] chore: testing 2 --- sn_node_manager/src/service.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sn_node_manager/src/service.rs b/sn_node_manager/src/service.rs index f9745e137b..dddf1d8a83 100644 --- a/sn_node_manager/src/service.rs +++ b/sn_node_manager/src/service.rs @@ -21,7 +21,7 @@ use std::{ }; use sysinfo::{Pid, System, SystemExt}; -/// 1 +/// 2 #[derive(Debug, PartialEq)] pub struct ServiceConfig { pub data_dir_path: PathBuf, From 09ba66212172054461001ba6e7a763344e281e72 Mon Sep 17 00:00:00 2001 From: Roland Sherwin Date: Mon, 29 Jan 2024 20:19:16 +0530 Subject: [PATCH 5/8] chore: testing 3 --- sn_node_manager/src/service.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sn_node_manager/src/service.rs b/sn_node_manager/src/service.rs index dddf1d8a83..57365f8604 100644 --- a/sn_node_manager/src/service.rs +++ b/sn_node_manager/src/service.rs @@ -21,7 +21,7 @@ use std::{ }; use sysinfo::{Pid, System, SystemExt}; -/// 2 +/// 3 #[derive(Debug, PartialEq)] pub struct ServiceConfig { pub data_dir_path: PathBuf, From a1f469f0d77d087ffb29421ee230800d9e0f7bf2 Mon Sep 17 00:00:00 2001 From: Roland Sherwin Date: Mon, 29 Jan 2024 20:19:27 +0530 Subject: [PATCH 6/8] chore: testing 4 --- sn_node_manager/src/service.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sn_node_manager/src/service.rs b/sn_node_manager/src/service.rs index 57365f8604..ae9dfbc7a0 100644 --- a/sn_node_manager/src/service.rs +++ b/sn_node_manager/src/service.rs @@ -21,7 +21,7 @@ use std::{ }; use sysinfo::{Pid, System, SystemExt}; -/// 3 +/// 4 #[derive(Debug, PartialEq)] pub struct ServiceConfig { pub data_dir_path: PathBuf, From cbf0b8faf7401702d08eda8d2535ace3d9c62f32 Mon Sep 17 00:00:00 2001 From: Roland Sherwin Date: Mon, 29 Jan 2024 20:20:26 +0530 Subject: [PATCH 7/8] chore: testing 5 --- sn_node_manager/src/service.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sn_node_manager/src/service.rs b/sn_node_manager/src/service.rs index ae9dfbc7a0..935d2f0694 100644 --- a/sn_node_manager/src/service.rs +++ b/sn_node_manager/src/service.rs @@ -21,7 +21,7 @@ use std::{ }; use sysinfo::{Pid, System, SystemExt}; -/// 4 +/// 5 #[derive(Debug, PartialEq)] pub struct ServiceConfig { pub data_dir_path: PathBuf, From cb3df82caac01d29376f25713e294aa67ed63c78 Mon Sep 17 00:00:00 2001 From: Roland Sherwin Date: Mon, 29 Jan 2024 20:20:34 +0530 Subject: [PATCH 8/8] chore: testing 6 --- sn_node_manager/src/service.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sn_node_manager/src/service.rs b/sn_node_manager/src/service.rs index 935d2f0694..24a97518bd 100644 --- a/sn_node_manager/src/service.rs +++ b/sn_node_manager/src/service.rs @@ -21,7 +21,7 @@ use std::{ }; use sysinfo::{Pid, System, SystemExt}; -/// 5 +/// 6 #[derive(Debug, PartialEq)] pub struct ServiceConfig { pub data_dir_path: PathBuf,