Fix TcpStream::connect_timeout on linux
Linux appears to set POLLOUT when a conection's refused, which is pretty weird. Invert the check to look for an error explicitly. Also add an explict test for this case. Closes #45265.
This commit is contained in:
parent
02a24dbdd8
commit
cab99a3f8c
2 changed files with 24 additions and 4 deletions
|
@ -1579,6 +1579,21 @@ mod tests {
|
||||||
"bad error: {} {:?}", e, e.kind());
|
"bad error: {} {:?}", e, e.kind());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn connect_timeout_unbound() {
|
||||||
|
// bind and drop a socket to track down a "probably unassigned" port
|
||||||
|
let socket = TcpListener::bind("127.0.0.1:0").unwrap();
|
||||||
|
let addr = socket.local_addr().unwrap();
|
||||||
|
drop(socket);
|
||||||
|
|
||||||
|
let timeout = Duration::from_secs(1);
|
||||||
|
let e = TcpStream::connect_timeout(&addr, timeout).unwrap_err();
|
||||||
|
assert!(e.kind() == io::ErrorKind::ConnectionRefused ||
|
||||||
|
e.kind() == io::ErrorKind::TimedOut ||
|
||||||
|
e.kind() == io::ErrorKind::Other,
|
||||||
|
"bad error: {} {:?}", e, e.kind());
|
||||||
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn connect_timeout_valid() {
|
fn connect_timeout_valid() {
|
||||||
let listener = TcpListener::bind("127.0.0.1:0").unwrap();
|
let listener = TcpListener::bind("127.0.0.1:0").unwrap();
|
||||||
|
|
|
@ -176,11 +176,16 @@ impl Socket {
|
||||||
}
|
}
|
||||||
0 => {}
|
0 => {}
|
||||||
_ => {
|
_ => {
|
||||||
if pollfd.revents & libc::POLLOUT == 0 {
|
// linux returns POLLOUT|POLLERR|POLLHUP for refused connections (!), so look
|
||||||
if let Some(e) = self.take_error()? {
|
// for POLLHUP rather than read readiness
|
||||||
|
if pollfd.revents & libc::POLLHUP != 0 {
|
||||||
|
let e = self.take_error()?
|
||||||
|
.unwrap_or_else(|| {
|
||||||
|
io::Error::new(io::ErrorKind::Other, "no error set after POLLHUP")
|
||||||
|
});
|
||||||
return Err(e);
|
return Err(e);
|
||||||
}
|
}
|
||||||
}
|
|
||||||
return Ok(());
|
return Ok(());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue