Skip to content
This repository has been archived by the owner on Jan 25, 2024. It is now read-only.

fix: add delay to make sure we drop the socket #25

Merged
merged 2 commits into from
Jan 23, 2024
Merged
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
10 changes: 5 additions & 5 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -391,7 +391,7 @@ async fn main() -> Result<()> {
node_path,
node_version,
peers,
skip_validation,
skip_validation: _,
} => {
println!("=================================================");
println!(" Joining Local Network ");
Expand Down Expand Up @@ -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(())
Expand Down Expand Up @@ -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)?;
Expand Down Expand Up @@ -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?;

Expand Down
12 changes: 10 additions & 2 deletions src/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -163,7 +164,14 @@ impl ServiceControl for NodeServiceManager {
fn get_available_port(&self) -> Result<u16> {
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<()> {
Expand Down
Loading