Skip to content

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

Open
Kalmarv wants to merge 1 commit into
xcl-dev:mainfrom
Kalmarv:fix/dispatch-chain-drop-outside-lock
Open

Kalmarv wants to merge 1 commit into
xcl-dev:mainfrom
Kalmarv:fix/dispatch-chain-drop-outside-lock

Conversation

@Kalmarv

@Kalmarv Kalmarv commented Dec 2, 2026 •

Copy link
Copy Markdown
Contributor

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.

When replacing an existing entry in CHAINS, bind the returned Option<Arc<Chain>> and drop it after releasing the CHAINS mutex so dropping a replaced chain (and any COM/D3D12 pipeline objects inside, whose Release can re-enter forget_pso -> forget) never runs while holding CHAINS.

Matches dispatch_chain::forget and adds a unit test verifying re-entrant lock acquisition during Drop.

Co-Authored-By: Antigravity (Gemini 4 Neon) <noreply@google.com>
@github-actions github-actions Bot added area:d3d12 D3D12.X runtime, command lists, resources, sparse memory gpu:both Code affects both AMD and NVIDIA GPUs (scope tag; see needs:*-test for remaining checks) labels Dec 2, 2026

@SavageCore SavageCore left a comment •

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sorry, ignore, agent went crazy

@vasanex

vasanex commented Dec 2, 2026 •

Copy link
Copy Markdown

merge-check
base: 1c8b551
head: 0e5223e
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.

@github-actions github-actions Bot added needs-1st-check Passes gates; needs first independent merge-check (#3041) needs:amd-test Needs a test report or merge-check on an AMD GPU needs:nvidia-test Needs a test report or merge-check on an NVIDIA GPU needs-2nd-check Has 1 safe merge-check; needs second independent check (#3041) and removed needs-1st-check Passes gates; needs first independent merge-check (#3041) needs:nvidia-test Needs a test report or merge-check on an NVIDIA GPU labels Dec 2, 2026
@Foniass

Foniass commented Dec 2, 2026

Copy link
Copy Markdown
Collaborator

Review (head 0e5223e, 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

Foniass commented Dec 2, 2026

Copy link
Copy Markdown
Collaborator

merge-check
base: 1c8b551
head: 0e5223e
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 #3648 (comment) 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 1c8b551 vs this merge (0e5223e): 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.

@github-actions github-actions Bot added ready-to-merge Has 2 independent safe merge-checks on current head (#3041) and removed needs-2nd-check Has 1 safe merge-check; needs second independent check (#3041) needs:amd-test Needs a test report or merge-check on an AMD GPU labels Dec 2, 2026
@Decodere

Decodere commented Dec 2, 2026 •

Copy link
Copy Markdown
Contributor

merge-check
base: 1c8b551
head: 0e5223e
merge: clean (fast-forward onto base, commit 0e5223e, 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 1c8b551 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)

@github-actions github-actions Bot removed the ready-to-merge Has 2 independent safe merge-checks on current head (#3041) label Dec 2, 2026

@bmjask bmjask left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

*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);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

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

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

bmjask commented Dec 2, 2026

Copy link
Copy Markdown
Contributor

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

@Kalmarv

Kalmarv commented Dec 2, 2026

Copy link
Copy Markdown
Contributor Author

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area:d3d12 D3D12.X runtime, command lists, resources, sparse memory gpu:both Code affects both AMD and NVIDIA GPUs (scope tag; see needs:*-test for remaining checks)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants