axum-server icon indicating copy to clipboard operation
axum-server copied to clipboard

Address already in use after stopping axum_server

Open mraszyk opened this issue 1 year ago • 0 comments

Steps to reproduce:

  1. Run the following binary
use std::net::SocketAddr;
use std::net::TcpListener;

#[tokio::main]
async fn main() {
    let addr = SocketAddr::from(([127, 0, 0, 1], 4943));
    let listener = TcpListener::bind(addr).unwrap();
    axum_server::from_tcp(listener).serve(axum::Router::new().into_make_service()).await.unwrap();
}
  1. Access http://localhost:4943 in Google Chrome (should give 404).

  2. CTRL-C the binary from step 1. and run the following binary within ~minute after step 2.:

use socket2::{Socket, Domain};
use std::net::SocketAddr;

#[tokio::main]
async fn main() {
  let socket = Socket::new(Domain::IPV4, socket2::Type::STREAM, None).unwrap();
  socket.bind(&SocketAddr::new(std::net::IpAddr::V4(std::net::Ipv4Addr::new(127, 0, 0, 1)), 4943).into()).unwrap();
}

Step 3 fails with

called `Result::unwrap()` on an `Err` value: Os { code: 98, kind: AddrInUse, message: "Address already in use" }
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace

while the expected behavior is that step 3. succeeds without any error which is the case if we access http://localhost:4943 in step 2. using curl (should also give 404) instead of Google Chrome.

For reference, I also attach my Cargo.toml:

[package]
name = "port"
version = "0.1.0"
edition = "2021"

[dependencies]
axum = "0.7.9"
axum-server = "0.7.1"
socket2 = "0.5.7"
tokio = { version = "1.41.1", features = ["full"] }

mraszyk avatar Nov 20 '24 16:11 mraszyk