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

Address already in use after stopping axum_server #157

Open
mraszyk opened this issue Nov 20, 2024 · 0 comments
Open

Address already in use after stopping axum_server #157

mraszyk opened this issue Nov 20, 2024 · 0 comments

Comments

@mraszyk
Copy link

mraszyk commented Nov 20, 2024

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"] }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant