From a10b91239bfd46bc4216e5aa27099e8511ca1dfd Mon Sep 17 00:00:00 2001 From: Clement Tsang <34804052+ClementTsang@users.noreply.github.com> Date: Wed, 5 Jul 2023 01:40:12 -0400 Subject: bug: missing windows syscall to close the handle on drop when killing (#1245) * bug: missing windows syscall to close the handle on drop when killing * changelog * fix --- src/app/process_killer.rs | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) (limited to 'src') diff --git a/src/app/process_killer.rs b/src/app/process_killer.rs index 862b17c2..aac85639 100644 --- a/src/app/process_killer.rs +++ b/src/app/process_killer.rs @@ -2,7 +2,7 @@ #[cfg(target_os = "windows")] use windows::Win32::{ - Foundation::HANDLE, + Foundation::{CloseHandle, HANDLE}, System::Threading::{ OpenProcess, TerminateProcess, PROCESS_QUERY_INFORMATION, PROCESS_TERMINATE, }, @@ -27,7 +27,7 @@ impl Process { } fn kill(self) -> Result<(), String> { - // SAFETY: Windows API call, tread carefully with the args. + // SAFETY: Windows API call, this is safe as we are passing in the handle. let result = unsafe { TerminateProcess(self.0, 1) }; if result.0 == 0 { return Err("process may have already been terminated.".to_string()); @@ -37,6 +37,16 @@ impl Process { } } +#[cfg(target_os = "windows")] +impl Drop for Process { + fn drop(&mut self) { + // SAFETY: Windows API call, this is safe as we are passing in the handle. + unsafe { + CloseHandle(self.0); + } + } +} + /// Kills a process, given a PID, for windows. #[cfg(target_os = "windows")] pub fn kill_process_given_pid(pid: Pid) -> crate::utils::error::Result<()> { -- cgit v1.2.3