Skip to content

Commit 9ffc6b1

Browse files
committed
refactor(windows): manage process handles with OwnedHandle
Use standard-library ownership for process snapshot and parent handles instead of scopeguard and explicit CloseHandle calls. Remove the unused scopeguard dependency.
1 parent 89b9336 commit 9ffc6b1

3 files changed

Lines changed: 11 additions & 20 deletions

File tree

Cargo.lock

Lines changed: 0 additions & 7 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,6 @@ snapbox = { version = "1", features = ["term-svg"], optional = true }
110110
walkdir = { version = "2", optional = true }
111111

112112
[target."cfg(windows)".dependencies]
113-
scopeguard = "1"
114113
windows-registry = "0.100"
115114
windows-result = "0.100"
116115

src/cli/self_update/windows.rs

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,10 @@ use std::{
66
fs::OpenOptions,
77
io::{self, Write},
88
mem,
9-
os::windows::fs::OpenOptionsExt,
9+
os::windows::{
10+
fs::OpenOptionsExt,
11+
io::{AsRawHandle, FromRawHandle, OwnedHandle},
12+
},
1013
path::{Path, PathBuf},
1114
process::{Command, Stdio},
1215
ptr, thread,
@@ -23,8 +26,8 @@ use windows_registry::{CURRENT_USER, HSTRING, Key};
2326
use windows_result::WIN32_ERROR;
2427
use windows_sys::Win32::{
2528
Foundation::{
26-
CloseHandle, ERROR_FILE_NOT_FOUND, ERROR_INVALID_DATA, INVALID_HANDLE_VALUE, LPARAM,
27-
WAIT_OBJECT_0, WPARAM,
29+
ERROR_FILE_NOT_FOUND, ERROR_INVALID_DATA, INVALID_HANDLE_VALUE, LPARAM, WAIT_OBJECT_0,
30+
WPARAM,
2831
},
2932
Storage::FileSystem::{
3033
FILE_FLAG_DELETE_ON_CLOSE, FILE_SHARE_DELETE, FILE_SHARE_READ, SYNCHRONIZE,
@@ -415,23 +418,21 @@ pub(crate) fn wait_for_parent() -> anyhow::Result<()> {
415418
return Err(err).context(CliError::WindowsUninstallMadness);
416419
}
417420

418-
let snapshot = scopeguard::guard(snapshot, |h| {
419-
let _ = CloseHandle(h);
420-
});
421+
let snapshot = OwnedHandle::from_raw_handle(snapshot);
421422

422423
let mut entry: PROCESSENTRY32 = mem::zeroed();
423424
entry.dwSize = size_of::<PROCESSENTRY32>() as u32;
424425

425426
// Iterate over system processes looking for ours
426-
let success = Process32First(*snapshot, &mut entry);
427+
let success = Process32First(snapshot.as_raw_handle(), &mut entry);
427428
if success == 0 {
428429
let err = io::Error::last_os_error();
429430
return Err(err).context(CliError::WindowsUninstallMadness);
430431
}
431432

432433
let this_pid = GetCurrentProcessId();
433434
while entry.th32ProcessID != this_pid {
434-
let success = Process32Next(*snapshot, &mut entry);
435+
let success = Process32Next(snapshot.as_raw_handle(), &mut entry);
435436
if success == 0 {
436437
let err = io::Error::last_os_error();
437438
return Err(err).context(CliError::WindowsUninstallMadness);
@@ -450,12 +451,10 @@ pub(crate) fn wait_for_parent() -> anyhow::Result<()> {
450451
return Ok(());
451452
}
452453

453-
let parent = scopeguard::guard(parent, |h| {
454-
let _ = CloseHandle(h);
455-
});
454+
let parent = OwnedHandle::from_raw_handle(parent);
456455

457456
// Wait for our parent to exit
458-
let res = WaitForSingleObject(*parent, INFINITE);
457+
let res = WaitForSingleObject(parent.as_raw_handle(), INFINITE);
459458

460459
if res != WAIT_OBJECT_0 {
461460
let err = io::Error::last_os_error();

0 commit comments

Comments
 (0)