smol-rs / async-channel

Async multi-producer multi-consumer channel
Apache License 2.0
726 stars 38 forks source link

Sending cancel safety #87

Open BigBigos opened 3 months ago

BigBigos commented 3 months ago

I believe Sender::send() is not cancel safe in regard that it takes ownership of the value to be sent. If the future is cancelled, the value is gone with no way to recover it. This makes using it inconvenient within the select! loop.

tokio's mpsc::Sender suggests using reserve() and Permit::send() to resolve this problem. reserve() returns a future, but it does not consume any value by itself. Permit::send() then can be used to send the value without any blocking.

Can a similar API be implemented for async-channel? Is that feasible?

[1] https://docs.rs/tokio/latest/tokio/macro.select.html [2] https://docs.rs/tokio/latest/tokio/sync/mpsc/struct.Sender.html#cancel-safety

notgull commented 3 months ago

Support would need to be added to the underlying concurrent-queue crate... which may be a lot harder in practice. I'd accept PRs but we would first need to come up with the underlying algorithm.