Skip to content

Commit 545cc4d

Browse files
Handle failure of OpenProcess
1 parent 7469199 commit 545cc4d

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed

src/native/windows.rs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@ use winapi::um::winnt::PROCESS_TERMINATE;
88
pub(crate) fn kill_process(id: u32) -> Fallible<()> {
99
unsafe {
1010
let handle = OpenProcess(PROCESS_TERMINATE, 0, id);
11+
if handle.is_null() {
12+
bail!("OpenProcess for process {} failed", id);
13+
}
1114
if TerminateProcess(handle, 101) == 0 {
1215
bail!("TerminateProcess for process {} failed", id);
1316
}
@@ -45,3 +48,20 @@ pub(crate) fn make_executable<P: AsRef<Path>>(path: P) -> Fallible<()> {
4548
failure::bail!("Downloaded binaries should be executable by default");
4649
}
4750
}
51+
52+
53+
#[cfg(test)]
54+
mod tests {
55+
use super::*;
56+
use std::process::Command;
57+
58+
#[test]
59+
fn test_kill_process() {
60+
// Try to kill a sleep command
61+
let mut cmd = Command::new("timeout").args(&["2"]).spawn().unwrap();
62+
kill_process(cmd.id()).unwrap();
63+
64+
// Ensure it returns the code passed to `TerminateProcess`
65+
assert_eq!(cmd.wait().unwrap().code(), Some(101));
66+
}
67+
}

0 commit comments

Comments
 (0)