Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore(manager): use TcpListener to check free port #1240

Closed
wants to merge 8 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 1 addition & 3 deletions sn_node_manager/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"]}
Expand Down
27 changes: 8 additions & 19 deletions sn_node_manager/src/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,19 +14,14 @@
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,
};
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);

/// 6
#[derive(Debug, PartialEq)]
pub struct ServiceConfig {
pub data_dir_path: PathBuf,
Expand Down Expand Up @@ -154,12 +149,9 @@
}

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));
Fixed Show fixed Hide fixed

Check notice

Code scanning / devskim

Accessing localhost could indicate debug code, or could hinder scaling. Note

Do not leave debug code in production
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
}
Expand All @@ -173,12 +165,9 @@
fn get_available_port(&self) -> Result<u16> {
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.
// Without the delay, we may face 'Port already in use' error, when trying to re-use this port.
sleep(PORT_UNBINDING_DELAY);

Ok(port)
}
Expand Down
Loading