D3D12.X: forget a command list before the runtime frees it #2681

Closed
SergeAx wants to merge 1 commit from upstream-pr/2691 into main
SergeAx commented 2026-11-26 06:27:24 +00:00 (Migrated from github.com)

Imported from ivall/xcl. Originally posted by SergeAx on 2026-11-26T06:27:24Z.


Fixes #2690.

real::cl_release dropped what XCL keeps by a command list's address only after the runtime's final Release had freed the list, so a list created on another thread in between could get the address and lose its bind state, allocator, last submit and DRED marks. As res_release and #2422 do, the last reference (AddRef returns 2) now forgets the list first. When two last-but-one releases race, both miss it and the list is forgotten after its release, as before.

What is in it

  • crates/xcl/src/hle/d3d12x/real.rs: cl_release forgets the list before the runtime's final Release (forget_command_list).
  • crates/xcl/src/hle/d3d12x/real.rs (list_release_tests): a_new_list_at_a_released_lists_address_keeps_its_state, a fake list whose Release hands its address to a new list at once.

Tested

  • NVIDIA GeForce RTX 5080, driver 621.14; AMD Ryzen 5 3500X; Windows 10 Pro 22H2 (19045.6466), D3D12 Agility SDK 1.620.2; Rust 1.100.0, debug
  • cargo test --workspace --no-fail-fast -- --test-threads=4 at 008fc961 (on main b49be183), with a retail title running on the same GPU: 1430 passed, 6 failed, 47 ignored. The 6: #1905 (fuzz_validate_and_gpu_0.._3); vram::tests::d32s8_targets_take_their_own_size_of_video_memory (#2588, which #2593 fixes); list_x::tests::a_reset_while_the_last_submit_runs_records_on_another_pc_list (#42, parallel-only; not explained by this change, see #2690).
  • SKIPPED (25): 21 × no wave64; 3 × wave32 per-lane form refused (V_MUL_F16, V_PK_MUL_F16, V_CVT_F16_F32); heap_window_out_of_memory_falls_back_to_the_switch_form: no heap windows
  • The new test fails with main's cl_release (the new list's bind state is gone) and passes with this change.
  • The lib test binary as --test-threads=4 hle::d3d12x::list_x, builds alternating: a_dispatch_with_a_pipeline_still_building_waits_at_the_submit failed in 13 of 200 runs on main and 6 of 200 with this change. Most of it is title_list::release's longer window, which #1453 closes (0 of 400 with #1453, 0 of 400 with both).

Regression run

main:

xcl-smoke v1
commit: b49be1836844fe0b0cdb3211dce3cb7fb4e21ac7
gpu: NVIDIA GeForce RTX 5080
title: 3c290c16
seconds: 60
exit: 0x0
frames shown: 1400 (PresentX 1401)
presents per 2 s (last 3): 60 52 47
device removals: 0
crashes: 0
peak commit: 18229 MB

this PR:

xcl-smoke v1
commit: 008fc9618549088436a454a2247bfd4d62aeb5cc
gpu: NVIDIA GeForce RTX 5080
title: 3c290c16
seconds: 60
exit: 0x0
frames shown: 1445 (PresentX 1445)
presents per 2 s (last 3): 60 60 60
device removals: 0
crashes: 0
peak commit: 15653 MB

Not tested

  • AMD and Intel GPUs, Windows 11
  • A title run that shows the race: it was found with the test above, not in a title
  • Merging with #2138, which reorders the same three calls in cl_release: the two conflict there textually (forget_command_list would take #2138's order)

AI use

  • Claude Code (Claude Opus 5.5) found the cause, wrote the change and the test, ran the tests and measurements, and wrote this description.
