fix: don't crash when sending signals to child processes fails

This commit is contained in:
Gabriele Musco 2024-08-06 17:24:53 +02:00
parent 3e21ca2eb8
commit 6d0d270254
2 changed files with 15 additions and 6 deletions

View file

@ -252,6 +252,9 @@ impl App {
}
pub fn shutdown_xrservice(&mut self) {
if let Some(worker) = self.autostart_worker.as_ref() {
worker.stop();
}
self.xrservice_ready = false;
if let Some(w) = self.openxr_prober_worker.as_ref() {
w.stop();
@ -262,9 +265,6 @@ impl App {
if let Some(worker) = self.xrservice_worker.as_ref() {
worker.stop();
}
if let Some(worker) = self.autostart_worker.as_ref() {
worker.stop();
}
self.libmonado = None;
self.main_view
.sender()

View file

@ -89,17 +89,26 @@ impl JobWorker {
}
pub fn stop(&self) {
if self.state.lock().unwrap().started && !self.state.lock().unwrap().exited {
let should_stop_manually = {
let state = self.state.lock().unwrap();
state.started && !state.exited
};
if should_stop_manually {
self.state.lock().unwrap().stop_requested = true;
if let Some(pid) = self.state.lock().unwrap().current_pid {
kill(pid, SIGTERM).expect("Could not send sigterm to process");
if let Err(e) = kill(pid, SIGTERM) {
eprintln!("Failed to send SIGTERM: {e:#?}");
}
let state = self.state.clone();
thread::spawn(move || {
sleep(Duration::from_secs(2));
if let Ok(s) = state.lock() {
if !s.exited {
// process is still alive
kill(pid, SIGKILL).expect("Failed to kill process");
eprintln!("Process is still alive 2 seconds after SIGTERM, proceeding to send SIGKILL...");
if let Err(e) = kill(pid, SIGKILL) {
eprintln!("Failed to send SIGKILL: {e:#?}");
};
}
}
});