Proposal
Problem statement
The mpsc and mpmc channel API's do not support checking if the endpoint is connected, without first attempting to send or receive a message.
Motivating examples or use cases
Imagine you have a thread, which runs the state for a QUIC connection.
Any incoming data this thread produces, is send over a channel to a consumer.
If the consumer panics or otherwise drops the Receiver, the thread will unknowingly do work keeping the connection alive.
Only will it learn that the channel has been disconnected, when it tries to send data.
Instead, it could check tx.is_connected() in its main loop to determine if it's Receiver has disconnected.
If so, it can terminate its own connection immediately.
Solution sketch
std::sync::mpmc::Sender<T>::is_disconnected(&self) -> bool
std::sync::mpmc::Receiver<T>::is_disconnected(&self) -> bool
std::sync::mpsc::Sender<T>::is_disconnected(&self) -> bool
std::sync::mpsc::Receiver<T>::is_disconnected(&self) -> bool
The mpsc methods would be behind feature gate mpsc_is_disconnected.
The mpmc methods can be added to the existing mpmc_channel feature gate.
The mpmc implementation may be as simple as:
pub fn is_disconnected(&self) -> bool {
match &self.flavor {
ReceiverFlavor::Array(chan) => chan.is_disconnected(),
ReceiverFlavor::List(chan) => chan.is_disconnected(),
ReceiverFlavor::Zero(chan) => chan.is_disconnected(),
}
}
The is_disconnected method for the Zero flavor may be implemented by reading its inner is_disconnected field.
The is_diconnected method is already implemented for the other two flavors.
Alternatives
As an alternative, either end of the channel may hold a Weak reference to the other. If this reference fails to upgrade, the other endpoint must have dropped.
Links and related work
PR: rust-lang/rust#153170
How to tell if a channel’s sender still has a valid receiver?
What happens now?
This issue contains an API change proposal (or ACP) and is part of the libs-api team feature lifecycle. Once this issue is filed, the libs-api team will review open proposals as capability becomes available. Current response times do not have a clear estimate, but may be up to several months.
Possible responses
The libs team may respond in various different ways. First, the team will consider the problem (this doesn't require any concrete solution or alternatives to have been proposed):
- We think this problem seems worth solving, and the standard library might be the right place to solve it.
- We think that this probably doesn't belong in the standard library.
Second, if there's a concrete solution:
- We think this specific solution looks roughly right, approved, you or someone else should implement this. (Further review will still happen on the subsequent implementation PR.)
- We're not sure this is the right solution, and the alternatives or other materials don't give us enough information to be sure about that. Here are some questions we have that aren't answered, or rough ideas about alternatives we'd want to see discussed.
Proposal
Problem statement
The
mpscandmpmcchannel API's do not support checking if the endpoint is connected, without first attempting to send or receive a message.Motivating examples or use cases
Imagine you have a thread, which runs the state for a QUIC connection.
Any incoming data this thread produces, is send over a channel to a consumer.
If the consumer panics or otherwise drops the Receiver, the thread will unknowingly do work keeping the connection alive.
Only will it learn that the channel has been disconnected, when it tries to send data.
Instead, it could check
tx.is_connected()in its main loop to determine if it's Receiver has disconnected.If so, it can terminate its own connection immediately.
Solution sketch
The
mpscmethods would be behind feature gatempsc_is_disconnected.The
mpmcmethods can be added to the existingmpmc_channelfeature gate.The
mpmcimplementation may be as simple as:The
is_disconnectedmethod for the Zero flavor may be implemented by reading its inneris_disconnectedfield.The
is_diconnectedmethod is already implemented for the other two flavors.Alternatives
As an alternative, either end of the channel may hold a
Weakreference to the other. If this reference fails to upgrade, the other endpoint must have dropped.Links and related work
PR: rust-lang/rust#153170
How to tell if a channel’s sender still has a valid receiver?
What happens now?
This issue contains an API change proposal (or ACP) and is part of the libs-api team feature lifecycle. Once this issue is filed, the libs-api team will review open proposals as capability becomes available. Current response times do not have a clear estimate, but may be up to several months.
Possible responses
The libs team may respond in various different ways. First, the team will consider the problem (this doesn't require any concrete solution or alternatives to have been proposed):
Second, if there's a concrete solution: