futures-rs icon indicating copy to clipboard operation
futures-rs copied to clipboard

Only return `Poll::Ready` from `Sender::poll_flush` if the channel is empty

Open thomaseizinger opened this issue 1 year ago • 6 comments

I've found it to be very surprising behaviour that poll_flush on a Sender returns Poll::Ready despite the channel not being empty. This PR attempts to fix this by checking the number of messages in the channel and parking the task if is > 0.

Resolves: https://github.com/rust-lang/futures-rs/issues/2504.

thomaseizinger avatar May 22 '23 10:05 thomaseizinger

Miri is detecting a deadlock but I am not quite sure why. Help is much appreciated.

thomaseizinger avatar May 22 '23 13:05 thomaseizinger

Okay, I think I found the problem. SinkExt::send implies flush which caused a deadlock if we have a task that suspends on send and doesn't at the same time read from the receiver.

I've replaced that with feed which makes it work again.

This makes me wonder whether this might perhaps be a fairly intrusive change for the ecosystem. I still think that this is the more correct behaviour but I wonder, how many people are relying on SinkExt::send essentially being equivalent to SinkExt::feed.

thomaseizinger avatar May 22 '23 13:05 thomaseizinger

Not sure why https://github.com/rust-lang/futures-rs/actions/runs/5046514587/jobs/9052136457?pr=2746#step:5:326 is failing. It passes locally. Help would be appreciated.

thomaseizinger avatar May 22 '23 13:05 thomaseizinger

Thanks for the PR.

Not sure why https://github.com/rust-lang/futures-rs/actions/runs/5046514587/jobs/9052136457?pr=2746#step:5:326 is failing.

armv7 failure is unrelated to this PR (should be fixed by rebase).

This makes me wonder whether this might perhaps be a fairly intrusive change for the ecosystem.

Yeah, I'm not open to introducing a patch that may introduce hard-to-debug deadlocks. (even if it is in a breaking release)

taiki-e avatar Jul 19 '23 16:07 taiki-e

This makes me wonder whether this might perhaps be a fairly intrusive change for the ecosystem.

Yeah, I'm not open to introducing a patch that may introduce hard-to-debug deadlocks. (even if it is in a breaking release)

To be fair, it only occurs if both sender and receiver are polled by the same task. I think that is a rather unlikely situation because you could also just push and pop a Vec then instead of using a channel.

thomaseizinger avatar Jul 19 '23 16:07 thomaseizinger

Any chance this will be merged someday?

0x7CFE avatar May 16 '24 11:05 0x7CFE