Skip to content

Commit

Permalink
FillBuf: don't poll a second time on EOF
Browse files Browse the repository at this point in the history
There is no hard guarantee that polling a second time will return
Poll::Ready, and this is particularly likely to break in the EOF case,
which is precisely where we don't need to do so at all.

Both tokio::io::BufReader and futures::io::BufReader always attempt to
read from the underlying reader when the buffer is empty, rather than
fusing EOF.
  • Loading branch information
edef1c authored and taiki-e committed Nov 20, 2023
1 parent f75c4c1 commit 0fe4b88
Showing 1 changed file with 2 additions and 0 deletions.
2 changes: 2 additions & 0 deletions futures-util/src/io/fill_buf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ where
let reader = this.reader.take().expect("Polled FillBuf after completion");

match Pin::new(&mut *reader).poll_fill_buf(cx) {
// We don't need to poll a second time for EOF, and doing so is likely to return Poll::Pending
Poll::Ready(Ok(&[])) => Poll::Ready(Ok(&[])),
// With polonius it is possible to remove this inner match and just have the correct
// lifetime of the reference inferred based on which branch is taken
Poll::Ready(Ok(_)) => match Pin::new(reader).poll_fill_buf(cx) {
Expand Down

0 comments on commit 0fe4b88

Please sign in to comment.