D3D12.X: drop replaced dispatch chain outside CHAINS mutex #3648

Open
Kalmarv wants to merge 1 commit from upstream-pr/3648 into main
Kalmarv commented 2026-12-02 03:29:54 +00:00 (Migrated from github.com)

Imported from ivall/xcl. Originally posted by Kalmarv on 2026-12-02T03:29:54Z.


Drop any replaced Arc<Chain> outside the CHAINS mutex when inserting into dispatch_chain::CHAINS.

dispatch_chain::forget already binds the removed Option<Arc<Chain>> and drops it after unlocking CHAINS, because dropping a Chain releases its COM/D3D12 tail pipeline states (whose Release can re-enter forget_pso -> dispatch_chain::forget). dispatch_chain::build called CHAINS.lock().unwrap().insert(...) directly, which would drop a replaced Arc<Chain> while still holding CHAINS.

What is in it

  • Add replace_entry helper in crates/xcl/src/hle/d3d12x/real/dispatch_chain.rs to bind and drop the previous map value after releasing the mutex, and use it when inserting into CHAINS in dispatch_chain::build.
  • Add unit test replacing_chain_entry_drops_previous_value_outside_lock verifying the map mutex is unlocked when a replaced entry's Drop runs.

Tested

  • hardware (GPU + driver, CPU), Rust version, debug / release: NVIDIA GeForce RTX 5070 Ti (driver 610.74), AMD Ryzen 9 9950X, rustc 1.99.0 (b940084d7 2026-09-28), debug tests + release build (cargo build --release -p xcl: 0 new warnings vs main baseline of 39).
  • scripts/test.ps1 (workspace build, cargo bench -p xcl --bench '*' -- --test, cargo test --workspace -- --test-threads=4 with DXC 1.9.2609): exit 0, 0 failed (replacing_chain_entry_drops_previous_value_outside_lock passed; SKIPPED GPU tests: 22 no wave64, 2 no heap windows).

Regression run

main:

xcl-smoke v1
commit: 1c8b551475e1e7bfe48f94448492a4c3544a2e0f
gpu: NVIDIA GeForce RTX 5070 Ti
title: ea3b5e4d
seconds: 60
exit: 0x0
frames shown: 1387 (PresentX 1387)
presents per 2 s (last 3): 60 60 60
device removals: 0
crashes: 0
peak commit: 17480 MB

PR:

xcl-smoke v1
commit: 0e5223e618ee902090440bba046f114dd15a7a13
gpu: NVIDIA GeForce RTX 5070 Ti
title: ea3b5e4d
seconds: 60
exit: 0x0
frames shown: 1655 (PresentX 1655)
presents per 2 s (last 3): 61 60 63
device removals: 0
crashes: 0
peak commit: 17001 MB

Not tested

  • AMD / Intel GPUs, native wave64 GPU tests, Wine/Linux, and game-data sample tests (XCL_SAMPLE_* unset).
  • Calling dispatch_chain::build end-to-end on a colliding key (the unit test exercises the replace_entry helper directly rather than build).
  • The pre-existing pso_release (real.rs) ordering where Release runs before forget_pso (a concurrent key reuse in that window still drops the newer entry later; this PR only removes the self-deadlock on replacement).

AI use

  • Antigravity (Gemini 4 Neon) implemented the replace_entry helper and unit test, ran the local build/test/smoke verification, and drafted this PR description.
