Lifetime issue while acquiring mutex lock

Below is a snippet of the code. Everything is properly defined. Issue is I get an error, manager_state does not live long enough when I try compiling. Any help appreciated

loop {
            match child.as_mut().unwrap().try_wait() {
                Ok(Some(status)) => {
                    tx.send(WatcherMessage::Stopped {
                        name: name.to_string(),
                        output: std::process::Output {
                            status,
                            stdout: Vec::new(),
                            stderr: Vec::new(),
                        },
                    })
                    .unwrap();
                    break;
                }
                Ok(None) => {
                    let manager_state = state.lock().unwrap();
                    let mutex = manager_state.mutex.lock().unwrap();
                    // Wait for the condvar to be notified
                    match manager_state.condvar.wait(mutex) {
                        Ok(_) => {
                            println!("condvar notified");
                            child.as_mut().unwrap().kill().unwrap();
                        }
                        Err(e) => {
                            println!("error attempting to wait: {e}");
                            break;
                        }
                    }
                }
                Err(e) => {
                    println!("error attempting to wait: {e}");
                    break;
                }
            }
        }

Rust compiler errors have more info than just a single message. There are hints and lots more info. Please read it all and also post it all here. To get the full message do cargo build in the terminal and post the full output.

2 Likes

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.