diff --git a/tokio/src/io/util/chain.rs b/tokio/src/io/util/chain.rs index 9f1912771..1d0ce4ef0 100644 --- a/tokio/src/io/util/chain.rs +++ b/tokio/src/io/util/chain.rs @@ -94,7 +94,9 @@ where if !*me.done_first { let rem = buf.remaining(); ready!(me.first.poll_read(cx, buf))?; - if buf.remaining() == rem { + // A read that fills nothing only indicates EOF if the buffer + // had capacity to begin with. + if buf.remaining() == rem && rem != 0 { *me.done_first = true; } else { return Poll::Ready(Ok(())); diff --git a/tokio/tests/io_chain.rs b/tokio/tests/io_chain.rs index e2d59411a..70398295b 100644 --- a/tokio/tests/io_chain.rs +++ b/tokio/tests/io_chain.rs @@ -14,3 +14,15 @@ async fn chain() { assert_ok!(rd.read_to_end(&mut buf).await); assert_eq!(buf, b"hello world"); } + +#[tokio::test] +async fn empty_read_does_not_skip_first() { + let mut buf = Vec::new(); + let rd1: &[u8] = b"hello "; + let rd2: &[u8] = b"world"; + + let mut rd = rd1.chain(rd2); + assert_eq!(assert_ok!(rd.read(&mut []).await), 0); + assert_ok!(rd.read_to_end(&mut buf).await); + assert_eq!(buf, b"hello world"); +}