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
This commit is contained in:
Clement Tsang 2023-07-05 01:40:12 -04:00 committed by GitHub
parent f21ffde068
commit a10b91239b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 13 additions and 2 deletions

View File

@ -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

View File

@ -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<()> {