diff --git a/src/main.rs b/src/main.rs index 0e00bdd..40a66fe 100644 --- a/src/main.rs +++ b/src/main.rs @@ -87,7 +87,6 @@ async fn main() -> Result<(), Box> { // Logging and warnings // - // SimpleLogger::new().with_level(config.log_level).init()?; tracing_subscriber::fmt() .with_max_level(config.log_level) .init(); @@ -121,6 +120,13 @@ async fn main() -> Result<(), Box> { // HTTP Server init + // Try bind to provided port first + let port_reservation = std::net::TcpListener::bind(bind_address); + if let Err(e) = port_reservation { + error!("Failed to bind to port!"); + return Err(e.into()); + }; + let server = if OFFLINE_MODE.load(Ordering::Acquire) { ServerState::init_offline() } else { @@ -225,6 +231,10 @@ async fn main() -> Result<(), Box> { }) .shutdown_timeout(60); + // drop port reservation, might have a TOCTOU but it's not a big deal; this + // is just a best effort. + std::mem::drop(port_reservation); + if disable_tls { server.bind(bind_address)?.run().await?; } else {