Repository navigation
D3D12-LOADER-PROFILE-AND-STACKS-SAFETY: fix handle leaks, CPU underflow, and stderr panics - #2947
bobcatchris wants to merge 2 commits into
Conversation
…derr panics (D3D12-LOADER-PROFILE-AND-STACKS-SAFETY)
|
Independent handle ownership and CPU-total checks under Wine on I added the same test-only probe to both trees. It opened a real Windows handle to the test’s own thread and checked it with A separate probe put 50,000 then 10,000 synthetic 100 ns ticks into the report writer. The optimized main build wrote MEASURED under Wine 11.22 with OpenAI Codex and a separate read-only review. |
KelboLive
left a comment
There was a problem hiding this comment.
The fix itself is sound: Thread/ThreadHandle Drop now cover the failure paths (profile.rs:52-61, stacks.rs:100-110), every CPU delta is saturating with a monotonic now (profile.rs:148-153, :191), and stderr writes can no longer panic. Three of the four mutations are caught (now - first, a no-op Thread::drop, and eprintln! in stacks) — good.
One blocker: the regression tests live behind feature = "diagnostics" (lib.rs:40-43), and CI runs cargo test --workspace --exclude xtask --locked with no features (.github/workflows/d3d12.yml:76). So the default --lib run is 84 tests, none of the new ones compile, and the PR body's "83 passed" run proves nothing about this change. Either add a diagnostics test step or the guards can silently regress.
Second: the stacks handle-close guard is untested — making ThreadHandle::drop a no-op leaves all three stacks::tests green, while the equivalent profile mutation is caught. One more assertion would close it.
Minor: if the first cpu() fails, first = 0 makes the reported milliseconds a lifetime total rather than since profiling began — worth a comment or a None state. And #2588 is unrelated: it is the 64 MB GPU worker stack, not this diagnostic stack dumper, so there is no duplication between them.
Merge is clean against current main, and no other open PR touches profile.rs/stacks.rs.
…ard missing first cpu(), and test ThreadHandle drop
|
Updated in fast-forward commit
Re-verified locally (Windows 11, Rust 1.98.1):
AI-assisted, including a separate read-only independent review pass (APPROVE). |
Summary
Claimed in the coordination issue as:
D3D12-LOADER-PROFILE-AND-STACKS-SAFETY(#2570 (comment)).Fixes thread-handle leaks, unsigned CPU-time underflow, and closed-stderr panics in
leonida-loader's diagnostic sampler and stack dumper:d3d12/crates/leonida-loader/src/profile.rs:impl Drop for Threadto close every non-nullOpenThreadhandle once on drop (and removes the manualCloseHandleinsidewrite()), so handles are never leaked ifFile::create/writeln!fails orprofile()unwinds.cpu(HANDLE)returnOption<u64>checkingGetThreadTimes(...) != 0withsaturating_add, keepst.nowmonotonic (t.now = now.max(t.now)), and usessaturating_subfort.now - t.lastandt.now - t.firstso a failed query cannot panic with subtract underflow in debug builds or wrap tou64::MAXin release builds.eprintln!calls on the detachedLEONIDA_PROFILEthread with falliblelet _ = writeln!(std::io::stderr(), ...).d3d12/crates/leonida-loader/src/stacks.rs:OpenThreadhandle in an RAIIThreadHandleguard, drops it immediately after reading the thread's frames/name/start (before stderr writes), and replaceseprintln!indump_stacks()with falliblelet _ = writeln!(std::io::stderr(), ...).Tests & Verification
profile::tests::cpu_handles_invalid_handle_and_write_saturates_underflow,profile::tests::profile_writes_file_and_closes_handles_when_stderr_pipe_is_closed, andstacks::tests::dump_stacks_does_not_panic_when_stderr_pipe_is_closed.saturating_subandwriteln!tot.now - t.firstandeprintln!: 3/3 FAIL (RED_EXIT=101,attempt to subtract with overflow+ closed-stderr panics).cargo test --locked -p leonida-loader --lib --features diagnostics -- profile::tests stacks::tests: 6 passed, 0 failed.cargo test --locked -p leonida-loader --lib: 83 passed, 0 failed (excluding unrelated pre-existingmemory_atrace fixed in D3D12-LOADER-CRASH-MEMORY-AT-ISOLATION: remove post-MEM_RELEASE VA-reuse race in memory_at test #2939).python -I tools/d3d12_switches.py --check d3d12/docs/switches.md: exit 0.python -I scripts/check_no_game_data.py d3d12/crates/leonida-loader/src/profile.rs d3d12/crates/leonida-loader/src/stacks.rs: exit 0.AI Assistance
AI-assisted using Gemini 4 Neon, including a separate read-only independent review pass (APPROVE).