Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix fill_buf by not calling poll_fill_buf twice #4084

Merged
merged 3 commits into from
Aug 31, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 7 additions & 6 deletions tokio/src/io/util/fill_buf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,15 +34,16 @@ impl<'a, R: AsyncBufRead + ?Sized + Unpin> Future for FillBuf<'a, R> {
fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
let me = self.project();

// Due to a limitation in the borrow-checker, we cannot return the value
// directly on Ready. Once Rust starts using the polonius borrow checker,
// this can be simplified.
let reader = me.reader.take().expect("Polled after completion.");
match Pin::new(&mut *reader).poll_fill_buf(cx) {
Poll::Ready(_) => match Pin::new(reader).poll_fill_buf(cx) {
Poll::Ready(slice) => Poll::Ready(slice),
Poll::Pending => panic!("poll_fill_buf returned Pending while having data"),
Poll::Ready(Ok(slice)) => unsafe {
// Safety: This is necessary only due to a limitation in the
// borrow checker. Once Rust starts using the polonius borrow
// checker, this can be simplified.
let slice = std::mem::transmute::<&[u8], &'a [u8]>(slice);
Poll::Ready(Ok(slice))
},
Poll::Ready(Err(err)) => Poll::Ready(Err(err)),
Poll::Pending => {
*me.reader = Some(reader);
Poll::Pending
Expand Down
34 changes: 34 additions & 0 deletions tokio/tests/io_fill_buf.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
#![warn(rust_2018_idioms)]
#![cfg(feature = "full")]

use tempfile::NamedTempFile;
use tokio::fs::File;
use tokio::io::{AsyncBufReadExt, BufReader};
use tokio_test::assert_ok;

#[tokio::test]
async fn fill_buf_file() {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

take it or leave it: maybe worth a comment summarizing the bug this is a regression test for (and a link to the original issue?)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't really feel like waiting for CI over again, since I am making a release immediately after merging this.

let file = NamedTempFile::new().unwrap();

assert_ok!(std::fs::write(file.path(), b"hello"));

let file = assert_ok!(File::open(file.path()).await);
let mut file = BufReader::new(file);

let mut contents = Vec::new();

loop {
let consumed = {
let buffer = assert_ok!(file.fill_buf().await);
if buffer.is_empty() {
break;
}
contents.extend_from_slice(buffer);
buffer.len()
};

file.consume(consumed);
}

assert_eq!(contents, b"hello");
}