<!-- greyfinch-sync:ivall/xcl:issue:5505163411 --> Imported from [ivall/xcl](https://github.com/xcl-dev/xcl/pull/2691). Originally posted by [SergeAx](https://github.com/SergeAx) on 2026-11-26T06:27:24Z. --- Fixes #2690. `real::cl_release` dropped what XCL keeps by a command list's address only after the runtime's final `Release` had freed the list, so a list created on another thread in between could get the address and lose its bind state, allocator, last submit and DRED marks. As `res_release` and #2422 do, the last reference (`AddRef` returns 2) now forgets the list first. When two last-but-one releases race, both miss it and the list is forgotten after its release, as before. **What is in it** - `crates/xcl/src/hle/d3d12x/real.rs`: `cl_release` forgets the list before the runtime's final `Release` (`forget_command_list`). - `crates/xcl/src/hle/d3d12x/real.rs` (`list_release_tests`): `a_new_list_at_a_released_lists_address_keeps_its_state`, a fake list whose `Release` hands its address to a new list at once. **Tested** - NVIDIA GeForce RTX 5080, driver 621.14; AMD Ryzen 5 3500X; Windows 10 Pro 22H2 (19045.6466), D3D12 Agility SDK 1.620.2; Rust 1.100.0, debug - `cargo test --workspace --no-fail-fast -- --test-threads=4` at `008fc961` (on `main` `b49be183`), with a retail title running on the same GPU: 1430 passed, 6 failed, 47 ignored. The 6: #1905 (`fuzz_validate_and_gpu_0`..`_3`); `vram::tests::d32s8_targets_take_their_own_size_of_video_memory` (#2588, which #2593 fixes); `list_x::tests::a_reset_while_the_last_submit_runs_records_on_another_pc_list` (#42, parallel-only; not explained by this change, see #2690). - SKIPPED (25): 21 × no wave64; 3 × wave32 per-lane form refused (`V_MUL_F16`, `V_PK_MUL_F16`, `V_CVT_F16_F32`); `heap_window_out_of_memory_falls_back_to_the_switch_form`: no heap windows - The new test fails with `main`'s `cl_release` (the new list's bind state is gone) and passes with this change. - The lib test binary as `--test-threads=4 hle::d3d12x::list_x`, builds alternating: `a_dispatch_with_a_pipeline_still_building_waits_at_the_submit` failed in 13 of 200 runs on `main` and 6 of 200 with this change. Most of it is `title_list::release`'s longer window, which #1453 closes (0 of 400 with #1453, 0 of 400 with both). **Regression run** main: ``` xcl-smoke v1 commit: b49be1836844fe0b0cdb3211dce3cb7fb4e21ac7 gpu: NVIDIA GeForce RTX 5080 title: 3c290c16 seconds: 60 exit: 0x0 frames shown: 1400 (PresentX 1401) presents per 2 s (last 3): 60 52 47 device removals: 0 crashes: 0 peak commit: 18229 MB ``` this PR: ``` xcl-smoke v1 commit: 008fc9618549088436a454a2247bfd4d62aeb5cc gpu: NVIDIA GeForce RTX 5080 title: 3c290c16 seconds: 60 exit: 0x0 frames shown: 1445 (PresentX 1445) presents per 2 s (last 3): 60 60 60 device removals: 0 crashes: 0 peak commit: 15653 MB ``` **Not tested** - AMD and Intel GPUs, Windows 11 - A title run that shows the race: it was found with the test above, not in a title - Merging with #2138, which reorders the same three calls in `cl_release`: the two conflict there textually (`forget_command_list` would take #2138's order) **AI use** - Claude Code (Claude Opus 5.5) found the cause, wrote the change and the test, ran the tests and measurements, and wrote this description.
SlopTart commented 2026-11-26 07:03:44 +00:00 (Migrated from github.com)

Imported from ivall/xcl. Originally posted by SlopTart on 2026-11-26T06:50:54Z.


Tested on the Linux/Wine lane, full standard env (AMD rig: Ryzen 9 9900X, 96 GB DDR5, RX 7900 XT 20 GB, Arch kernel 7.2.15, Mesa 26.2.5 RADV, wine-11.0 Proton, rustc 1.94.0; d3d12,d3d12core,dxgi,d3d11,d3d10core=n,b overrides, official DXC kit, VKD3D_CONFIG=ignore_shared_fence, threads=2), head 008fc961, mergeable against main b49be183:
non-gpu suite (--skip gpu::): 597 passed, 3 failed, 4 ignored, 733 filtered.
The failure set is identical to bare main b49be183 measured on the same rig the same day (596 passed, 3 failed): gpu_promoted_uav_clear (our #2490), xma2_source_voice (#2481, vendored-FAudio gate). The third red, the sparse tile video-memory test, is a known batch flake (green solo).
Zero delta from this PR; every test it adds passes inside the suite.
Not tested: gpu suite, Windows native.
Correction: an earlier version of this row reported three extra reds; those runs lacked the d3d12,d3d12core=n,b DLL overrides and hit stub-DLL NOTIMPL artifacts. Re-ran with the standard env, artifacts gone.
AI use: Hermes agent (local Qwen-class) ran the tests and drafted this; I checked it before posting.

<!-- greyfinch-sync:ivall/xcl:comment:7184842423 --> Imported from [ivall/xcl](https://github.com/xcl-dev/xcl/pull/2691#issuecomment-7184842423). Originally posted by [SlopTart](https://github.com/SlopTart) on 2026-11-26T06:50:54Z. --- **Tested** on the Linux/Wine lane, full standard env (AMD rig: Ryzen 9 9900X, 96 GB DDR5, RX 7900 XT 20 GB, Arch kernel 7.2.15, Mesa 26.2.5 RADV, wine-11.0 Proton, rustc 1.94.0; `d3d12,d3d12core,dxgi,d3d11,d3d10core=n,b` overrides, official DXC kit, `VKD3D_CONFIG=ignore_shared_fence`, threads=2), head `008fc961`, mergeable against main `b49be183`: non-gpu suite (`--skip gpu::`): **597 passed, 3 failed, 4 ignored, 733 filtered**. The failure set is identical to bare main `b49be183` measured on the same rig the same day (596 passed, 3 failed): gpu_promoted_uav_clear (our #2490), xma2_source_voice (#2481, vendored-FAudio gate). The third red, the sparse tile video-memory test, is a known batch flake (green solo). Zero delta from this PR; every test it adds passes inside the suite. **Not tested**: gpu suite, Windows native. **Correction**: an earlier version of this row reported three extra reds; those runs lacked the `d3d12,d3d12core=n,b` DLL overrides and hit stub-DLL NOTIMPL artifacts. Re-ran with the standard env, artifacts gone. **AI use**: Hermes agent (local Qwen-class) ran the tests and drafted this; I checked it before posting.
luckyteagirl commented 2026-11-26 08:04:36 +00:00 (Migrated from github.com)

Imported from ivall/xcl. Originally posted by luckyteagirl on 2026-11-26T07:09:18Z.


RX 6800M verification at exact PR head 008fc9618549088436a454a2247bfd4d62aeb5cc against current main b49be1836844fe0b0cdb3211dce3cb7fb4e21ac7 (Windows 11, AMD driver 30.0.13002.13003, Rust 1.99.0 MSVC):

  • cargo test --locked -p xcl --lib -j 1 hle::d3d12x::real::list_release_tests::a_new_list_at_a_released_lists_address_keeps_its_state -- --exact --test-threads=1 --nocapture: 1 passed, 0 failed, 0 ignored, 1,336 filtered. This is the synthetic pointer-reuse fixture.
  • cargo build --locked --release -p xcl -j 1: passed; head executable SHA-256 D81895D827BCDFEEAB5C971FAADC03D381C521690D74C7C40E36AFC948BD8F81.
  • Paired guarded release smoke with scripts/smoke.ps1 -Seconds 12, XCL_ADAPTER=6800M, and separate data/DXIL cache directories, on the same locally owned title build (hash 2ef5fd64):
Main b49be183 PR 008fc961
Exit 0x0 0x0
Frames shown / PresentX 147 / 147 153 / 153
Last three 2-second present windows 27, 60, 60 33, 60, 60
Device removals / crashes 0 / 0 0 / 0
Peak commit 7,512 MB 7,499 MB

I attempted a longer 60-second clean-main control. A separate low-memory monitor stopped that control after about 22 seconds when available commit fell to 1,015 MB, so there is no comparable 60-second result. These short runs show startup and presentation; they do not trigger the command-list address-reuse race or establish world-load/performance behavior. I did not run the full workspace suite on this head. AI-assisted test report with Codex.

<!-- greyfinch-sync:ivall/xcl:comment:7185153241 --> Imported from [ivall/xcl](https://github.com/xcl-dev/xcl/pull/2691#issuecomment-7185153241). Originally posted by [luckyteagirl](https://github.com/luckyteagirl) on 2026-11-26T07:09:18Z. --- RX 6800M verification at exact PR head `008fc9618549088436a454a2247bfd4d62aeb5cc` against current main `b49be1836844fe0b0cdb3211dce3cb7fb4e21ac7` (Windows 11, AMD driver `30.0.13002.13003`, Rust 1.99.0 MSVC): - `cargo test --locked -p xcl --lib -j 1 hle::d3d12x::real::list_release_tests::a_new_list_at_a_released_lists_address_keeps_its_state -- --exact --test-threads=1 --nocapture`: **1 passed, 0 failed, 0 ignored**, 1,336 filtered. This is the synthetic pointer-reuse fixture. - `cargo build --locked --release -p xcl -j 1`: passed; head executable SHA-256 `D81895D827BCDFEEAB5C971FAADC03D381C521690D74C7C40E36AFC948BD8F81`. - Paired guarded release smoke with `scripts/smoke.ps1 -Seconds 12`, `XCL_ADAPTER=6800M`, and separate data/DXIL cache directories, on the same locally owned title build (hash `2ef5fd64`): | | Main `b49be183` | PR `008fc961` | | --- | ---: | ---: | | Exit | `0x0` | `0x0` | | Frames shown / PresentX | 147 / 147 | 153 / 153 | | Last three 2-second present windows | 27, 60, 60 | 33, 60, 60 | | Device removals / crashes | 0 / 0 | 0 / 0 | | Peak commit | 7,512 MB | 7,499 MB | I attempted a longer 60-second clean-main control. A separate low-memory monitor stopped that control after about 22 seconds when available commit fell to 1,015 MB, so there is no comparable 60-second result. These short runs show startup and presentation; they do not trigger the command-list address-reuse race or establish world-load/performance behavior. I did not run the full workspace suite on this head. AI-assisted test report with Codex.
sage-sage commented 2026-11-26 08:04:41 +00:00 (Migrated from github.com)

Imported from ivall/xcl. Originally posted by sage-sage on 2026-11-26T07:40:23Z.


Test report: passes on NVIDIA, and with #1453 the dispatch flake from #2690 did not show in 300 parallel runs. Exact head 008fc961 (its base is current main b49be18, so head = merged).

Tested

  • RTX 4080 SUPER (driver 621.40), Ryzen 5 7600X, Windows 11 26H2 build 26300.10075, rustc 1.100.0 MSVC, DXC 1.9.2602.17 (Windows SDK 10.0.26100), debug build
  • cargo test --workspace -- --test-threads=4 on this head: 1436 passed, 0 failed, 47 ignored; on main + #2691 + #1453: 1438 passed, 0 failed, 47 ignored
  • SKIPPED: 29 lines, the same as on main on this machine (wave64, debug layer, Developer Mode, heap windows)
  • main + #2691 + #1453 merged locally (fc8561d), the lib test binary with hle:: --test-threads=12, 300 runs: a_dispatch_with_a_pipeline_still_building_waits_at_the_submit failed 0 times. On main (with only a test-only acphal change, #2700) the same setting failed it 32 times in 300 runs. The other failures in the 300 were acphal::tests::xapu_connect_returns_oom_and_cleans_up_on_failure (5), a test-only flake #2700 fixes.

Not tested

  • This PR without #1453 under the stress run (#2690 measured that one); no game; AMD / Intel.

Claude Code (Claude Opus 5.5) ran the tests and wrote this report.

<!-- greyfinch-sync:ivall/xcl:comment:7185709350 --> Imported from [ivall/xcl](https://github.com/xcl-dev/xcl/pull/2691#issuecomment-7185709350). Originally posted by [sage-sage](https://github.com/sage-sage) on 2026-11-26T07:40:23Z. --- **Test report: passes on NVIDIA, and with #1453 the dispatch flake from #2690 did not show in 300 parallel runs.** Exact head `008fc961` (its base is current `main` `b49be18`, so head = merged). **Tested** - RTX 4080 SUPER (driver 621.40), Ryzen 5 7600X, Windows 11 26H2 build 26300.10075, rustc 1.100.0 MSVC, DXC 1.9.2602.17 (Windows SDK 10.0.26100), debug build - `cargo test --workspace -- --test-threads=4` on this head: 1436 passed, 0 failed, 47 ignored; on `main` + #2691 + #1453: 1438 passed, 0 failed, 47 ignored - SKIPPED: 29 lines, the same as on `main` on this machine (wave64, debug layer, Developer Mode, heap windows) - `main` + #2691 + #1453 merged locally (`fc8561d`), the lib test binary with `hle:: --test-threads=12`, 300 runs: `a_dispatch_with_a_pipeline_still_building_waits_at_the_submit` failed 0 times. On `main` (with only a test-only acphal change, #2700) the same setting failed it 32 times in 300 runs. The other failures in the 300 were `acphal::tests::xapu_connect_returns_oom_and_cleans_up_on_failure` (5), a test-only flake #2700 fixes. **Not tested** - This PR without #1453 under the stress run (#2690 measured that one); no game; AMD / Intel. Claude Code (Claude Opus 5.5) ran the tests and wrote this report.
KDenKC commented 2026-11-26 10:06:19 +00:00 (Migrated from github.com)

Imported from ivall/xcl. Originally posted by KDenKC on 2026-11-26T09:12:09Z.


A data point from our AMD iGPU (RDNA2), list_x with --test-threads=4. The flaky a_dispatch_with_a_pipeline_still_building_waits_at_the_submit (stand-in lost, 0x0 skipped) failed 3 of 5 runs on main, 3 of 11 with this PR alone, 0 of 50 with #1453, and 0 of 50 with both. So on this fixture the remaining failures come from the title_list path #1453 fixes. The two merge cleanly together. Details are in #1453: https://github.com/xcl-dev/xcl/pull/1453

AI use: Claude Code (Claude Opus 5.5) ran the arms and wrote this.

<!-- greyfinch-sync:ivall/xcl:comment:7187584953 --> Imported from [ivall/xcl](https://github.com/xcl-dev/xcl/pull/2691#issuecomment-7187584953). Originally posted by [KDenKC](https://github.com/KDenKC) on 2026-11-26T09:12:09Z. --- A data point from our AMD iGPU (RDNA2), `list_x` with `--test-threads=4`. The flaky `a_dispatch_with_a_pipeline_still_building_waits_at_the_submit` (stand-in lost, `0x0` skipped) failed 3 of 5 runs on `main`, 3 of 11 with this PR alone, 0 of 50 with #1453, and 0 of 50 with both. So on this fixture the remaining failures come from the `title_list` path #1453 fixes. The two merge cleanly together. Details are in #1453: https://github.com/xcl-dev/xcl/pull/1453 AI use: Claude Code (Claude Opus 5.5) ran the arms and wrote this.
KDenKC commented 2026-11-26 16:23:18 +00:00 (Migrated from github.com)

Imported from ivall/xcl. Originally posted by KDenKC on 2026-11-26T16:23:18Z.


@SergeAx a small suggestion for the concurrent-release case that the description leaves open, in case it is useful.

On 008fc961, two releases that race can both miss the last-reference probe and take the late cleanup path. That path removes bind::STATES by address after the native Release, so it can remove the state of a new list that already reuses the address. The patch below:

  • takes a generation for the entry before the final native Release;
  • afterwards removes the entry only if its generation still matches, under the same lock;
  • gives creation/reset and split inheritance fresh generations, so an absent old entry never authorizes deleting a new one.

Scope is the binding state only. LISTS, allocator associations and DRED keep this PR's cleanup.

Test: a GPU-free fake final release that checks a replacement at the same address, an absent old entry, and ordinary final cleanup. On 008fc961 it fails (the replacement's state is erased); with the patch both release tests pass (2/2). About 70 changed lines on 008fc961.

Not tested: a full suite or a title run with the patch.

patch on 008fc961
diff --git a/crates/xcl/src/hle/d3d12x/bind.rs b/crates/xcl/src/hle/d3d12x/bind.rs
index 3f31c48b..ab4b3753 100644
--- a/crates/xcl/src/hle/d3d12x/bind.rs
+++ b/crates/xcl/src/hle/d3d12x/bind.rs
@@ -20,6 +20,7 @@ use super::console_list::{self, Layout, Param};
 
 #[derive(Default)]
 struct State {
+    generation: u64,
     layout: Option<Arc<Layout>>,
     args: Vec<u8>,
     /// the graphics root signature's layout and arguments
@@ -38,6 +39,11 @@ struct State {
 
 static STATES: Mutex<Option<HashMap<usize, State>>> = Mutex::new(None);
 
+fn next_generation() -> u64 {
+    static NEXT: AtomicU64 = AtomicU64::new(1);
+    NEXT.fetch_add(1, Ordering::Relaxed)
+}
+
 /// draws / dispatches of console code: with a translated pipeline, skipped, with a PC one
 static COUNTS: [AtomicU64; 5] = [const { AtomicU64::new(0) }; 5];
 
@@ -66,7 +72,8 @@ fn count(k: usize) {
 }
 
 fn with<R>(list: *mut c_void, f: impl FnOnce(&mut State) -> R) -> R {
-    f(STATES.lock().unwrap().get_or_insert_with(HashMap::new).entry(list as usize).or_default())
+    f(STATES.lock().unwrap().get_or_insert_with(HashMap::new).entry(list as usize)
+        .or_insert_with(|| State { generation: next_generation(), ..Default::default() }))
 }
 
 /// The list was created or reset onto `allocator` with `initial_pso`: the previous
@@ -74,6 +81,7 @@ fn with<R>(list: *mut c_void, f: impl FnOnce(&mut State) -> R) -> R {
 /// and root signature state reset per D3D12 `CreateCommandList` / `Reset` semantics.
 pub(super) fn reset(list: *mut c_void, allocator: usize, initial_pso: usize) {
     let (old, blocks) = with(list, |s| {
+        s.generation = next_generation();
         let old = std::mem::replace(&mut s.allocator, allocator);
         s.arena = Default::default();
         s.pso = initial_pso;
@@ -108,6 +116,7 @@ pub(super) fn inherit_for_split(from: *mut c_void, to: *mut c_void, allocator: u
         (s.layout.clone(), s.args.clone(), s.layout_gfx.clone(), s.args_gfx.clone(), s.rs_gfx, s.pso)
     });
     let (old, blocks) = with(to, |s| {
+        s.generation = next_generation();
         let old = std::mem::replace(&mut s.allocator, allocator);
         s.arena = Default::default();
         s.layout = layout;
@@ -139,7 +148,28 @@ pub(super) fn pass_arena(from: *mut c_void, to: *mut c_void) {
 
 /// The list is gone.
 pub(super) fn forget(list: *mut c_void) {
-    if let Some(s) = STATES.lock().unwrap().as_mut().and_then(|m| m.remove(&(list as usize))) {
+    forget_if(list, |_| true);
+}
+
+pub(super) fn generation(list: *mut c_void) -> Option<u64> {
+    STATES.lock().unwrap().as_ref()?.get(&(list as usize)).map(|s| s.generation)
+}
+
+pub(super) fn forget_generation(list: *mut c_void, generation: Option<u64>) {
+    if let Some(generation) = generation {
+        forget_if(list, |s| s.generation == generation);
+    }
+}
+
+fn forget_if(list: *mut c_void, matches: impl FnOnce(&State) -> bool) {
+    let old = {
+        use std::collections::hash_map::Entry;
+        match STATES.lock().unwrap().as_mut().map(|m| m.entry(list as usize)) {
+            Some(Entry::Occupied(e)) if matches(e.get()) => Some(e.remove()),
+            _ => None,
+        }
+    };
+    if let Some(s) = old {
         console_list::hold_until_reset(s.allocator, s.uma);
     }
 }
diff --git a/crates/xcl/src/hle/d3d12x/real.rs b/crates/xcl/src/hle/d3d12x/real.rs
index ae390356..a3ac3bbe 100644
--- a/crates/xcl/src/hle/d3d12x/real.rs
+++ b/crates/xcl/src/hle/d3d12x/real.rs
@@ -7006,7 +7006,7 @@ unsafe extern "system" fn cl_execute_indirect(
 
 /// The last reference: what XCL keeps by the list's address goes before the runtime frees it,
 /// as a list created on another thread can get the address at once. (Two last-but-one releases
-/// at once miss each other here; the list's state then goes after it, as before.)
+/// at once miss each other here; check the binding generation in that fallback.)
 unsafe extern "system" fn cl_release(this: *mut c_void) -> u32 {
     let add: RefFn = std::mem::transmute(orig_fn(this, 1));
     let f: RefFn = std::mem::transmute(orig_fn(this, 2));
@@ -7015,9 +7015,12 @@ unsafe extern "system" fn cl_release(this: *mut c_void) -> u32 {
     if last {
         forget_command_list(this);
     }
+    let generation = if last { None } else { super::bind::generation(this) };
     let n = f(this);
     if n == 0 && !last {
-        forget_command_list(this);
+        super::list_x::on_release(this as usize);
+        super::bind::forget_generation(this, generation);
+        super::dred::forget_list(this);
     }
     n
 }
@@ -8997,6 +9000,8 @@ mod list_release_tests {
     struct FakeList {
         vtable: usize,
         refs: u32,
+        concurrent_release: bool,
+        recycle: bool,
     }
 
     const ALLOC: usize = 0x7E57_A130;
@@ -9012,7 +9017,11 @@ mod list_release_tests {
     unsafe extern "system" fn release(this: *mut c_void) -> u32 {
         let l = &mut *(this as *mut FakeList);
         l.refs -= 1;
-        if l.refs == 0 {
+        if std::mem::take(&mut l.concurrent_release) {
+            // The probe observed two references. The other owner releases while it unwinds.
+            l.refs -= 1;
+        }
+        if l.refs == 0 && l.recycle {
             super::super::bind::reset(this, ALLOC, PSO);
             super::super::alloc_pool::created_on(this as usize, ALLOC, 0);
         }
@@ -9023,7 +9032,7 @@ mod list_release_tests {
     fn a_new_list_at_a_released_lists_address_keeps_its_state() {
         let pc = [0, add_ref as usize, release as usize];
         let ext = [pc.as_ptr() as usize, 0];
-        let mut list = FakeList { vtable: ext.as_ptr() as usize + 8, refs: 2 };
+        let mut list = FakeList { vtable: ext.as_ptr() as usize + 8, refs: 2, concurrent_release: false, recycle: true };
         let this = &mut list as *mut FakeList as *mut c_void;
         super::super::bind::reset(this, ALLOC + 0x10, PSO + 0x10);
         let (kept, n) = unsafe {
@@ -9039,5 +9048,24 @@ mod list_release_tests {
         assert_eq!(pso, PSO, "the new list's bind state");
         assert_eq!(backing, Some(ALLOC), "the new list's allocator");
     }
+
+    #[test]
+    fn concurrent_final_release_only_forgets_its_bind_generation() {
+        let pc = [0, add_ref as usize, release as usize];
+        let ext = [pc.as_ptr() as usize, 0];
+        for (seed, recycle) in [(true, true), (false, true), (true, false)] {
+            let mut list = FakeList { vtable: ext.as_ptr() as usize + 8, refs: 2, concurrent_release: true, recycle };
+            let this = &mut list as *mut FakeList as *mut c_void;
+            if seed {
+                super::super::bind::reset(this, ALLOC + 0x10, PSO + 0x10);
+            }
+            let n = unsafe { cl_release(this) };
+            let pso = super::super::bind::tracked_pso(this);
+            super::super::bind::forget(this);
+            super::super::alloc_pool::forget_list(this as usize);
+            assert_eq!(n, 0);
+            assert_eq!(pso, if recycle { PSO } else { 0 }, "bind state after concurrent last release (seed={seed}, recycle={recycle})");
+        }
+    }
 }

AI use: Codex (GPT-6.2-Sol) wrote the patch and test and ran them. Claude Code (Claude Opus 5.5) reviewed it and wrote this.

<!-- greyfinch-sync:ivall/xcl:comment:7197578341 --> Imported from [ivall/xcl](https://github.com/xcl-dev/xcl/pull/2691#issuecomment-7197578341). Originally posted by [KDenKC](https://github.com/KDenKC) on 2026-11-26T16:23:18Z. --- @SergeAx a small suggestion for the concurrent-release case that the description leaves open, in case it is useful. On `008fc961`, two releases that race can both miss the last-reference probe and take the late cleanup path. That path removes `bind::STATES` by address after the native `Release`, so it can remove the state of a new list that already reuses the address. The patch below: - takes a generation for the entry before the final native `Release`; - afterwards removes the entry only if its generation still matches, under the same lock; - gives creation/reset and split inheritance fresh generations, so an absent old entry never authorizes deleting a new one. Scope is the binding state only. `LISTS`, allocator associations and DRED keep this PR's cleanup. Test: a GPU-free fake final release that checks a replacement at the same address, an absent old entry, and ordinary final cleanup. On `008fc961` it fails (the replacement's state is erased); with the patch both release tests pass (2/2). About 70 changed lines on `008fc961`. Not tested: a full suite or a title run with the patch. <details><summary>patch on 008fc961</summary> ```diff diff --git a/crates/xcl/src/hle/d3d12x/bind.rs b/crates/xcl/src/hle/d3d12x/bind.rs index 3f31c48b..ab4b3753 100644 --- a/crates/xcl/src/hle/d3d12x/bind.rs +++ b/crates/xcl/src/hle/d3d12x/bind.rs @@ -20,6 +20,7 @@ use super::console_list::{self, Layout, Param}; #[derive(Default)] struct State { + generation: u64, layout: Option<Arc<Layout>>, args: Vec<u8>, /// the graphics root signature's layout and arguments @@ -38,6 +39,11 @@ struct State { static STATES: Mutex<Option<HashMap<usize, State>>> = Mutex::new(None); +fn next_generation() -> u64 { + static NEXT: AtomicU64 = AtomicU64::new(1); + NEXT.fetch_add(1, Ordering::Relaxed) +} + /// draws / dispatches of console code: with a translated pipeline, skipped, with a PC one static COUNTS: [AtomicU64; 5] = [const { AtomicU64::new(0) }; 5]; @@ -66,7 +72,8 @@ fn count(k: usize) { } fn with<R>(list: *mut c_void, f: impl FnOnce(&mut State) -> R) -> R { - f(STATES.lock().unwrap().get_or_insert_with(HashMap::new).entry(list as usize).or_default()) + f(STATES.lock().unwrap().get_or_insert_with(HashMap::new).entry(list as usize) + .or_insert_with(|| State { generation: next_generation(), ..Default::default() })) } /// The list was created or reset onto `allocator` with `initial_pso`: the previous @@ -74,6 +81,7 @@ fn with<R>(list: *mut c_void, f: impl FnOnce(&mut State) -> R) -> R { /// and root signature state reset per D3D12 `CreateCommandList` / `Reset` semantics. pub(super) fn reset(list: *mut c_void, allocator: usize, initial_pso: usize) { let (old, blocks) = with(list, |s| { + s.generation = next_generation(); let old = std::mem::replace(&mut s.allocator, allocator); s.arena = Default::default(); s.pso = initial_pso; @@ -108,6 +116,7 @@ pub(super) fn inherit_for_split(from: *mut c_void, to: *mut c_void, allocator: u (s.layout.clone(), s.args.clone(), s.layout_gfx.clone(), s.args_gfx.clone(), s.rs_gfx, s.pso) }); let (old, blocks) = with(to, |s| { + s.generation = next_generation(); let old = std::mem::replace(&mut s.allocator, allocator); s.arena = Default::default(); s.layout = layout; @@ -139,7 +148,28 @@ pub(super) fn pass_arena(from: *mut c_void, to: *mut c_void) { /// The list is gone. pub(super) fn forget(list: *mut c_void) { - if let Some(s) = STATES.lock().unwrap().as_mut().and_then(|m| m.remove(&(list as usize))) { + forget_if(list, |_| true); +} + +pub(super) fn generation(list: *mut c_void) -> Option<u64> { + STATES.lock().unwrap().as_ref()?.get(&(list as usize)).map(|s| s.generation) +} + +pub(super) fn forget_generation(list: *mut c_void, generation: Option<u64>) { + if let Some(generation) = generation { + forget_if(list, |s| s.generation == generation); + } +} + +fn forget_if(list: *mut c_void, matches: impl FnOnce(&State) -> bool) { + let old = { + use std::collections::hash_map::Entry; + match STATES.lock().unwrap().as_mut().map(|m| m.entry(list as usize)) { + Some(Entry::Occupied(e)) if matches(e.get()) => Some(e.remove()), + _ => None, + } + }; + if let Some(s) = old { console_list::hold_until_reset(s.allocator, s.uma); } } diff --git a/crates/xcl/src/hle/d3d12x/real.rs b/crates/xcl/src/hle/d3d12x/real.rs index ae390356..a3ac3bbe 100644 --- a/crates/xcl/src/hle/d3d12x/real.rs +++ b/crates/xcl/src/hle/d3d12x/real.rs @@ -7006,7 +7006,7 @@ unsafe extern "system" fn cl_execute_indirect( /// The last reference: what XCL keeps by the list's address goes before the runtime frees it, /// as a list created on another thread can get the address at once. (Two last-but-one releases -/// at once miss each other here; the list's state then goes after it, as before.) +/// at once miss each other here; check the binding generation in that fallback.) unsafe extern "system" fn cl_release(this: *mut c_void) -> u32 { let add: RefFn = std::mem::transmute(orig_fn(this, 1)); let f: RefFn = std::mem::transmute(orig_fn(this, 2)); @@ -7015,9 +7015,12 @@ unsafe extern "system" fn cl_release(this: *mut c_void) -> u32 { if last { forget_command_list(this); } + let generation = if last { None } else { super::bind::generation(this) }; let n = f(this); if n == 0 && !last { - forget_command_list(this); + super::list_x::on_release(this as usize); + super::bind::forget_generation(this, generation); + super::dred::forget_list(this); } n } @@ -8997,6 +9000,8 @@ mod list_release_tests { struct FakeList { vtable: usize, refs: u32, + concurrent_release: bool, + recycle: bool, } const ALLOC: usize = 0x7E57_A130; @@ -9012,7 +9017,11 @@ mod list_release_tests { unsafe extern "system" fn release(this: *mut c_void) -> u32 { let l = &mut *(this as *mut FakeList); l.refs -= 1; - if l.refs == 0 { + if std::mem::take(&mut l.concurrent_release) { + // The probe observed two references. The other owner releases while it unwinds. + l.refs -= 1; + } + if l.refs == 0 && l.recycle { super::super::bind::reset(this, ALLOC, PSO); super::super::alloc_pool::created_on(this as usize, ALLOC, 0); } @@ -9023,7 +9032,7 @@ mod list_release_tests { fn a_new_list_at_a_released_lists_address_keeps_its_state() { let pc = [0, add_ref as usize, release as usize]; let ext = [pc.as_ptr() as usize, 0]; - let mut list = FakeList { vtable: ext.as_ptr() as usize + 8, refs: 2 }; + let mut list = FakeList { vtable: ext.as_ptr() as usize + 8, refs: 2, concurrent_release: false, recycle: true }; let this = &mut list as *mut FakeList as *mut c_void; super::super::bind::reset(this, ALLOC + 0x10, PSO + 0x10); let (kept, n) = unsafe { @@ -9039,5 +9048,24 @@ mod list_release_tests { assert_eq!(pso, PSO, "the new list's bind state"); assert_eq!(backing, Some(ALLOC), "the new list's allocator"); } + + #[test] + fn concurrent_final_release_only_forgets_its_bind_generation() { + let pc = [0, add_ref as usize, release as usize]; + let ext = [pc.as_ptr() as usize, 0]; + for (seed, recycle) in [(true, true), (false, true), (true, false)] { + let mut list = FakeList { vtable: ext.as_ptr() as usize + 8, refs: 2, concurrent_release: true, recycle }; + let this = &mut list as *mut FakeList as *mut c_void; + if seed { + super::super::bind::reset(this, ALLOC + 0x10, PSO + 0x10); + } + let n = unsafe { cl_release(this) }; + let pso = super::super::bind::tracked_pso(this); + super::super::bind::forget(this); + super::super::alloc_pool::forget_list(this as usize); + assert_eq!(n, 0); + assert_eq!(pso, if recycle { PSO } else { 0 }, "bind state after concurrent last release (seed={seed}, recycle={recycle})"); + } + } } ``` </details> AI use: Codex (GPT-6.2-Sol) wrote the patch and test and ran them. Claude Code (Claude Opus 5.5) reviewed it and wrote this.
SergeAx commented 2026-11-26 18:48:41 +00:00 (Migrated from github.com)

Imported from ivall/xcl. Originally posted by SergeAx on 2026-11-26T18:48:41Z.


@KDenKC thanks, the generation check is right for what it covers. I read it against 008fc961: every place that starts a list's bind state (reset, inherit_for_split and the first insert in with) takes a fresh generation, so a list made at the address in the window keeps its state.

I'd still keep it out of this PR:

  • It closes the concurrent window for bind::STATES only. The same late path also drops the list's alloc_pool entries (through list_x::on_release) and its DRED marks by address. A fix of that window should guard all three the same way, and that is a second change, of about the same size again.
  • This PR fixes the window its test reproduces, the single last release, and the description says the concurrent case stays as before.
  • The concurrent window needs two owners releasing the same list at once, and a new list made at the freed address before the late cleanup. Nothing here has shown it yet.

If you want to take it on, a follow-up PR stacked on this one, with your test and the guard for all three, is the way I'd go; I'd test it here.

AI use: Claude Code (Claude Opus 5.5) checked the patch against the code and wrote this reply.

<!-- greyfinch-sync:ivall/xcl:comment:7200877655 --> Imported from [ivall/xcl](https://github.com/xcl-dev/xcl/pull/2691#issuecomment-7200877655). Originally posted by [SergeAx](https://github.com/SergeAx) on 2026-11-26T18:48:41Z. --- @KDenKC thanks, the generation check is right for what it covers. I read it against `008fc961`: every place that starts a list's bind state (`reset`, `inherit_for_split` and the first insert in `with`) takes a fresh generation, so a list made at the address in the window keeps its state. I'd still keep it out of this PR: - It closes the concurrent window for `bind::STATES` only. The same late path also drops the list's `alloc_pool` entries (through `list_x::on_release`) and its DRED marks by address. A fix of that window should guard all three the same way, and that is a second change, of about the same size again. - This PR fixes the window its test reproduces, the single last release, and the description says the concurrent case stays as before. - The concurrent window needs two owners releasing the same list at once, and a new list made at the freed address before the late cleanup. Nothing here has shown it yet. If you want to take it on, a follow-up PR stacked on this one, with your test and the guard for all three, is the way I'd go; I'd test it here. AI use: Claude Code (Claude Opus 5.5) checked the patch against the code and wrote this reply.
Decodere commented 2026-11-26 20:44:14 +00:00 (Migrated from github.com)

Imported from ivall/xcl. Originally posted by Decodere on 2026-11-26T20:44:14Z.


Independent desktop NVIDIA verification of PR #2691 at head 008fc9618549088436a454a2247bfd4d62aeb5cc (1 commit; exact parent & merge-base: b49be1836844fe0b0cdb3211dce3cb7fb4e21ac7, pre-#2555 ancestor of main).

Environment

  • GPU: NVIDIA GeForce RTX 4060 (8 GB / 7956 MiB reported by D3D12), driver 32.0.16.2114 (NVIDIA 621.14, WDDM 3.2)
  • CPU / RAM / OS: Intel Core i5-11400F (6C / 12T), 32 GB RAM (31.9 GiB usable), Windows 11 Home 26200 (Developer Mode off)
  • Toolchain: rustc 1.100.0 (0ef1bc386 2026-11-09) MSVC, DXC 1.9.2609.5, --release

Targeted release tests & build (008fc961)

Command Result
cargo test --release -p xcl --lib a_new_list_at_a_released_lists_address_keeps_its_state -- --nocapture 1 passed; 0 failed; 0 ignored; 1336 filtered out (0.00s)
cargo test --release -p xcl --lib hle::d3d12x::real:: -- --test-threads=4 (3 consecutive runs) 61 passed; 0 failed; 0 ignored; 1276 filtered out × 3 (0.72s, 0.70s, 0.66s)
cargo test --release -p xcl --lib hle::d3d12x::list_x:: -- --test-threads=4 (3 consecutive runs) 40 passed; 0 failed; 0 ignored; 1297 filtered out × 3 (0.62s, 0.62s, 0.61s; a_dispatch_with_a_pipeline_still_building_waits_at_the_submit passed 3/3)
cargo build --release -p xcl --bin xcl Clean build (1m 11s, exit 0)

Paired quiet 60 s retail smoke (title: 29640639)

Exact parent & merge-base (b49be1836844fe0b0cdb3211dce3cb7fb4e21ac7, cached baseline on the same machine):

xcl-smoke v1
commit: b49be1836844fe0b0cdb3211dce3cb7fb4e21ac7
gpu: NVIDIA GeForce RTX 4060
title: 29640639
seconds: 60
exit: 0x0
frames shown: 1518 (PresentX 1518)
presents per 2 s (last 3): 60 60 60
device removals: 0
crashes: 0
peak commit: 14018 MB

PR #2691 (008fc9618549088436a454a2247bfd4d62aeb5cc):

xcl-smoke v1
commit: 008fc9618549088436a454a2247bfd4d62aeb5cc
gpu: NVIDIA GeForce RTX 4060
title: 29640639
seconds: 60
exit: 0x0
frames shown: 1599 (PresentX 1599)
presents per 2 s (last 3): 60 60 60
device removals: 0
crashes: 0
peak commit: 14018 MB

Runtime log observations (xcl.log)

  • 9219 log lines, 0 [ERROR] / device-removal / panic lines.
  • Final draw/memory telemetry in log: 19507 draws with translated pipelines (0 skipped, 0 with PC pipelines), 1024 dispatches (0 skipped); last memory sample 14017 MB committed by the process; video memory 6116 MB local, 4683 MB non-local.

Limits of coverage

  • Running 3 parallel passes (--test-threads=4) of hle::d3d12x::real:: and hle::d3d12x::list_x:: (3/3 clean on a_dispatch_with_a_pipeline_still_building_waits_at_the_submit) alongside a_new_list_at_a_released_lists_address_keeps_its_state and a 60 s retail smoke confirms clean behavior and zero regression on RTX 4060, but 3 runs are not a high-iteration statistical flake stress run (the remaining title_list::release race window noted in the PR description is tracked separately in #1453).
  • Frame-count delta vs. the cached exact-parent smoke (1599 vs. 1518 frames) reflects normal single-run startup variance and is not attributed to this PR.

AI disclosure: test execution and report drafting assisted by Google Antigravity (Gemini 4 Neon).

<!-- greyfinch-sync:ivall/xcl:comment:7203020407 --> Imported from [ivall/xcl](https://github.com/xcl-dev/xcl/pull/2691#issuecomment-7203020407). Originally posted by [Decodere](https://github.com/Decodere) on 2026-11-26T20:44:14Z. --- Independent desktop NVIDIA verification of **PR #2691** at head `008fc9618549088436a454a2247bfd4d62aeb5cc` (`1` commit; exact parent & merge-base: `b49be1836844fe0b0cdb3211dce3cb7fb4e21ac7`, pre-`#2555` ancestor of `main`). ### Environment - **GPU:** NVIDIA GeForce RTX 4060 (8 GB / `7956 MiB` reported by D3D12), driver `32.0.16.2114` (NVIDIA `621.14`, WDDM 3.2) - **CPU / RAM / OS:** Intel Core i5-11400F (6C / 12T), 32 GB RAM (`31.9 GiB` usable), Windows 11 Home `26200` (Developer Mode off) - **Toolchain:** `rustc 1.100.0 (0ef1bc386 2026-11-09)` MSVC, DXC `1.9.2609.5`, `--release` ### Targeted release tests & build (`008fc961`) | Command | Result | | --- | --- | | `cargo test --release -p xcl --lib a_new_list_at_a_released_lists_address_keeps_its_state -- --nocapture` | `1 passed; 0 failed; 0 ignored; 1336 filtered out` (`0.00s`) | | `cargo test --release -p xcl --lib hle::d3d12x::real:: -- --test-threads=4` (3 consecutive runs) | `61 passed; 0 failed; 0 ignored; 1276 filtered out` × 3 (`0.72s`, `0.70s`, `0.66s`) | | `cargo test --release -p xcl --lib hle::d3d12x::list_x:: -- --test-threads=4` (3 consecutive runs) | `40 passed; 0 failed; 0 ignored; 1297 filtered out` × 3 (`0.62s`, `0.62s`, `0.61s`; `a_dispatch_with_a_pipeline_still_building_waits_at_the_submit` passed `3/3`) | | `cargo build --release -p xcl --bin xcl` | Clean build (`1m 11s`, exit `0`) | ### Paired quiet 60 s retail smoke (`title: 29640639`) Exact parent & merge-base (`b49be1836844fe0b0cdb3211dce3cb7fb4e21ac7`, cached baseline on the same machine): ``` xcl-smoke v1 commit: b49be1836844fe0b0cdb3211dce3cb7fb4e21ac7 gpu: NVIDIA GeForce RTX 4060 title: 29640639 seconds: 60 exit: 0x0 frames shown: 1518 (PresentX 1518) presents per 2 s (last 3): 60 60 60 device removals: 0 crashes: 0 peak commit: 14018 MB ``` PR #2691 (`008fc9618549088436a454a2247bfd4d62aeb5cc`): ``` xcl-smoke v1 commit: 008fc9618549088436a454a2247bfd4d62aeb5cc gpu: NVIDIA GeForce RTX 4060 title: 29640639 seconds: 60 exit: 0x0 frames shown: 1599 (PresentX 1599) presents per 2 s (last 3): 60 60 60 device removals: 0 crashes: 0 peak commit: 14018 MB ``` ### Runtime log observations (`xcl.log`) - `9219` log lines, `0` `[ERROR]` / device-removal / panic lines. - Final draw/memory telemetry in log: `19507 draws with translated pipelines (0 skipped, 0 with PC pipelines), 1024 dispatches (0 skipped)`; last memory sample `14017 MB committed by the process; video memory 6116 MB local, 4683 MB non-local`. ### Limits of coverage - Running 3 parallel passes (`--test-threads=4`) of `hle::d3d12x::real::` and `hle::d3d12x::list_x::` (3/3 clean on `a_dispatch_with_a_pipeline_still_building_waits_at_the_submit`) alongside `a_new_list_at_a_released_lists_address_keeps_its_state` and a 60 s retail smoke confirms clean behavior and zero regression on RTX 4060, but 3 runs are not a high-iteration statistical flake stress run (the remaining `title_list::release` race window noted in the PR description is tracked separately in `#1453`). - Frame-count delta vs. the cached exact-parent smoke (`1599` vs. `1518` frames) reflects normal single-run startup variance and is not attributed to this PR. *AI disclosure: test execution and report drafting assisted by Google Antigravity (Gemini 4 Neon).*
SergeAx commented 2026-11-28 06:36:54 +00:00 (Migrated from github.com)

Imported from ivall/xcl. Originally posted by SergeAx on 2026-11-28T06:36:54Z.


This change is on main now as 309e3557, which came in with #2959's stack. Its code and list_release_tests match 008fc961 line for line, and only the commit message was reworded. So I'm closing this PR as merged there. #2690 is fixed by that commit. #2852 makes the same change.

Thanks @luckyteagirl, @sage-sage, @KDenKC and @Decodere for the test reports. KDenKC's generation guard for two releases that race is still open, for a stacked follow-up.

<!-- greyfinch-sync:ivall/xcl:comment:7235408930 --> Imported from [ivall/xcl](https://github.com/xcl-dev/xcl/pull/2691#issuecomment-7235408930). Originally posted by [SergeAx](https://github.com/SergeAx) on 2026-11-28T06:36:54Z. --- This change is on `main` now as [`309e3557`](https://github.com/xcl-dev/xcl/commit/309e35572a5a522b33e5fd7ca3d0e8e3e661b90b), which came in with #2959's stack. Its code and `list_release_tests` match `008fc961` line for line, and only the commit message was reworded. So I'm closing this PR as merged there. #2690 is fixed by that commit. #2852 makes the same change. Thanks @luckyteagirl, @sage-sage, @KDenKC and @Decodere for the test reports. KDenKC's generation guard for two releases that race is still open, for a stacked follow-up.

Pull request closed

Sign in to join this conversation.
No reviewers
No labels
No milestone
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Reference
xcl-dev/xcl!2681
No description provided.