<!-- greyfinch-sync:ivall/xcl:issue:5599254271 --> Imported from [ivall/xcl](https://github.com/xcl-dev/xcl/pull/3648). Originally posted by [Kalmarv](https://github.com/Kalmarv) on 2026-12-02T03:29:54Z. --- Drop any replaced `Arc<Chain>` outside the `CHAINS` mutex when inserting into `dispatch_chain::CHAINS`. `dispatch_chain::forget` already binds the removed `Option<Arc<Chain>>` and drops it after unlocking `CHAINS`, because dropping a `Chain` releases its COM/D3D12 tail pipeline states (whose `Release` can re-enter `forget_pso` -> `dispatch_chain::forget`). `dispatch_chain::build` called `CHAINS.lock().unwrap().insert(...)` directly, which would drop a replaced `Arc<Chain>` while still holding `CHAINS`. **What is in it** - Add `replace_entry` helper in `crates/xcl/src/hle/d3d12x/real/dispatch_chain.rs` to bind and drop the previous map value after releasing the mutex, and use it when inserting into `CHAINS` in `dispatch_chain::build`. - Add unit test `replacing_chain_entry_drops_previous_value_outside_lock` verifying the map mutex is unlocked when a replaced entry's `Drop` runs. **Tested** - hardware (GPU + driver, CPU), Rust version, debug / release: NVIDIA GeForce RTX 5070 Ti (driver 610.74), AMD Ryzen 9 9950X, `rustc 1.99.0 (b940084d7 2026-09-28)`, debug tests + release build (`cargo build --release -p xcl`: 0 new warnings vs `main` baseline of 39). - `scripts/test.ps1` (workspace build, `cargo bench -p xcl --bench '*' -- --test`, `cargo test --workspace -- --test-threads=4` with DXC 1.9.2609): exit 0, 0 failed (`replacing_chain_entry_drops_previous_value_outside_lock` passed; SKIPPED GPU tests: 22 no wave64, 2 no heap windows). **Regression run** main: ``` xcl-smoke v1 commit: 1c8b551475e1e7bfe48f94448492a4c3544a2e0f gpu: NVIDIA GeForce RTX 5070 Ti title: ea3b5e4d seconds: 60 exit: 0x0 frames shown: 1387 (PresentX 1387) presents per 2 s (last 3): 60 60 60 device removals: 0 crashes: 0 peak commit: 17480 MB ``` PR: ``` xcl-smoke v1 commit: 0e5223e618ee902090440bba046f114dd15a7a13 gpu: NVIDIA GeForce RTX 5070 Ti title: ea3b5e4d seconds: 60 exit: 0x0 frames shown: 1655 (PresentX 1655) presents per 2 s (last 3): 61 60 63 device removals: 0 crashes: 0 peak commit: 17001 MB ``` **Not tested** - AMD / Intel GPUs, native wave64 GPU tests, Wine/Linux, and game-data sample tests (`XCL_SAMPLE_*` unset). - Calling `dispatch_chain::build` end-to-end on a colliding key (the unit test exercises the `replace_entry` helper directly rather than `build`). - The pre-existing `pso_release` (`real.rs`) ordering where `Release` runs before `forget_pso` (a concurrent key reuse in that window still drops the newer entry later; this PR only removes the self-deadlock on replacement). **AI use** - Antigravity (Gemini 4 Neon) implemented the `replace_entry` helper and unit test, ran the local build/test/smoke verification, and drafted this PR description.
SavageCore commented 2026-12-02 04:30:46 +00:00 (Migrated from github.com)

Imported from ivall/xcl. Originally posted by SavageCore on 2026-12-02T04:30:46Z.

Review: APPROVED


sorry, ignore, agent went crazy

<!-- greyfinch-sync:ivall/xcl:review:6144785776 --> Imported from [ivall/xcl](https://github.com/xcl-dev/xcl/pull/3648#pullrequestreview-6144785776). Originally posted by [SavageCore](https://github.com/SavageCore) on 2026-12-02T04:30:46Z. **Review:** APPROVED --- sorry, ignore, agent went crazy
vasanex commented 2026-12-02 07:43:59 +00:00 (Migrated from github.com)

Imported from ivall/xcl. Originally posted by vasanex on 2026-12-02T07:43:59Z.


merge-check
base: 1c8b551475
head: 0e5223e618
merge: clean
build: cargo build --workspace -> ok
tests: cargo test --workspace --no-fail-fast -- --test-threads=4 -> 2910 passed, 0 failed, 51 ignored; SKIPPED: 25 (22 no wave64, 2 no heap windows, 1 cannot disable the driver cache), the same tests as on the base (2909 passed there); failing: none. The new test replacing_chain_entry_drops_previous_value_outside_lock ran and passed.
review: whole diff read (one file, +25/-1), no finding. In replace_entry the guard is a temporary of the let statement, so it is released before replaced is dropped. The new test checks the helper with a value whose Drop tries the map's lock. The path is defensive: the key is the address of the pipeline just created, so an entry is replaced only when a released pipeline's entry was not forgotten before its address was reused; I did not see that happen in a run, and the 60 s smoke does not show it either way.
performance: none expected (one map insert per split program, as before). 60 s smoke, release, same title and settings: base 1608 frames, peak commit 19911 MB; merged tree 1699 frames, 18920 MB (95 %), presents 60 60 60, 0 device removals, 0 crashes.
verdict: safe to merge
hardware / tool and model: NVIDIA GeForce RTX 3080 10 GB (driver 621.40), AMD Ryzen 9 7950X, Windows 11 Pro 26200, rustc 1.100.0 / Claude Code with Claude Fable 5.2. Run and posted without a human check of each point.

<!-- greyfinch-sync:ivall/xcl:comment:7338541646 --> Imported from [ivall/xcl](https://github.com/xcl-dev/xcl/pull/3648#issuecomment-7338541646). Originally posted by [vasanex](https://github.com/vasanex) on 2026-12-02T07:43:59Z. --- merge-check base: 1c8b551475e1e7bfe48f94448492a4c3544a2e0f head: 0e5223e618ee902090440bba046f114dd15a7a13 merge: clean build: cargo build --workspace -> ok tests: cargo test --workspace --no-fail-fast -- --test-threads=4 -> 2910 passed, 0 failed, 51 ignored; SKIPPED: 25 (22 no wave64, 2 no heap windows, 1 cannot disable the driver cache), the same tests as on the base (2909 passed there); failing: none. The new test `replacing_chain_entry_drops_previous_value_outside_lock` ran and passed. review: whole diff read (one file, +25/-1), no finding. In `replace_entry` the guard is a temporary of the `let` statement, so it is released before `replaced` is dropped. The new test checks the helper with a value whose `Drop` tries the map's lock. The path is defensive: the key is the address of the pipeline just created, so an entry is replaced only when a released pipeline's entry was not forgotten before its address was reused; I did not see that happen in a run, and the 60 s smoke does not show it either way. performance: none expected (one map insert per split program, as before). 60 s smoke, release, same title and settings: base 1608 frames, peak commit 19911 MB; merged tree 1699 frames, 18920 MB (95 %), presents 60 60 60, 0 device removals, 0 crashes. verdict: safe to merge hardware / tool and model: NVIDIA GeForce RTX 3080 10 GB (driver 621.40), AMD Ryzen 9 7950X, Windows 11 Pro 26200, rustc 1.100.0 / Claude Code with Claude Fable 5.2. Run and posted without a human check of each point.
Foniass commented 2026-12-02 09:08:25 +00:00 (Migrated from github.com)

Imported from ivall/xcl. Originally posted by Foniass on 2026-12-02T09:08:25Z.


Review (head 0e5223e6, diff read, not a merge-check):

  • The test covers the helper but not the call site. replacing_chain_entry_drops_previous_value_outside_lock (dispatch_chain.rs ~697) fails if replace_entry goes back to a one-statement map.lock().unwrap().insert(..);, because the returned value is dropped before the guard and the try_lock assert fires. It still passes if build (~316) stops calling replace_entry and goes back to inserting under CHAINS directly. Without a GPU there is probably no better test, but the description should not read as if build were covered.
  • A replacement still loses state; only the deadlock is gone. The description does not say when build finds an entry already there. By reading, this happens in two cases. (1) The prepared pipeline library on main (compute_container, real.rs ~6374, load_compute) gives the same COM object for the same piece name, so the same split program created twice gets the same key. (2) pso_release (real.rs ~2615-2620) calls the real Release before forget_pso, so another thread's new first piece can reuse the address in between. In case (2), after this PR the late forget_pso on the first thread removes the new chain (and the other per-address state) instead of deadlocking. That bug was there before this PR and is not blocking, but it is worth saying under Not tested / in the description.

Fine: the deadlock is real. Kind::PipelineState hooks Release on the driver vtable (real.rs ~4068), so when a replaced chain's last reference releases a tail or whole-form PSO, pso_release -> forget_pso -> dispatch_chain::forget takes CHAINS again on the same thread. The let binding drops the guard before drop(replaced), the same as forget (~320). In-flight lists keep the replaced chain alive through Recording.chain: Arc<Chain> (~393, dropped outside RECORDINGS at ~490), so the early drop frees nothing the GPU still uses. No textual or functional overlap with #3528 (different hunks of dispatch_chain.rs). This is the separate fix #3633 announces, and its account of the deadlock matches the code. #3633's hand-out-once is still needed after this PR: without it, two creations share one object and one CHAINS key, and one chain replaces the other without any error. Checks green; smoke blocks present.

AI review: Claude Code (Claude Opus 5.5), posted by Foniass without a human check of each point.

<!-- greyfinch-sync:ivall/xcl:comment:7340435556 --> Imported from [ivall/xcl](https://github.com/xcl-dev/xcl/pull/3648#issuecomment-7340435556). Originally posted by [Foniass](https://github.com/Foniass) on 2026-12-02T09:08:25Z. --- Review (head 0e5223e6, diff read, not a merge-check): - **The test covers the helper but not the call site.** `replacing_chain_entry_drops_previous_value_outside_lock` (`dispatch_chain.rs` ~697) fails if `replace_entry` goes back to a one-statement `map.lock().unwrap().insert(..);`, because the returned value is dropped before the guard and the `try_lock` assert fires. It still passes if `build` (~316) stops calling `replace_entry` and goes back to inserting under `CHAINS` directly. Without a GPU there is probably no better test, but the description should not read as if `build` were covered. - **A replacement still loses state; only the deadlock is gone.** The description does not say when `build` finds an entry already there. By reading, this happens in two cases. (1) The prepared pipeline library on main (`compute_container`, `real.rs` ~6374, `load_compute`) gives the same COM object for the same piece name, so the same split program created twice gets the same key. (2) `pso_release` (`real.rs` ~2615-2620) calls the real `Release` before `forget_pso`, so another thread's new first piece can reuse the address in between. In case (2), after this PR the late `forget_pso` on the first thread removes the new chain (and the other per-address state) instead of deadlocking. That bug was there before this PR and is not blocking, but it is worth saying under **Not tested** / in the description. Fine: the deadlock is real. `Kind::PipelineState` hooks `Release` on the driver vtable (`real.rs` ~4068), so when a replaced chain's last reference releases a tail or whole-form PSO, `pso_release` -> `forget_pso` -> `dispatch_chain::forget` takes `CHAINS` again on the same thread. The `let` binding drops the guard before `drop(replaced)`, the same as `forget` (~320). In-flight lists keep the replaced chain alive through `Recording.chain: Arc<Chain>` (~393, dropped outside `RECORDINGS` at ~490), so the early drop frees nothing the GPU still uses. No textual or functional overlap with #3528 (different hunks of `dispatch_chain.rs`). This is the separate fix #3633 announces, and its account of the deadlock matches the code. #3633's hand-out-once is still needed after this PR: without it, two creations share one object and one `CHAINS` key, and one chain replaces the other without any error. Checks green; smoke blocks present. AI review: Claude Code (Claude Opus 5.5), posted by Foniass without a human check of each point.
Foniass commented 2026-12-02 09:44:18 +00:00 (Migrated from github.com)

Imported from ivall/xcl. Originally posted by Foniass on 2026-12-02T09:44:18Z.


merge-check
base: 1c8b551475
head: 0e5223e618
merge: clean
build: cargo build --workspace -> ok
tests: cargo test --workspace --no-fail-fast -- --test-threads=4 -> 2910 passed, 0 failed, 51 ignored; SKIPPED: 28 (20 no wave64, 2 no d3d12SDKLayers.dll, 1 no D3D12 debug layer, 1 no heap windows, 1 cannot disable the driver cache, 3 others; the same set as on main); failing: none
review: read by one reviewer at this same head; the review is at https://github.com/xcl-dev/xcl/pull/3648#issuecomment-7340435556 and its findings stand. Most serious: the fix is correct; the test covers the helper, not the call site in build.
performance: 60 s smoke of a retail title, main 1c8b5514 vs this merge (0e5223e6): 1652 -> 1667 frames shown (100 %; last presents per 2 s: 61 60 60), peak commit 18228 -> 17892 MB, 0 device removals, 0 crashes.
verdict: safe to merge: no blocking finding in the review (the points there are minor), the suite passes and the smoke run is clean
hardware / tool and model: RTX 5070 Ti (driver 621.14), Ryzen 7 7800X3D, Windows 11 26300 / Claude Code with Claude Opus 5.5

AI-written and posted by Foniass without a human check of each point.

<!-- greyfinch-sync:ivall/xcl:comment:7341410004 --> Imported from [ivall/xcl](https://github.com/xcl-dev/xcl/pull/3648#issuecomment-7341410004). Originally posted by [Foniass](https://github.com/Foniass) on 2026-12-02T09:44:18Z. --- merge-check base: 1c8b551475e1e7bfe48f94448492a4c3544a2e0f head: 0e5223e618ee902090440bba046f114dd15a7a13 merge: clean build: cargo build --workspace -> ok tests: cargo test --workspace --no-fail-fast -- --test-threads=4 -> 2910 passed, 0 failed, 51 ignored; SKIPPED: 28 (20 no wave64, 2 no d3d12SDKLayers.dll, 1 no D3D12 debug layer, 1 no heap windows, 1 cannot disable the driver cache, 3 others; the same set as on main); failing: none review: read by one reviewer at this same head; the review is at https://github.com/xcl-dev/xcl/pull/3648#issuecomment-7340435556 and its findings stand. Most serious: the fix is correct; the test covers the helper, not the call site in `build`. performance: 60 s smoke of a retail title, main 1c8b5514 vs this merge (0e5223e6): 1652 -> 1667 frames shown (100 %; last presents per 2 s: 61 60 60), peak commit 18228 -> 17892 MB, 0 device removals, 0 crashes. verdict: safe to merge: no blocking finding in the review (the points there are minor), the suite passes and the smoke run is clean hardware / tool and model: RTX 5070 Ti (driver 621.14), Ryzen 7 7800X3D, Windows 11 26300 / Claude Code with Claude Opus 5.5 AI-written and posted by Foniass without a human check of each point.
Decodere commented 2026-12-02 09:58:28 +00:00 (Migrated from github.com)

Imported from ivall/xcl. Originally posted by Decodere on 2026-12-02T09:58:28Z.


merge-check
base: 1c8b551475
head: 0e5223e618
merge: clean (fast-forward onto base, commit 0e5223e618, git diff --check clean)
build: cargo build --workspace ok; cargo build --release -p xcl ok
tests: 43 targets combined: 2910 passed, 0 failed, 51 ignored (+1 new test vs main baseline). Specifically, all 42 non-lib workspace targets under cargo test --workspace --no-fail-fast -- --test-threads=4 --nocapture passed 148/0 (20 ignored, including gpu_two_pass_cache_e2e), and cargo test -p xcl --lib -- --test-threads=2 passed 2762/0 (31 ignored; --test-threads=4 hit the unrelated pre-existing ASLR collision on synthetic pointer 0x7c000041 in main's set_pipeline_state_goes_into_a_run_only_with_a_two_pass_pipeline -> real.rs:1237). All 17/17 hle::d3d12x::real::dispatch_chain::tests passed with 0 skips, including new replacing_chain_entry_drops_previous_value_outside_lock. 28 SKIPPED lines (host environment only: 22 no wave64, 2 no d3d12SDKLayers.dll, 1 no D3D12 debug layer, 2 no heap windows, 1 cannot disable the driver cache (Windows Developer Mode required)).
review: Read the full merged diff (+25/-1 in crates/xcl/src/hle/d3d12x/real/dispatch_chain.rs). Adds replace_entry, which captures the displaced Option<V> returned by map.lock().unwrap().insert(key, value) and drops it after the MutexGuard ends. Using replace_entry(&CHAINS, *result as usize, chain) in build prevents self-deadlock when dropping a replaced Chain releases a tail PSO whose COM Release hook calls back into forget_pso and locks CHAINS. The new unit test verifies try_lock().is_ok() inside Drop on replacement and cleans up outside the lock. Minimal, exact, no scope creep or UB.
performance: 60 s retail smoke (title: 29640639): 1666 frames (+14.0% vs 1c8b5514 baseline 1462 frames), 14601 MB peak commit (-9.0% vs 16043 MB baseline), 0 device removals, 0 crashes.

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

verdict: safe to merge
hardware / tool and model: NVIDIA GeForce RTX 4060 8 GB (driver 621.40), Intel Core i5-11400F, 32 GB RAM, Windows 11 Build 26200, Rust 1.100.1 MSVC, DXC 1.9.2602.17 / Antigravity (Gemini 4 Neon)

<!-- greyfinch-sync:ivall/xcl:comment:7341765817 --> Imported from [ivall/xcl](https://github.com/xcl-dev/xcl/pull/3648#issuecomment-7341765817). Originally posted by [Decodere](https://github.com/Decodere) on 2026-12-02T09:58:28Z. --- merge-check base: 1c8b551475e1e7bfe48f94448492a4c3544a2e0f head: 0e5223e618ee902090440bba046f114dd15a7a13 merge: clean (fast-forward onto base, commit 0e5223e618ee902090440bba046f114dd15a7a13, git diff --check clean) build: cargo build --workspace ok; cargo build --release -p xcl ok tests: 43 targets combined: 2910 passed, 0 failed, 51 ignored (+1 new test vs main baseline). Specifically, all 42 non-lib workspace targets under `cargo test --workspace --no-fail-fast -- --test-threads=4 --nocapture` passed 148/0 (20 ignored, including gpu_two_pass_cache_e2e), and `cargo test -p xcl --lib -- --test-threads=2` passed 2762/0 (31 ignored; `--test-threads=4` hit the unrelated pre-existing ASLR collision on synthetic pointer 0x7c000041 in main's set_pipeline_state_goes_into_a_run_only_with_a_two_pass_pipeline -> real.rs:1237). All 17/17 hle::d3d12x::real::dispatch_chain::tests passed with 0 skips, including new replacing_chain_entry_drops_previous_value_outside_lock. 28 SKIPPED lines (host environment only: 22 no wave64, 2 no d3d12SDKLayers.dll, 1 no D3D12 debug layer, 2 no heap windows, 1 cannot disable the driver cache (Windows Developer Mode required)). review: Read the full merged diff (+25/-1 in crates/xcl/src/hle/d3d12x/real/dispatch_chain.rs). Adds `replace_entry`, which captures the displaced `Option<V>` returned by `map.lock().unwrap().insert(key, value)` and drops it after the `MutexGuard` ends. Using `replace_entry(&CHAINS, *result as usize, chain)` in `build` prevents self-deadlock when dropping a replaced `Chain` releases a tail PSO whose COM Release hook calls back into `forget_pso` and locks `CHAINS`. The new unit test verifies `try_lock().is_ok()` inside `Drop` on replacement and cleans up outside the lock. Minimal, exact, no scope creep or UB. performance: 60 s retail smoke (title: 29640639): 1666 frames (+14.0% vs 1c8b5514 baseline 1462 frames), 14601 MB peak commit (-9.0% vs 16043 MB baseline), 0 device removals, 0 crashes. ``` xcl-smoke v1 commit: 0e5223e618ee902090440bba046f114dd15a7a13 gpu: NVIDIA GeForce RTX 4060 title: 29640639 seconds: 60 exit: 0x0 frames shown: 1666 (PresentX 1666) presents per 2 s (last 3): 60 60 60 device removals: 0 crashes: 0 peak commit: 14601 MB ``` verdict: safe to merge hardware / tool and model: NVIDIA GeForce RTX 4060 8 GB (driver 621.40), Intel Core i5-11400F, 32 GB RAM, Windows 11 Build 26200, Rust 1.100.1 MSVC, DXC 1.9.2602.17 / Antigravity (Gemini 4 Neon)
bmjask commented 2026-12-02 18:07:00 +00:00 (Migrated from github.com)

Imported from ivall/xcl. Originally posted by bmjask on 2026-12-02T18:07:00Z.

Code review location: crates/xcl/src/hle/d3d12x/real/dispatch_chain.rs; line 316

@@ -307,7 +312,8 @@ pub(super) unsafe fn build(this: *mut c_void, create: PsoFn, desc: &D3D12_COMPUT
     PROGRAMS.fetch_add(1, Ordering::Relaxed);
     PIECES.fetch_add(plan.pieces.len() as u64, Ordering::Relaxed);
     *result = first.into_raw();
-    CHAINS.lock().unwrap().insert(*result as usize, chain);
+    // Drop any replaced chain outside `CHAINS`: releasing a tail PSO also goes through `forget_pso`.
+    replace_entry(&CHAINS, *result as usize, chain);

High: A key collision in CHAINS only happens in the pso_release race (the driver Release returns 0 before forget_pso runs, real.rs:2615-2620), and in that race the stale forget_pso then removes the newly built chain, so the PR turns the deadlock into silently wrong GPU work. Thread A releases split PSO P: f(this) returns 0, so the driver can reuse the address, but A hasn't reached forget_pso yet. Thread B creates a new split program, gets the same address P, and build calls replace_entry(&CHAINS, P, new_chain), dropping the old chain (no deadlock now). A then runs forget_pso(P) -> dispatch_chain::forget(P), which removes B's live chain and releases its tail PSOs. Every later dispatch(…, P, …) misses and falls back to dispatching the first piece alone (or remap_fallback), so the program's later pieces never run. Forgetting before the final Release, or keying CHAINS by (address, generation) like pso_manifest::ready_compute_generation, would fix the root cause. At minimum, logging when replaced.is_some() would make it visible.

<!-- greyfinch-sync:ivall/xcl:review-comment:4773705883 --> Imported from [ivall/xcl](https://github.com/xcl-dev/xcl/pull/3648#discussion_r4773705883). Originally posted by [bmjask](https://github.com/bmjask) on 2026-12-02T18:07:00Z. **Code review location:** `crates/xcl/src/hle/d3d12x/real/dispatch_chain.rs`; line 316 ~~~~diff @@ -307,7 +312,8 @@ pub(super) unsafe fn build(this: *mut c_void, create: PsoFn, desc: &D3D12_COMPUT PROGRAMS.fetch_add(1, Ordering::Relaxed); PIECES.fetch_add(plan.pieces.len() as u64, Ordering::Relaxed); *result = first.into_raw(); - CHAINS.lock().unwrap().insert(*result as usize, chain); + // Drop any replaced chain outside `CHAINS`: releasing a tail PSO also goes through `forget_pso`. + replace_entry(&CHAINS, *result as usize, chain); ~~~~ --- **High**: A key collision in CHAINS only happens in the `pso_release` race (the driver Release returns 0 before `forget_pso` runs, real.rs:2615-2620), and in that race the stale `forget_pso` then removes the newly built chain, so the PR turns the deadlock into silently wrong GPU work. Thread A releases split PSO P: `f(this)` returns 0, so the driver can reuse the address, but A hasn't reached `forget_pso` yet. Thread B creates a new split program, gets the same address P, and `build` calls `replace_entry(&CHAINS, P, new_chain)`, dropping the old chain (no deadlock now). A then runs `forget_pso(P)` -> `dispatch_chain::forget(P)`, which removes B's live chain and releases its tail PSOs. Every later `dispatch(…, P, …)` misses and falls back to dispatching the first piece alone (or remap_fallback), so the program's later pieces never run. Forgetting before the final Release, or keying CHAINS by (address, generation) like `pso_manifest::ready_compute_generation`, would fix the root cause. At minimum, logging when `replaced.is_some()` would make it visible.
bmjask commented 2026-12-02 18:07:00 +00:00 (Migrated from github.com)

Imported from ivall/xcl. Originally posted by bmjask on 2026-12-02T18:07:00Z.

Code review location: crates/xcl/src/hle/d3d12x/real/dispatch_chain.rs; line 278

@@ -278,6 +278,11 @@ unsafe fn signature_for(dev: &ID3D12Device, samplers: &[pso::StaticSampler]) ->
     Some(SIGNATURES.lock().unwrap().entry(key).or_insert(created).clone())

Medium: signature_for has the same pattern this PR fixes: SIGNATURES.lock().unwrap().entry(key).or_insert(created) drops the losing created root signature (a COM Release) while SIGNATURES is still locked. When two preparation workers build split programs with the same device and samplers at once, both miss and create a root signature, and the second's or_insert drops its duplicate inside the SIGNATURES critical section. If that Release is hooked or re-enters XCL code that touches SIGNATURES, it deadlocks the way CHAINS did. Binding the duplicate and dropping it after unlocking would avoid it.

<!-- greyfinch-sync:ivall/xcl:review-comment:4773705893 --> Imported from [ivall/xcl](https://github.com/xcl-dev/xcl/pull/3648#discussion_r4773705893). Originally posted by [bmjask](https://github.com/bmjask) on 2026-12-02T18:07:00Z. **Code review location:** `crates/xcl/src/hle/d3d12x/real/dispatch_chain.rs`; line 278 ~~~~diff @@ -278,6 +278,11 @@ unsafe fn signature_for(dev: &ID3D12Device, samplers: &[pso::StaticSampler]) -> Some(SIGNATURES.lock().unwrap().entry(key).or_insert(created).clone()) ~~~~ --- **Medium**: `signature_for` has the same pattern this PR fixes: `SIGNATURES.lock().unwrap().entry(key).or_insert(created)` drops the losing `created` root signature (a COM Release) while SIGNATURES is still locked. When two preparation workers build split programs with the same device and samplers at once, both miss and create a root signature, and the second's `or_insert` drops its duplicate inside the SIGNATURES critical section. If that Release is hooked or re-enters XCL code that touches SIGNATURES, it deadlocks the way CHAINS did. Binding the duplicate and dropping it after unlocking would avoid it.
bmjask commented 2026-12-02 18:07:00 +00:00 (Migrated from github.com)

Imported from ivall/xcl. Originally posted by bmjask on 2026-12-02T18:07:00Z.

Review: COMMENTED


Review of 0e5223e6: 2 finding(s) (1 high, 1 medium), most severe first.

  1. High crates/xcl/src/hle/d3d12x/real/dispatch_chain.rs:316: A key collision in CHAINS only happens in the pso_release race (the driver Release returns 0 before forget_pso runs, real.rs:2615-2620), and in that race the stale forget_pso then removes the newly built chain, so the PR turns the .... (inline)
  2. Medium crates/xcl/src/hle/d3d12x/real/dispatch_chain.rs:278: signature_for has the same pattern this PR fixes: SIGNATURES.lock().unwrap().entry(key).or_insert(created) drops the losing created root signature (a COM Release) while SIGNATURES is still locked. (inline)

These findings come from an AI-assisted review (Claude Code) and focus on correctness. Each is a candidate; please check it before acting on it.

<!-- greyfinch-sync:ivall/xcl:review:6156150397 --> Imported from [ivall/xcl](https://github.com/xcl-dev/xcl/pull/3648#pullrequestreview-6156150397). Originally posted by [bmjask](https://github.com/bmjask) on 2026-12-02T18:07:00Z. **Review:** COMMENTED --- Review of `0e5223e6`: 2 finding(s) (1 high, 1 medium), most severe first. 1. **High** `crates/xcl/src/hle/d3d12x/real/dispatch_chain.rs:316`: A key collision in CHAINS only happens in the `pso_release` race (the driver Release returns 0 before `forget_pso` runs, real.rs:2615-2620), and in that race the stale `forget_pso` then removes the newly built chain, so the PR turns the .... (inline) 2. **Medium** `crates/xcl/src/hle/d3d12x/real/dispatch_chain.rs:278`: `signature_for` has the same pattern this PR fixes: `SIGNATURES.lock().unwrap().entry(key).or_insert(created)` drops the losing `created` root signature (a COM Release) while SIGNATURES is still locked. (inline) --- _These findings come from an AI-assisted review (Claude Code) and focus on correctness. Each is a candidate; please check it before acting on it._
bmjask commented 2026-12-02 18:07:05 +00:00 (Migrated from github.com)

Imported from ivall/xcl. Originally posted by bmjask on 2026-12-02T18:07:05Z.


@Prime1997 could you take a look at this one for review and merge? My automated review notes are above, if any.

<!-- greyfinch-sync:ivall/xcl:comment:7353962719 --> Imported from [ivall/xcl](https://github.com/xcl-dev/xcl/pull/3648#issuecomment-7353962719). Originally posted by [bmjask](https://github.com/bmjask) on 2026-12-02T18:07:05Z. --- @Prime1997 could you take a look at this one for review and merge? My automated review notes are above, if any.
Kalmarv commented 2026-12-02 18:15:45 +00:00 (Migrated from github.com)

Imported from ivall/xcl. Originally posted by Kalmarv on 2026-12-02T18:15:45Z.


Thanks @bmjask for the review notes — leaving 0e5223e6 untouched (3 safe to merge merge-checks) since neither finding is a regression of this PR:

  1. dispatch_chain.rs:316 (pso_release race / key collision): Both collision paths on main (prepared pipeline reuse in compute_container when the same split program is created twice, and the pre-existing pso_release window where Release returns 0 before forget_pso runs) exist independently of this PR, and the pso_release ordering is already noted under Not tested (and in @Foniass's review above). Fixing pso_release / generation-keying across forget_pso is a separate logical change; this PR is intentionally scoped to removing the self-deadlock on replacement.
  2. dispatch_chain.rs:278 (signature_for / SIGNATURES): Kind::RootSignature does not hook Release (real.rs overrides falls through to _ => {}) and no Release hook touches SIGNATURES, so dropping a duplicate ID3D12RootSignature inside SIGNATURES cannot re-enter SIGNATURES the way Kind::PipelineState's pso_release hook re-enters CHAINS. Still good defensive hygiene to drop outside the lock in a follow-up if touched.
<!-- greyfinch-sync:ivall/xcl:comment:7354241721 --> Imported from [ivall/xcl](https://github.com/xcl-dev/xcl/pull/3648#issuecomment-7354241721). Originally posted by [Kalmarv](https://github.com/Kalmarv) on 2026-12-02T18:15:45Z. --- Thanks @bmjask for the review notes — leaving `0e5223e6` untouched (3 `safe to merge` merge-checks) since neither finding is a regression of this PR: 1. **`dispatch_chain.rs:316` (`pso_release` race / key collision):** Both collision paths on `main` (prepared pipeline reuse in `compute_container` when the same split program is created twice, and the pre-existing `pso_release` window where `Release` returns 0 before `forget_pso` runs) exist independently of this PR, and the `pso_release` ordering is already noted under **Not tested** (and in @Foniass's review above). Fixing `pso_release` / generation-keying across `forget_pso` is a separate logical change; this PR is intentionally scoped to removing the self-deadlock on replacement. 2. **`dispatch_chain.rs:278` (`signature_for` / `SIGNATURES`):** `Kind::RootSignature` does not hook `Release` (`real.rs` `overrides` falls through to `_ => {}`) and no `Release` hook touches `SIGNATURES`, so dropping a duplicate `ID3D12RootSignature` inside `SIGNATURES` cannot re-enter `SIGNATURES` the way `Kind::PipelineState`'s `pso_release` hook re-enters `CHAINS`. Still good defensive hygiene to drop outside the lock in a follow-up if touched.
This pull request cannot be merged automatically due to conflicts.
Merge manually to resolve the conflicts.
View command line instructions

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u origin 'upstream-pr/3648:upstream-pr/3648'
git switch 'upstream-pr/3648'

Merge

Merge the changes and update on Forgejo.

Warning: The "Autodetect manual merge" setting is not enabled for this repository, you will have to mark this pull request as manually merged afterwards.

git switch 'main'
git merge --no-ff 'upstream-pr/3648'
git switch 'upstream-pr/3648'
git rebase 'main'
git switch 'main'
git merge --ff-only 'upstream-pr/3648'
git switch 'upstream-pr/3648'
git rebase 'main'
git switch 'main'
git merge --no-ff 'upstream-pr/3648'
git switch 'main'
git merge --squash 'upstream-pr/3648'
git switch 'main'
git merge --ff-only 'upstream-pr/3648'
git switch 'main'
git merge 'upstream-pr/3648'
git push origin 'main'
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!3648
No description provided.