Skip to content

Commit

Permalink
Merge pull request nervosnetwork#4422 from eval-exec/exec/windows-kill
Browse files Browse the repository at this point in the history
Fix send Ctrl-C signal on Windows integration test'
  • Loading branch information
quake authored Apr 19, 2024
2 parents 5424e06 + 23cad16 commit 9944b22
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 8 deletions.
3 changes: 3 additions & 0 deletions test/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,9 @@ log = "0.4"
[target.'cfg(not(target_os="windows"))'.dependencies]
nix = { version = "0.24.0", default-features = false, features = ["signal"] }

[target.'cfg(target_os="windows")'.dependencies]
windows-sys = { version = "0.52", features = ["Win32_Foundation", "Win32_System_Threading", "Win32_Security", "Win32_System_Console"] }

# Prevent this from interfering with workspaces
[workspace]
members = ["."]
Expand Down
2 changes: 1 addition & 1 deletion test/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ fn main() {

let cloned_running_names = Arc::clone(&running_spec_names);
ctrlc::set_handler(move || {
std::thread::sleep(Duration::from_secs(1));
std::thread::sleep(Duration::from_secs(10));
warn!(
"Total {} specs are not finished",
cloned_running_names.lock().len()
Expand Down
19 changes: 12 additions & 7 deletions test/src/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ use std::process::{Child, Command, Stdio};
use std::thread::sleep;
use std::time::{Duration, Instant};

#[cfg(target_os = "windows")]
use windows_sys::Win32::System::Console::{GenerateConsoleCtrlEvent, CTRL_C_EVENT};

struct ProcessGuard {
pub name: String,
pub child: Child,
Expand Down Expand Up @@ -704,13 +707,15 @@ impl Node {

#[cfg(target_os = "windows")]
fn kill_gracefully(pid: u32) {
// taskkill /pid 1234
let kill_output = std::process::Command::new("taskkill")
.arg("/pid")
.arg(format!("{}", pid))
.output()
.expect("kill process on windows");
ckb_logger::info!("kill process {}, output: {:?}", pid, kill_output)
unsafe {
let ret = GenerateConsoleCtrlEvent(CTRL_C_EVENT, pid);
if ret == 0 {
let err = std::io::Error::last_os_error();
error!("GenerateConsoleCtrlEvent failed: {}", err);
} else {
info!("GenerateConsoleCtrlEvent success");
}
}
}

#[cfg(not(target_os = "windows"))]
Expand Down

0 comments on commit 9944b22

Please sign in to comment.