Skip to content

Commit

Permalink
Use SIGTERM to kill child processes on Unix
Browse files Browse the repository at this point in the history
The cross-platform stdlib API uses SIGKILL, but that doesn't allow
ports and resources to be freed. SIGTERM may fail, but that can be
solved by the user themselves.

Fixes #25 (webserver support)
  • Loading branch information
passcod committed Jan 31, 2016
1 parent b71a889 commit 40f34c8
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 4 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 3 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,10 @@ keywords = [
[dependencies]
docopt = "^0.6.78"
env_logger = "^0.3.2"
lazy_static = "0.1.15"
lazy_static = "^0.1.15"
libc = "^0.2.4"
log = "^0.3.4"
notify = "^2.5.4"
regex = "^0.1.44"
rustc-serialize = "^0.3.16"
wait-timeout = "0.1"
wait-timeout = "^0.1.0"
2 changes: 2 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ extern crate docopt;
extern crate env_logger;
#[macro_use]
extern crate lazy_static;
#[cfg(not(windows))]
extern crate libc;
#[macro_use]
extern crate log;
extern crate notify;
Expand Down
17 changes: 15 additions & 2 deletions src/schedule.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
use cargo;
use config;
#[cfg(not(windows))]
use libc;
use notify;
use std::io;
use std::process::Command;
use std::process::{Child, Command};
use std::sync::{Arc, Mutex};
use std::sync::mpsc::{channel, Receiver, Sender};
use std::thread::{self, JoinHandle};
Expand Down Expand Up @@ -154,7 +156,7 @@ fn execute_commands(
if let Ok(_) = kill.try_recv() {
// We got the order to kill the child and terminate
debug!("Killing spawned process");
let _ = child.kill();
kill_child(&mut child);
abort = true;
}

Expand Down Expand Up @@ -188,3 +190,14 @@ fn execute_commands(

debug!("Command run done");
}

#[cfg(windows)]
fn kill_child(child: &mut Child) -> () {
let _ = child.kill();
}

#[cfg(not(windows))]
fn kill_child(child: &mut Child) -> () {
let _ = unsafe { libc::kill(child.id() as i32, libc::SIGTERM) };
}

0 comments on commit 40f34c8

Please sign in to comment.