From b877629cb1678132611211b43b4b9e1b879e6b20 Mon Sep 17 00:00:00 2001 From: Alice Ryhl Date: Wed, 30 Jun 2021 18:39:06 +0200 Subject: [PATCH] net: handle HUP event with UnixStream (#3898) Fixes: #3879 --- tokio/src/net/unix/stream.rs | 5 +++++ tokio/tests/uds_stream.rs | 30 ++++++++++++++++++++++++++++++ 2 files changed, 35 insertions(+) diff --git a/tokio/src/net/unix/stream.rs b/tokio/src/net/unix/stream.rs index 33030cb69..4baac6062 100644 --- a/tokio/src/net/unix/stream.rs +++ b/tokio/src/net/unix/stream.rs @@ -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) } diff --git a/tokio/tests/uds_stream.rs b/tokio/tests/uds_stream.rs index 2754e8438..5f1b4cffb 100644 --- a/tokio/tests/uds_stream.rs +++ b/tokio/tests/uds_stream.rs @@ -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(()) +}