-
Notifications
You must be signed in to change notification settings - Fork 1.3k
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
connect_tcp() is inconsistent between Tokio and async-std
#2821
Comments
@abonander so something like this would work? #[cfg(feature = "_rt-async-std")]
{
use async_io::Async;
use async_std::net::ToSocketAddrs;
use std::net::TcpStream;
let mut last_err = None;
// Loop through all the Socket Addresses that the hostname resolves to
for socket_addr in (host, port).to_socket_addrs().await? {
match Async::<TcpStream>::connect(socket_addr).await {
Ok(stream) => return Ok(with_socket.with_socket(stream)),
Err(e) => last_err = Some(e),
}
}
// If we reach this point, it means we failed to connect to any of the addresses.
// Return the last error we encountered, or a custom error if the hostname didn't resolve to any address.
match last_err {
Some(err) => return Err(err.into()),
None => {
return Err(io::Error::new(
io::ErrorKind::AddrNotAvailable,
"Hostname did not resolve to any addresses",
)
.into())
}
}
} I tried this on my fork but the tests fail with the following (unrelated) message:
|
@anupj you have to enable the |
@abonander I am now following the tests
|
… connection This commit updates the error handling logic in the `connect_tcp` function. Previously, the function would panic if the hostname did not resolve to at least one address. The updated logic attempts to establish a TCP connection for each address that the hostname resolves to. If it fails to connect to any of the addresses, it will return the last encountered error. If the hostname doesn't resolve to any addresses, the function returns a custom error message stating "Hostname did not resolve to any addresses".
There's some fixes to |
If you rebase, |
…#2822) This commit updates the error handling logic in the `connect_tcp` function. Previously, the function would panic if the hostname did not resolve to at least one address. The updated logic attempts to establish a TCP connection for each address that the hostname resolves to. If it fails to connect to any of the addresses, it will return the last encountered error. If the hostname doesn't resolve to any addresses, the function returns a custom error message stating "Hostname did not resolve to any addresses".
Thank you @abonander and thanks for merging my PR 👍🏽 |
While triaging #2808 I realized that we have inconsistent behavior between Tokio and
async-std
when opening a TCP connection, in the case that a domain name resolves to multiple IP addresses:This is an easy fix, though:
sqlx/sqlx-core/src/net/socket/mod.rs
Lines 208 to 212 in 58cb18a
That code should iterate through the addresses returned by
(host, port).to_socket_addrs().await?
and try to connect to each one in turn, returning the first that succeeds or the last error otherwise. If it resolves to zero addresses, it should also return an error instead of panicking (what actually happens if the hostname doesn't resolve to anything? I don't actually know).It'd be pretty complex to set up a regression test for it, though it'd be nice to at least manually test it.
The text was updated successfully, but these errors were encountered: