sync: add mpsc::Sender::closed
future
#2800
Labels
A-tokio
Area: The main tokio crate
C-feature-request
Category: A feature request.
M-sync
Module: tokio/sync
Is your feature request related to a problem? Please describe.
Currently, the
tokio::sync::watch
channel has aSender::closed
async function. This can be awaited in order to notify a task when the receivers of that channel are all closed.However, the
mpsc
channel'sSender
type does not currently have such a function. This would be useful to add.For an example use-case, see linkerd/linkerd2#4926. In essence, the intended use case is to prevent leaks of a task that sends messages over an MPSC channel by ensuring it is woken when the receiver ends of that channel are all gone. Currently, the sender can only become aware of the channel closing when it actually attempts to send a message. Although dropping the receivers would wake up the sender task if it is waiting to send a message (in the bounded MPSC), it won't be woken if it is waiting for some other work...but if there are no longer any receivers, that other work may no longer be necessary, and should be cancelled.
If we added a
closed
future, it would be possible toselect
overclosed
and some other work, so that the task is woken when the channel is closed and can proactively cancel itself.Describe the solution you'd like
We should add a
closed
future, like the one onwatch::Sender
.Describe alternatives you've considered
We could, alternatively, not do this. However, there isn't really any other good way to solve the problem that I can think of.
Additional context
Somewhat related: #2481
The text was updated successfully, but these errors were encountered: