From cfb790e88b6b4efaeb55c0b1d0fb527bc8f10c21 Mon Sep 17 00:00:00 2001 From: Przemyslaw Walski Date: Wed, 10 Jan 2024 16:29:36 +0100 Subject: [PATCH] Runtime process termination on Windows using CTRL-BREAK --- agent/provider/Cargo.toml | 2 +- agent/provider/src/signal.rs | 3 +-- utils/process/src/lib.rs | 4 ++-- 3 files changed, 4 insertions(+), 5 deletions(-) diff --git a/agent/provider/Cargo.toml b/agent/provider/Cargo.toml index 0c4d900ca6..0a5969c226 100644 --- a/agent/provider/Cargo.toml +++ b/agent/provider/Cargo.toml @@ -64,7 +64,7 @@ strum = "0.24" strum_macros = "0.24" sys-info = "0.8.0" thiserror = "1.0.14" -tokio = { version = "1", features = ["process", "signal"] } +tokio = { version = "1", features = ["process", "signal", "macros"] } tokio-stream = { version = "0.1.6", features = ["sync"] } url = "2.1.1" walkdir = "2.3.1" diff --git a/agent/provider/src/signal.rs b/agent/provider/src/signal.rs index 48d02bb3af..a4804d6bd7 100644 --- a/agent/provider/src/signal.rs +++ b/agent/provider/src/signal.rs @@ -1,6 +1,5 @@ pub(crate) type Signal = &'static str; -use tokio::signal::windows::CtrlBreak; use tokio::task::JoinHandle; use tokio::{ select, @@ -13,7 +12,7 @@ use tokio::{ #[cfg(target_family = "unix")] use tokio::signal::unix; #[cfg(target_family = "windows")] -use tokio::signal::windows; +use tokio::signal::{windows, windows::CtrlBreak}; pub struct SignalMonitor { stop_tx: Sender, diff --git a/utils/process/src/lib.rs b/utils/process/src/lib.rs index c52fb8a046..76f5e06030 100644 --- a/utils/process/src/lib.rs +++ b/utils/process/src/lib.rs @@ -20,7 +20,7 @@ use { use { winapi::um::handleapi::CloseHandle, winapi::um::processthreadsapi::OpenProcess, - winapi::um::wincon::{GenerateConsoleCtrlEvent, CTRL_C_EVENT}, + winapi::um::wincon::{GenerateConsoleCtrlEvent, CTRL_BREAK_EVENT}, winapi::um::winnt::{PROCESS_QUERY_INFORMATION, PROCESS_TERMINATE, SYNCHRONIZE}, }; @@ -143,7 +143,7 @@ impl ProcessHandle { )); } - let event_result = GenerateConsoleCtrlEvent(CTRL_C_EVENT, process_pid); + let event_result = GenerateConsoleCtrlEvent(CTRL_BREAK_EVENT, process_pid); if event_result == 0 { return Err(anyhow!(