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

issue #2821 Update error handling logic when opening a TCP connection #2822

Merged
merged 1 commit into from
Oct 19, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 20 additions & 7 deletions sqlx-core/src/net/socket/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -205,15 +205,28 @@
use async_std::net::ToSocketAddrs;
use std::net::TcpStream;

let socket_addr = (host, port)
.to_socket_addrs()
.await?
.next()
.expect("BUG: to_socket_addrs() should have returned at least one result");
let mut last_err = None;

let stream = Async::<TcpStream>::connect(socket_addr).await?;
// 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),
}
}

return Ok(with_socket.with_socket(stream));
// 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())
}
}
}

#[cfg(not(feature = "_rt-async-std"))]
Expand All @@ -226,8 +239,8 @@
///
/// Returns an error if Unix Domain Sockets are not supported on this platform.
pub async fn connect_uds<P: AsRef<Path>, Ws: WithSocket>(
path: P,

Check warning on line 242 in sqlx-core/src/net/socket/mod.rs

View workflow job for this annotation

GitHub Actions / CLI Binaries (windows-latest)

unused variable: `path`
with_socket: Ws,

Check warning on line 243 in sqlx-core/src/net/socket/mod.rs

View workflow job for this annotation

GitHub Actions / CLI Binaries (windows-latest)

unused variable: `with_socket`
) -> crate::Result<Ws::Output> {
#[cfg(not(unix))]
{
Expand Down Expand Up @@ -258,7 +271,7 @@
}

#[cfg(not(feature = "_rt-async-std"))]
{

Check warning on line 274 in sqlx-core/src/net/socket/mod.rs

View workflow job for this annotation

GitHub Actions / CLI Binaries (windows-latest)

unreachable expression
crate::rt::missing_rt((path, with_socket))
}
}
Loading