Skip to content

Commit a10b912

Browse files
authored
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
1 parent f21ffde commit a10b912

File tree

2 files changed

+13
-2
lines changed

2 files changed

+13
-2
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1010
## Bug Fixes
1111

1212
- [#1230](https://github.com/ClementTsang/bottom/pull/1230): Fix core dump if the terminal is closed while bottom is open.
13+
- [#1245](https://github.com/ClementTsang/bottom/pull/1245): Fix killing processes in Windows leaving a handle open.
1314

1415
## Changes
1516

src/app/process_killer.rs

+12-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
33
#[cfg(target_os = "windows")]
44
use windows::Win32::{
5-
Foundation::HANDLE,
5+
Foundation::{CloseHandle, HANDLE},
66
System::Threading::{
77
OpenProcess, TerminateProcess, PROCESS_QUERY_INFORMATION, PROCESS_TERMINATE,
88
},
@@ -27,7 +27,7 @@ impl Process {
2727
}
2828

2929
fn kill(self) -> Result<(), String> {
30-
// SAFETY: Windows API call, tread carefully with the args.
30+
// SAFETY: Windows API call, this is safe as we are passing in the handle.
3131
let result = unsafe { TerminateProcess(self.0, 1) };
3232
if result.0 == 0 {
3333
return Err("process may have already been terminated.".to_string());
@@ -37,6 +37,16 @@ impl Process {
3737
}
3838
}
3939

40+
#[cfg(target_os = "windows")]
41+
impl Drop for Process {
42+
fn drop(&mut self) {
43+
// SAFETY: Windows API call, this is safe as we are passing in the handle.
44+
unsafe {
45+
CloseHandle(self.0);
46+
}
47+
}
48+
}
49+
4050
/// Kills a process, given a PID, for windows.
4151
#[cfg(target_os = "windows")]
4252
pub fn kill_process_given_pid(pid: Pid) -> crate::utils::error::Result<()> {

0 commit comments

Comments
 (0)