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: [PATCH] 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 --- CHANGELOG.md | 1 + src/app/process_killer.rs | 14 ++++++++++++-- 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 916d77cb..80ce8186 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -10,6 +10,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## Bug Fixes - [#1230](https://github.com/ClementTsang/bottom/pull/1230): Fix core dump if the terminal is closed while bottom is open. +- [#1245](https://github.com/ClementTsang/bottom/pull/1245): Fix killing processes in Windows leaving a handle open. ## Changes 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<()> {