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

Question about Sink's poll_flush and channels: mpsc senders don't fully flush the buffer before returning Poll::Ready #2504

Open
FSMaxB opened this issue Oct 8, 2021 · 2 comments · May be fixed by #2746

Comments

@FSMaxB
Copy link

FSMaxB commented Oct 8, 2021

Maybe this is just a misunderstanding of what poll_flush actually means, but from the documentation I would expect poll_flush of the Sink trait to only return Poll::Ready once all buffered messages have been received by the Receiver of the channel, meaning the buffer is empty.

/// Returns `Poll::Ready(Ok(()))` when no buffered items remain. If this
/// value is returned then it is guaranteed that all previous values sent
/// via `start_send` have been flushed.

Currently the Sink implementation for Sender and UnboundedSender don't do that though, making poll_flush almost equivalent to poll_ready. See:

fn poll_flush(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
match (*self).poll_ready(cx) {
Poll::Ready(Err(ref e)) if e.is_disconnected() => {
// If the receiver disconnected, we consider the sink to be flushed.
Poll::Ready(Ok(()))
}
x => x,
}
}

In the Unbounded case, it doesn't flush anything at all:

fn poll_flush(self: Pin<&mut Self>, _: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
Poll::Ready(Ok(()))
}

Is this intentional? And if so, how does this relate to the documentation of the Sink trait? Maybe the documentation can be updated to clarify this?

@e00E
Copy link

e00E commented Mar 7, 2022

I wanted to create the same issue. Sender should document what it considers flushed in terms of Sink. The current behavior is that it is flushed when there is space to receive more messages. When the the capacity is 0 this is equivalent to the item having been read but it isn't at higher capacity. Since this is undocumented users might confuse the behavior.

@qutesy
Copy link

qutesy commented Mar 10, 2022

Same here: from the documentation for Sink::poll_flush(), I thought that poll_flush() indicated that all the data had been consumed.

Seeing that mpsc::{Sender,UnboundedSender} can return Poll::Ready when there is data still in the channel was surprising and confusing.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants