From 1f9da539b462c5b50c79967fd6abb80b9a20c533 Mon Sep 17 00:00:00 2001 From: Rasmus Melchior Jacobsen Date: Mon, 6 Nov 2023 08:57:39 +0100 Subject: [PATCH 1/2] Handle reading from Tokio with empty buf as a special case --- embedded-io-adapters/src/tokio_1.rs | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/embedded-io-adapters/src/tokio_1.rs b/embedded-io-adapters/src/tokio_1.rs index 9369efe07..263229c5f 100644 --- a/embedded-io-adapters/src/tokio_1.rs +++ b/embedded-io-adapters/src/tokio_1.rs @@ -42,6 +42,14 @@ impl embedded_io::ErrorType for FromTokio { impl embedded_io_async::Read for FromTokio { async fn read(&mut self, buf: &mut [u8]) -> Result { + // The current tokio implementation (https://github.com/tokio-rs/tokio/blob/tokio-1.33.0/tokio/src/io/poll_evented.rs#L165) + // does not consider the case of buf.is_empty() as a special case, + // which can cause Poll::Pending to be returned at the end of the stream when called with an empty buffer. + // This poll will, however, never become ready, as no more bytes will be received. + if buf.is_empty() { + return Ok(0); + } + poll_fn(|cx| { let mut buf = tokio::io::ReadBuf::new(buf); match Pin::new(&mut self.inner).poll_read(cx, &mut buf) { From ab46bcece0ce01b243ccc4d7b84ea4f0a7a11f5a Mon Sep 17 00:00:00 2001 From: Rasmus Melchior Jacobsen Date: Mon, 6 Nov 2023 13:52:45 +0100 Subject: [PATCH 2/2] Update CHANGELOG.md --- embedded-io-adapters/CHANGELOG.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/embedded-io-adapters/CHANGELOG.md b/embedded-io-adapters/CHANGELOG.md index a81afe1a9..e9205d681 100644 --- a/embedded-io-adapters/CHANGELOG.md +++ b/embedded-io-adapters/CHANGELOG.md @@ -9,6 +9,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 Add unreleased changes here +- Handle reading from `FromTokio` with empty buffer, ensuring `Ok(0)` is always returned. + ## 0.6.0 - 2023-10-02 - Add support for adapting `BufRead` from `futures` and `tokio`.