From f4ec61d652be7532774c88ac31acbb35deeae18e Mon Sep 17 00:00:00 2001 From: Tiago Leal <183942127+tiagonleal@users.noreply.github.com> Date: Sat, 28 Nov 2026 17:21:47 +0000 Subject: [PATCH 1/2] Tests: retry the STATE try_lock in alloc_pool's lock-scope checks completion_queries_run_outside_allocator_state and submit_marking_calls_out_unlocked_and_revalidates_the_queue_marker check that a callback runs outside STATE with a single STATE.try_lock(). STATE is process-wide and other alloc_pool tests running in parallel take it for short sections, so that try_lock fails whenever a sibling test holds it at that instant, even though the caller does not (#3180). The checks now retry try_lock for up to 10 s. If the calling thread holds STATE, try_lock keeps returning WouldBlock and the assertion still fails (checked by taking STATE around the Signal call and the in_flight GetCompletedValue call: both tests fail after 10 s). Before: alloc_pool tests with --test-threads=32, 40 runs: 31 failed, these two tests failed 41 times. After: 0 failures of these two. --- crates/xcl/src/hle/d3d12x/alloc_pool.rs | 20 ++++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) diff --git a/crates/xcl/src/hle/d3d12x/alloc_pool.rs b/crates/xcl/src/hle/d3d12x/alloc_pool.rs index bbb56bdc3f..56a0b519d1 100644 --- a/crates/xcl/src/hle/d3d12x/alloc_pool.rs +++ b/crates/xcl/src/hle/d3d12x/alloc_pool.rs @@ -1609,6 +1609,18 @@ mod tests { assert!(s.backings[&other].users.contains(&elsewhere)); } + /// STATE is process-wide and other tests running in parallel take it briefly, so a single + /// `try_lock` can fail without the caller holding it. Retry until a generous deadline: when + /// this thread holds STATE, `try_lock` keeps returning `WouldBlock` and this returns false. + fn state_not_held_by_caller() -> bool { + let deadline = std::time::Instant::now() + std::time::Duration::from_secs(10); + loop { + if STATE.try_lock().is_ok() { return true; } + if std::time::Instant::now() >= deadline { return false; } + std::thread::yield_now(); + } + } + #[test] fn release_probe_transfers_only_the_logical_final_reference() { use std::cell::Cell; @@ -1734,7 +1746,7 @@ mod tests { refs: Cell, } unsafe extern "system" fn completed(this: *mut c_void) -> u64 { - let outside = STATE.try_lock().is_ok(); + let outside = state_not_held_by_caller(); OUTSIDE.store(outside, Ordering::Relaxed); if outside && MUTATE.swap(false, Ordering::Relaxed) { let mut g = STATE.lock().unwrap(); @@ -1859,11 +1871,11 @@ mod tests { assert!(pending.finish_with_queue_ops( first_queue, || { - assert!(STATE.try_lock().is_ok(), "fence creation must run outside STATE"); + assert!(state_not_held_by_caller(), "fence creation must run outside STATE"); Some(first_fence.clone()) }, |_, value| { - assert!(STATE.try_lock().is_ok(), "queue Signal must run outside STATE"); + assert!(state_not_held_by_caller(), "queue Signal must run outside STATE"); assert_eq!(value, 1); true }, @@ -1921,7 +1933,7 @@ mod tests { reused_queue, || Some(old_fence.clone()), |_, value| { - assert!(STATE.try_lock().is_ok(), "queue invalidation can run while Signal is outside STATE"); + assert!(state_not_held_by_caller(), "queue invalidation can run while Signal is outside STATE"); assert_eq!(value, 1); STATE.lock().unwrap().as_mut().unwrap().fences.insert( reused_queue, From 353546c6bb26a05d777600fa1ce21b516379db0f Mon Sep 17 00:00:00 2001 From: Tiago Leal <183942127+tiagonleal@users.noreply.github.com> Date: Sat, 28 Nov 2026 18:30:58 +0000 Subject: [PATCH 2/2] Tests: wait for a parallel sweep before checking the transferred owner transferred_owner_is_released_once_outside_state_after_logical_forget asserts (refs, releases) == (0, 1) right after its own sweep. STATE is process-wide, and other alloc_pool tests call forget(), which runs sweep_release_owners() over every retired backing. Under --test-threads=32 one of those sweeps can remove this test's backing first; our sweep then finds nothing and returns while the other thread is still inside the (deliberately slow) fake Release, so the check saw (1, 0). Temporary logging showed the late release came from released_lists_leave_only_live_recordings_and_gpu_work_holding_the_backing, submit_marking_calls_out_unlocked_and_revalidates_the_queue_marker and two submit_reservation_* tests, each releasing exactly once. Wait (bounded, 10 s) for the release to land before the unchanged assertions. 40 runs at 32 threads: 1 failure before, 0 after. --- crates/xcl/src/hle/d3d12x/alloc_pool.rs | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/crates/xcl/src/hle/d3d12x/alloc_pool.rs b/crates/xcl/src/hle/d3d12x/alloc_pool.rs index 56a0b519d1..ee1af7edc5 100644 --- a/crates/xcl/src/hle/d3d12x/alloc_pool.rs +++ b/crates/xcl/src/hle/d3d12x/alloc_pool.rs @@ -2154,6 +2154,12 @@ mod tests { backing.submit_pins = 0; } sweep_release_owners_with(drop, drop); + // Tests running in parallel sweep the same STATE (e.g. through `forget`), so another thread + // can take this retired backing first and release it just after our sweep returns. + let deadline = std::time::Instant::now() + std::time::Duration::from_secs(10); + while object.releases.load(Ordering::Acquire) == 0 && std::time::Instant::now() < deadline { + std::thread::yield_now(); + } assert_eq!( (object.refs.load(Ordering::Acquire), object.releases.load(Ordering::Acquire)), (0, 1),