Skip to content

Commit 50c7d57

Browse files
committed
Windows Fix
1 parent a1c3979 commit 50c7d57

File tree

2 files changed

+10
-12
lines changed

2 files changed

+10
-12
lines changed

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ libc = "0.2" # For setpgid and signal constants
2222
tracing-subscriber = { version = "0.3", features = ["env-filter", "fmt"] } # For logging setup
2323
tempfile = "3" # For creating temporary directories in examples/tests
2424
anyhow = "1.0" # For simple error handling in examples
25-
windows-sys = { version = "0.59.0", features = ["Win32", "Win32_System", "Win32_System_Threading"] }
25+
windows-sys = { version = "0.59.0", features = ["Win32", "Win32_System", "Win32_System_Threading", "Win32_Foundation"] }
2626
# Dependency on the library itself (needed for building examples within the workspace)
2727
# This line might not be strictly necessary if cargo automatically detects it,
2828
# but explicitly listing it can sometimes help.

src/lib.rs

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,8 @@ use tracing::{debug, instrument, warn};
1313
// --- Add nix imports ---
1414
#[cfg(unix)]
1515
use nix::sys::signal::{killpg, Signal};
16-
#[cfg(windows)]
17-
use windows_sys::Win32::System::Threading::{OpenProcess, TerminateProcess, PROCESS_TERMINATE};
1816
#[cfg(unix)]
1917
use nix::unistd::Pid;
20-
#[cfg(windows)]
21-
use windows_sys::Win32::Foundation::HANDLE;
2218

2319
// --- End add ---
2420

@@ -272,13 +268,12 @@ async fn handle_timeout_event(
272268
"Killing process group due to timeout"
273269
);
274270
// Convert u32 PID to nix's Pid type (i32)
275-
#[cfg(unix)]
276-
let pid = Pid::from_raw(pid_u32 as i32);
277-
#[cfg(windows)]
278-
let pid = HANDLE::from(pid_u32 as *mut c_void);
279271
// Send SIGKILL to the entire process group.
280272
// killpg takes the PID of any process in the group (usually the leader)
281273
// and signals the entire group associated with that process.
274+
275+
#[cfg(unix)]
276+
let pid = Pid::from_raw(pid_u32 as i32);
282277
#[cfg(unix)]
283278
match killpg(pid, Signal::SIGKILL) {
284279
Ok(()) => {
@@ -323,13 +318,16 @@ async fn handle_timeout_event(
323318
}
324319
#[cfg(windows)]
325320
{
321+
use std::ffi::c_void;
326322
use windows_sys::Win32::Foundation::{CloseHandle, HANDLE};
323+
use windows_sys::Win32::Foundation::INVALID_HANDLE_VALUE;
327324
use windows_sys::Win32::System::Threading::{OpenProcess, TerminateProcess, PROCESS_TERMINATE};
325+
use windows_sys::Win32::Foundation::BOOL;
328326

329327
unsafe {
330328
// Open a handle to the process with termination privileges.
331-
let handle: HANDLE = OpenProcess(PROCESS_TERMINATE, false, pid_u32);
332-
if handle.is_invalid() {
329+
let handle: HANDLE = OpenProcess(PROCESS_TERMINATE, BOOL::from(false), pid_u32);
330+
if handle == INVALID_HANDLE_VALUE {
333331
// Could not obtain a handle, perhaps the process already exited.
334332
let err = std::io::Error::last_os_error();
335333
warn!(pid = pid_u32, error = %err, "Failed to open process handle for termination (possibly already exited). Checking child status.");
@@ -350,7 +348,7 @@ async fn handle_timeout_event(
350348
}
351349
} else {
352350
// Attempt to terminate the process.
353-
if TerminateProcess(handle, 1).as_bool() {
351+
if TerminateProcess(handle, 1).is_positive() {
354352
debug!(pid = pid_u32, "Process terminated successfully via TerminateProcess.");
355353
CloseHandle(handle);
356354
Ok(None)

0 commit comments

Comments
 (0)