Skip to content

Commit

Permalink
fix: return write-zero error when write return 0
Browse files Browse the repository at this point in the history
  • Loading branch information
quininer committed Dec 3, 2024
1 parent 66fb0ae commit 818dc3d
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 2 deletions.
10 changes: 8 additions & 2 deletions src/common/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,8 @@ where

while self.session.wants_write() {
match self.write_io(cx) {
Poll::Ready(Ok(0)) =>
return Poll::Ready(Err(io::ErrorKind::WriteZero.into())),
Poll::Ready(Ok(n)) => {
wrlen += n;
need_flush = true;
Expand Down Expand Up @@ -322,14 +324,18 @@ where
fn poll_flush(mut self: Pin<&mut Self>, cx: &mut Context) -> Poll<io::Result<()>> {
self.session.writer().flush()?;
while self.session.wants_write() {
ready!(self.write_io(cx))?;
if ready!(self.write_io(cx))? == 0 {
return Poll::Ready(Err(io::ErrorKind::WriteZero.into()));
}
}
Pin::new(&mut self.io).poll_flush(cx)
}

fn poll_shutdown(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<io::Result<()>> {
while self.session.wants_write() {
ready!(self.write_io(cx))?;
if ready!(self.write_io(cx))? == 0 {
return Poll::Ready(Err(io::ErrorKind::WriteZero.into()));
}
}

Poll::Ready(match ready!(Pin::new(&mut self.io).poll_shutdown(cx)) {
Expand Down
66 changes: 66 additions & 0 deletions src/common/test_stream.rs
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,36 @@ impl AsyncWrite for Expected {
}
}

struct Eof;

impl AsyncRead for Eof {
fn poll_read(
self: Pin<&mut Self>,
_cx: &mut Context<'_>,
_buf: &mut ReadBuf<'_>,
) -> Poll<io::Result<()>> {
Poll::Ready(Ok(()))
}
}

impl AsyncWrite for Eof {
fn poll_write(
self: Pin<&mut Self>,
_cx: &mut Context<'_>,
_buf: &[u8],
) -> Poll<io::Result<usize>> {
Poll::Ready(Ok(0))
}

fn poll_flush(self: Pin<&mut Self>, _cx: &mut Context<'_>) -> Poll<io::Result<()>> {
Poll::Ready(Ok(()))
}

fn poll_shutdown(self: Pin<&mut Self>, _cx: &mut Context<'_>) -> Poll<io::Result<()>> {
Poll::Ready(Ok(()))
}
}

#[tokio::test]
async fn stream_good() -> io::Result<()> {
stream_good_impl(false).await
Expand Down Expand Up @@ -254,6 +284,23 @@ async fn stream_handshake_eof() -> io::Result<()> {
Ok(()) as io::Result<()>
}

#[tokio::test]
async fn stream_handshake_write_eof() -> io::Result<()> {
let (_, mut client) = make_pair();

let mut io = Eof;
let mut stream = Stream::new(&mut io, &mut client);

let mut cx = Context::from_waker(noop_waker_ref());
let r = stream.handshake(&mut cx);
assert_eq!(
r.map_err(|err| err.kind()),
Poll::Ready(Err(io::ErrorKind::WriteZero))
);

Ok(()) as io::Result<()>
}

// see https://github.com/tokio-rs/tls/issues/77
#[tokio::test]
async fn stream_handshake_regression_issues_77() -> io::Result<()> {
Expand Down Expand Up @@ -291,6 +338,25 @@ async fn stream_eof() -> io::Result<()> {
Ok(()) as io::Result<()>
}

#[tokio::test]
async fn stream_write_zero() -> io::Result<()> {
let (server, mut client) = make_pair();
let mut server = Connection::from(server);
poll_fn(|cx| do_handshake(&mut client, &mut server, cx)).await?;

let mut io = Eof;
let mut stream = Stream::new(&mut io, &mut client);

stream.write(b"1").await.unwrap();
let result = stream.shutdown().await;
assert_eq!(
result.err().map(|e| e.kind()),
Some(io::ErrorKind::WriteZero)
);

Ok(()) as io::Result<()>
}

fn make_pair() -> (ServerConnection, ClientConnection) {
let (sconfig, cconfig) = utils::make_configs();
let server = ServerConnection::new(Arc::new(sconfig)).unwrap();
Expand Down

0 comments on commit 818dc3d

Please sign in to comment.