From 88f29d183684c61f09b432e2c2650e2285b7cc8f Mon Sep 17 00:00:00 2001 From: John Howard <49204095+Royalflamejlh@users.noreply.github.com> Date: Wed, 25 Nov 2026 19:57:28 -0700 Subject: [PATCH] xcl run: the end of a run ends the process past XCL's own exit hook MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit `end_title` suspends the title's threads, drains the GPU and ends the process, with a watchdog that ends it within 15 s if the drain hangs: "a suspended thread may hold a lock the drain needs". The watchdog ended the process through `TerminateProcess`, so through XCL's `NtTerminateProcess` hook, which formats the stack and a log line first. When a suspended thread holds the process heap, the drain and then the watchdog both wait for it, and the process does not end. On Linux (Wine) the runs of today's trees log the drain's wait and, in about half of them, the watchdog's ("main process heap section wait timed out in thread …"), and then never exit. The watchdog now calls the real `NtTerminateProcess`. Co-Authored-By: Claude Opus 5.5 --- crates/xcl/src/eh.rs | 52 +++++++++++++++++++++++++++++++ crates/xcl/src/hle/d3d12x/real.rs | 2 +- 2 files changed, 53 insertions(+), 1 deletion(-) diff --git a/crates/xcl/src/eh.rs b/crates/xcl/src/eh.rs index 2d431e5a47..a5cc29a15a 100644 --- a/crates/xcl/src/eh.rs +++ b/crates/xcl/src/eh.rs @@ -141,6 +141,16 @@ unsafe extern "system" fn hk_nt_terminate_process(h: isize, status: i32) -> i32 orig(h, status) } +/// Ends the process past the hook above, which allocates: a suspended thread may hold the heap. +pub(crate) unsafe fn end_process_now(status: i32) { + let orig = ORIG_NT_TERMINATE.load(Ordering::Acquire); + if orig != 0 { + let f: unsafe extern "system" fn(isize, i32) -> i32 = std::mem::transmute(orig); + f(-1, status); + } + windows_sys::Win32::System::Threading::TerminateProcess(windows_sys::Win32::System::Threading::GetCurrentProcess(), status as u32); +} + pub fn install_crash_handler() { unsafe { if let Err(e) = hooks::hook("ntdll.dll", "NtTerminateProcess", hk_nt_terminate_process as usize, &ORIG_NT_TERMINATE) { @@ -974,6 +984,48 @@ pub fn start_probes() { mod tests { use super::*; + #[test] + fn the_process_ends_while_another_thread_holds_the_heap() { + static HELD: std::sync::atomic::AtomicU8 = std::sync::atomic::AtomicU8::new(0); + const CHILD: &str = "XCL_TEST_HEAP_HELD"; + if std::env::var_os(CHILD).is_some() { + unsafe { + install_crash_handler(); + assert_ne!(ORIG_NT_TERMINATE.load(Ordering::Acquire), 0, "NtTerminateProcess is not hooked"); + std::thread::spawn(|| { + let locked = windows_sys::Win32::System::Memory::HeapLock(windows_sys::Win32::System::Memory::GetProcessHeap()) != 0; + HELD.store(if locked { 1 } else { 2 }, Ordering::Release); + loop { + std::thread::sleep(std::time::Duration::from_secs(1)); + } + }); + let held = loop { + match HELD.load(Ordering::Acquire) { + 0 => std::hint::spin_loop(), + h => break h, + } + }; + assert_eq!(held, 1, "HeapLock failed"); + end_process_now(7); + } + } + let mut child = std::process::Command::new(std::env::current_exe().unwrap()) + .args(["--exact", "eh::tests::the_process_ends_while_another_thread_holds_the_heap", "--nocapture"]) + .env(CHILD, "1") + .spawn() + .unwrap(); + let start = std::time::Instant::now(); + while start.elapsed() < std::time::Duration::from_secs(20) { + if let Some(status) = child.try_wait().unwrap() { + assert_eq!(status.code(), Some(7)); + return; + } + std::thread::sleep(std::time::Duration::from_millis(50)); + } + let _ = child.kill(); + panic!("the process did not end while another thread held the heap"); + } + #[test] fn bounded_cstr_stops_at_nul_limit_and_unreadable_memory() { unsafe { diff --git a/crates/xcl/src/hle/d3d12x/real.rs b/crates/xcl/src/hle/d3d12x/real.rs index 76f31d4daf..cfdce1e735 100644 --- a/crates/xcl/src/hle/d3d12x/real.rs +++ b/crates/xcl/src/hle/d3d12x/real.rs @@ -3592,7 +3592,7 @@ pub fn end_title(code: u32) -> ! { let _ = std::thread::Builder::new().name("xcl-end-watchdog".into()).spawn(move || { let _ = tx.send(windows_sys::Win32::System::Threading::GetCurrentThreadId()); std::thread::sleep(std::time::Duration::from_secs(15)); - windows_sys::Win32::System::Threading::TerminateProcess(windows_sys::Win32::System::Threading::GetCurrentProcess(), code); + crate::eh::end_process_now(code as i32); }); let watchdog = rx.recv_timeout(std::time::Duration::from_secs(1)).unwrap_or(0); crate::log::flush();