Skip to content

Commit

Permalink
net: handle HUP event with UnixStream (#3898)
Browse files Browse the repository at this point in the history
Fixes: #3879
  • Loading branch information
Darksonn authored Jun 30, 2021
1 parent 0531549 commit b877629
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 0 deletions.
5 changes: 5 additions & 0 deletions tokio/src/net/unix/stream.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand Down
30 changes: 30 additions & 0 deletions tokio/tests/uds_stream.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(())
}

0 comments on commit b877629

Please sign in to comment.