Skip to content

Commit 1f9da53

Browse files
committed
Handle reading from Tokio with empty buf as a special case
1 parent 53076d8 commit 1f9da53

File tree

1 file changed

+8
-0
lines changed

1 file changed

+8
-0
lines changed

embedded-io-adapters/src/tokio_1.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,14 @@ impl<T: ?Sized> embedded_io::ErrorType for FromTokio<T> {
4242

4343
impl<T: tokio::io::AsyncRead + Unpin + ?Sized> embedded_io_async::Read for FromTokio<T> {
4444
async fn read(&mut self, buf: &mut [u8]) -> Result<usize, Self::Error> {
45+
// The current tokio implementation (https://github.com/tokio-rs/tokio/blob/tokio-1.33.0/tokio/src/io/poll_evented.rs#L165)
46+
// does not consider the case of buf.is_empty() as a special case,
47+
// which can cause Poll::Pending to be returned at the end of the stream when called with an empty buffer.
48+
// This poll will, however, never become ready, as no more bytes will be received.
49+
if buf.is_empty() {
50+
return Ok(0);
51+
}
52+
4553
poll_fn(|cx| {
4654
let mut buf = tokio::io::ReadBuf::new(buf);
4755
match Pin::new(&mut self.inner).poll_read(cx, &mut buf) {

0 commit comments

Comments
 (0)