Skip to content

Commit 6df95fd

Browse files
authored
Merge pull request #519 from rmja/tokio-read0
Handle reading from Tokio with empty buf as a special case
2 parents 53076d8 + ab46bce commit 6df95fd

File tree

2 files changed

+10
-0
lines changed

2 files changed

+10
-0
lines changed

embedded-io-adapters/CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
99

1010
Add unreleased changes here
1111

12+
- Handle reading from `FromTokio` with empty buffer, ensuring `Ok(0)` is always returned.
13+
1214
## 0.6.0 - 2023-10-02
1315

1416
- Add support for adapting `BufRead` from `futures` and `tokio`.

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)