Skip to content

Commit

Permalink
bugfix: Remove erroneous wake call in SinkWriter
Browse files Browse the repository at this point in the history
Per review comment #5070 (comment):
> Adding a wake call here will be a busy loop that consumes 100% CPU
> waiting for it to become ready. We shouldn't do that.

Furthermore, according to
https://docs.rs/futures-sink/latest/futures_sink/trait.Sink.html#tymethod.poll_ready,
poll_ready will make sure that the current task is notified.

Discussion: https://discord.com/channels/500028886025895936/500336346770964480/1072534504981418024
  • Loading branch information
funbringer committed Feb 7, 2023
1 parent a7945b4 commit 28d294c
Showing 1 changed file with 7 additions and 14 deletions.
21 changes: 7 additions & 14 deletions tokio-util/src/io/sink_writer.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use futures_core::ready;
use futures_sink::Sink;

use pin_project_lite::pin_project;
Expand Down Expand Up @@ -98,20 +99,12 @@ where
buf: &[u8],
) -> Poll<Result<usize, io::Error>> {
let mut this = self.project();
match this.inner.as_mut().poll_ready(cx) {
Poll::Ready(Ok(())) => {
if let Err(e) = this.inner.as_mut().start_send(buf) {
Poll::Ready(Err(e.into()))
} else {
Poll::Ready(Ok(buf.len()))
}
}
Poll::Ready(Err(e)) => Poll::Ready(Err(e.into())),
Poll::Pending => {
cx.waker().wake_by_ref();
Poll::Pending
}
}

let () = ready!(this.inner.as_mut().poll_ready(cx).map_err(Into::into))?;
Poll::Ready(match this.inner.as_mut().start_send(buf) {
Ok(()) => Ok(buf.len()),
Err(e) => Err(e.into()),
})
}

fn poll_flush(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Result<(), io::Error>> {
Expand Down

0 comments on commit 28d294c

Please sign in to comment.