What is the proper way to start a single sidecar and then kill it when the Tauri process ends (on window close). I have the following code, mostly AI gen, and there is an error on the attempt to kill the child process, noted in window event handler. Is there a better way to do this?
`....
let app_handle = app.handle();
match app_handle.shell().sidecar("server") {
Ok(command) => {
let (mut rx, child) = command.spawn()?;
// Store the child process in a thread-safe manner
let child = Arc::new(Mutex::new(child));
app_handle.manage::<SharedChild>(child.clone());
.....
.on_window_event(|window, event| {
if let tauri::WindowEvent::CloseRequested { api, .. } = event {
let app_handle = window.app_handle();
// This is the main window, so we'll shut down the app
api.prevent_close();
// Kill the sidecar process if it exists
if let Some(child_state) = app_handle.try_state::<SharedChild>() {
let child = child_state.lock().expect("Failed to lock child process");
// ERROR -> cannot move out of dereference of `MutexGuard<'_, CommandChild>`move occurs because value has type `CommandChild`, which does not implement the `Copy`
if let Err(e) = child.kill() {
eprintln!("Failed to kill sidecar process: {}", e);
}
}
// Close the window and exit the app
window.close().unwrap();
std::process::exit(0);
}
})
.run(tauri::generate_context!())
.expect("error while running tauri application");
}`