Make sure to kill the old command group before starting a new one

This commit is contained in:
Félix Saparelli 2021-07-21 21:42:55 +12:00
parent 9c20c8c8b5
commit 72cda2b0b0
No known key found for this signature in database
GPG Key ID: B948C4BAE44FC474
2 changed files with 20 additions and 9 deletions

View File

@ -2,12 +2,12 @@
use crate::error::Result; use crate::error::Result;
use crate::pathop::PathOp; use crate::pathop::PathOp;
use command_group::{CommandGroup, GroupChild};
use std::{ use std::{
collections::{HashMap, HashSet}, collections::{HashMap, HashSet},
path::PathBuf, path::PathBuf,
process::Command, process::Command,
}; };
use command_group::{CommandGroup, GroupChild};
/// Shell to use to run commands. /// Shell to use to run commands.
/// ///
@ -134,7 +134,7 @@ pub fn spawn(
collect_path_env_vars(updated_paths) collect_path_env_vars(updated_paths)
}; };
for &(ref name, ref val) in &command_envs { for (name, val) in &command_envs {
command.env(name, val); command.env(name, val);
} }

View File

@ -164,14 +164,18 @@ impl ExecHandler {
Signal::SIGCHLD => { Signal::SIGCHLD => {
debug!("Try-waiting on command"); debug!("Try-waiting on command");
child.try_wait().ok(); child.try_wait().ok();
}, }
_ => { _ => {
#[cfg(unix)] #[cfg(unix)]
child.signal(sig).unwrap_or_else(|err| warn!("Could not pass on signal to command: {}", err)); child.signal(sig).unwrap_or_else(|err| {
warn!("Could not pass on signal to command: {}", err)
});
#[cfg(not(unix))] #[cfg(not(unix))]
child.kill().unwrap_or_else(|err| warn!("Could not pass on termination to command: {}", err)); child.kill().unwrap_or_else(|err| {
}, warn!("Could not pass on termination to command: {}", err)
});
}
} }
} }
} }
@ -189,8 +193,13 @@ impl ExecHandler {
clearscreen::clear()?; clearscreen::clear()?;
} }
debug!("Launching command");
let mut guard = self.child_process.lock()?; let mut guard = self.child_process.lock()?;
if let Some(child) = guard.as_mut() {
debug!("Killing process group id={}", child.id());
child.kill()?;
}
debug!("Launching command");
*guard = Some(process::spawn( *guard = Some(process::spawn(
&self.args.cmd, &self.args.cmd,
ops, ops,
@ -346,12 +355,14 @@ fn signal_process(process: &Mutex<Option<GroupChild>>, signal: Signal) -> Result
let mut guard = process.lock().expect("poisoned lock in signal_process"); let mut guard = process.lock().expect("poisoned lock in signal_process");
if let Some(child) = guard.as_mut() { if let Some(child) = guard.as_mut() {
#[cfg(unix)] { #[cfg(unix)]
{
debug!("Signaling process with {}", signal); debug!("Signaling process with {}", signal);
child.signal(signal)?; child.signal(signal)?;
} }
#[cfg(not(unix))] { #[cfg(not(unix))]
{
if matches!(signal, Signal::SIGTERM | Signal::SIGKILL) { if matches!(signal, Signal::SIGTERM | Signal::SIGKILL) {
debug!("Killing process"); debug!("Killing process");
child.kill()?; child.kill()?;