From 679938d2411239fb3efaa358aac59e3f35b3a896 Mon Sep 17 00:00:00 2001 From: bobcatchris <83428450+bobcatchris@users.noreply.github.com> Date: Tue, 1 Dec 2026 17:07:21 -0600 Subject: [PATCH] d3d12/gpu: wrap shown saver CreateEventW handle in RAII Drop guard --- d3d12/crates/leonida-gpu/src/shown.rs | 58 ++++++++++++++++++++++++++- 1 file changed, 56 insertions(+), 2 deletions(-) diff --git a/d3d12/crates/leonida-gpu/src/shown.rs b/d3d12/crates/leonida-gpu/src/shown.rs index eb0fd0c66a..ecf2f8e05d 100644 --- a/d3d12/crates/leonida-gpu/src/shown.rs +++ b/d3d12/crates/leonida-gpu/src/shown.rs @@ -21,6 +21,7 @@ use std::sync::atomic::{AtomicU64, Ordering::Relaxed}; use std::sync::{Mutex, OnceLock}; +use windows::Win32::Foundation::{CloseHandle, HANDLE}; use windows::Win32::Graphics::Direct3D12::*; use windows::Win32::Graphics::Dxgi::Common::*; use windows::Win32::Graphics::Dxgi::*; @@ -393,17 +394,30 @@ fn copy_shown(this: *mut core::ffi::c_void, flags: u32) { } } +struct EventHandle(HANDLE); + +impl Drop for EventHandle { + fn drop(&mut self) { + if !self.0.is_invalid() { + // SAFETY: event handle created by `CreateEventW`; closed once on drop. + let _ = unsafe { CloseHandle(self.0) }; + self.0 = HANDLE::default(); + } + } +} + /// The thread that waits for each copy, takes its bytes out and frees its slot; the file is /// written by a thread of its own, as the capture at our present does. fn saver(jobs: std::sync::mpsc::Receiver, _device: ID3D12Device) { // SAFETY: a plain auto-reset event of ours. let Ok(event) = (unsafe { CreateEventW(None, false, false, None) }) else { return }; + let event = EventHandle(event); while let Ok(job) = jobs.recv() { let Some(hook) = HOOK.get() else { return }; // SAFETY: our fence and event. unsafe { - if hook.fence.GetCompletedValue() < job.value && hook.fence.SetEventOnCompletion(job.value, event).is_ok() { - WaitForSingleObject(event, INFINITE); + if hook.fence.GetCompletedValue() < job.value && hook.fence.SetEventOnCompletion(job.value, event.0).is_ok() { + WaitForSingleObject(event.0, INFINITE); } } let taken = { @@ -465,3 +479,43 @@ fn note_time(n: u64, ours: u64, us: u128) { let _ = writeln!(file, "{n} {ours} {us}"); } } + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn event_handle_drop_closes_event_handle() { + const CHILD_ENV: &str = "SHOWN_EVENT_DROP_CHILD"; + if std::env::var_os(CHILD_ENV).is_some() { + // SAFETY: creates a plain unnamed auto-reset event owned by this guard. + let raw = unsafe { CreateEventW(None, false, false, None) }.unwrap(); + let guard = EventHandle(raw); + let mut flags = 0u32; + // SAFETY: valid handle open in this isolated child process. + let open_before = unsafe { windows_sys::Win32::Foundation::GetHandleInformation(raw.0, &mut flags) }; + assert_ne!(open_before, 0, "event handle should be open before drop"); + drop(guard); + // SAFETY: isolated `--exact` subprocess; verify Drop closed `raw`. + let open_after = unsafe { windows_sys::Win32::Foundation::GetHandleInformation(raw.0, &mut flags) }; + assert_eq!(open_after, 0, "EventHandle::drop should close the event handle"); + return; + } + let exe = std::env::current_exe().unwrap(); + let out = std::process::Command::new(exe) + .env(CHILD_ENV, "1") + .args([ + "--exact", + "shown::tests::event_handle_drop_closes_event_handle", + "--nocapture", + ]) + .output() + .unwrap(); + let stdout = String::from_utf8_lossy(&out.stdout); + assert!( + out.status.success() && stdout.contains("1 passed"), + "shown event handle drop check failed (status={:?}, stdout={stdout})", + out.status + ); + } +}