Why does the newly created channel close?

I have code a test, every thread create a channel, and asynchronous receiver message,look this:

fn test(res:Receiver){

let res = res.recv();

if res.is_err(){




fn main() {



    std::thread::spawn(move ||{

        let (mut sender,mut res) = std::sync::mpsc::sync_channel(1);



the error message is:"receiving on a closed channel"
why every channel was closed when it created?I do not understand,someone konw?pls help, thanks!

Because you are dropping the sender immediately.
If there is no sender remaining, the channel is closed.

oh, u are right!thanks,so how could I make sender is alive?I think loop send like the heartbeat is bad idea?

If you want to send a heartbeat, then the channel needs to be created outside of the loop.


  1. create channel
  2. spawn thread that sends a message to the sender every second.
  3. receive messages from the channel in the main thread

I make one clone of sender with receiver to make sender alive as long as receiver, when receiver was drop, the sender was drop too, but it is a bad way?!

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.