When testing, tcp connect error
occurs.
#2756
-
Version Platform Description let addr = SocketAddr::from(([127, 0, 0, 1], 3000));
tokio::spawn(async move {
axum::Server::bind(&addr)
.serve(create_route().into_make_service())
.await
.expect("unexpected server error");
});
let client = hyper::Client::new();
let response = client
.request(
Request::builder()
.uri(format!("http://{}", addr))
.body(Body::empty())
.unwrap(),
)
.await
.unwrap();
I rewrote it using let listener = TcpListener::bind("127.0.0.1:8000".parse::<SocketAddr>().unwrap()).unwrap();
let addr = listener.local_addr().unwrap();
...
tokio::spawn(async move {
axum::Server::from_tcp(listener)
.unwrap()
.serve(create_route().into_make_service())
.await
.expect("unexpected server error");
}); Both worked fine when I did a |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 2 replies
-
This is probably a race. In the first version, the client connection can occur before the spawned tasks actually binds the server. In the second example, you've bound the server before spawning the server task. |
Beta Was this translation helpful? Give feedback.
This is probably a race. In the first version, the client connection can occur before the spawned tasks actually binds the server. In the second example, you've bound the server before spawning the server task.