Why does the channel close on its own?

In the following code:

    let (config_raw, config_path) = get_config();
    let config_rc = Arc::from(Mutex::new(config_raw));

    let (tx, rx) = channel();
    if let Some(config_path) = config_path {
        let mut watcher: RecommendedWatcher = Watcher::new(tx, Duration::from_secs(5)).unwrap();
        watcher
            .watch(config_path, RecursiveMode::Recursive)
            .unwrap();

        let config_rc = Arc::clone(&config_rc);
        thread::spawn(move || loop {
            match rx.recv() {
                Ok(event) => match event {
                    DebouncedEvent::Write(path) => {
                        if let Ok(conf) = &mut Config::from_file(&path) {
                            let mut config = config_rc.lock().unwrap();
                            println!("Got hands on config: {}", path.display());
                            config.update(conf);
                        }
                    }
                    _ => {}
                },
                Err(e) => println!("{}", e),
            };
        });
    } else {
        warn!("no config file found; choosing to default config");
    }
    // Some more code and an infinite loop

I just get a RecvError while matching rx.recv() with the message receiving on a closed channel. I cannot see why the channel gets closed. Could someone please help me out on this?

watcher is dropped right after the thread is spawned, and you passed ownership of the tx to it, so that will close the channel.

Thanks a bunch!

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.