net: handle HUP event with UnixStream (#3898)

Fixes: #3879
This commit is contained in:
Alice Ryhl 2021-06-30 18:39:06 +02:00 committed by GitHub
parent 0531549b6e
commit b877629cb1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 35 additions and 0 deletions

View File

@ -51,6 +51,11 @@ impl UnixStream {
let stream = UnixStream::new(stream)?;
poll_fn(|cx| stream.io.registration().poll_write_ready(cx)).await?;
if let Some(e) = stream.io.take_error()? {
return Err(e);
}
Ok(stream)
}

View File

@ -379,3 +379,33 @@ async fn try_read_buf() -> std::io::Result<()> {
Ok(())
}
// https://github.com/tokio-rs/tokio/issues/3879
#[tokio::test]
#[cfg(not(target_os = "macos"))]
async fn epollhup() -> io::Result<()> {
let dir = tempfile::Builder::new()
.prefix("tokio-uds-tests")
.tempdir()
.unwrap();
let sock_path = dir.path().join("connect.sock");
let listener = UnixListener::bind(&sock_path)?;
let connect = UnixStream::connect(&sock_path);
tokio::pin!(connect);
// Poll `connect` once.
poll_fn(|cx| {
use std::future::Future;
assert_pending!(connect.as_mut().poll(cx));
Poll::Ready(())
})
.await;
drop(listener);
let err = connect.await.unwrap_err();
assert_eq!(err.kind(), io::ErrorKind::ConnectionReset);
Ok(())
}