Merge pull request #112 from sopium/windows-tcp-connect-take-error

Correctly detect TCP connect failures on Windows
This commit is contained in:
Stjepan Glavina 2020-05-15 06:50:08 -07:00 committed by GitHub
commit 85ce918357
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 17 additions and 0 deletions

View File

@ -570,6 +570,20 @@ impl Async<TcpStream> {
// Wait for connect to complete.
let wait_connect = |mut stream: &TcpStream| match stream.write(&[]) {
Err(err) if err.kind() == io::ErrorKind::NotConnected => {
// On other systems, if a non-blocking connect call fails, a
// sensible error code is returned by the send (write) call.
//
// But not on Windows. If a non-blocking connect call fails, the
// send call always returns NotConnected. We have to use
// take_error (i.e., getsockopt SO_ERROR) to find out whether
// the connect call has failed, and retrieve the error code if
// it has.
#[cfg(windows)]
{
if let Some(e) = stream.take_error()? {
return Err(e);
}
}
Err(io::ErrorKind::WouldBlock.into())
}
res => res.map(|_| ()),

View File

@ -35,6 +35,9 @@ fn tcp_connection() -> io::Result<()> {
stream1.get_ref().local_addr()?
);
// Now that the listener is closed, connect should fail.
Async::<TcpStream>::connect(&addr).await.unwrap_err();
Ok(())
})
}