D3D12.X: drop replaced dispatch chain outside CHAINS mutex #3648
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/3648"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Imported from ivall/xcl. Originally posted by Kalmarv on 2026-12-02T03:29:54Z.
Drop any replaced
Arc<Chain>outside theCHAINSmutex when inserting intodispatch_chain::CHAINS.dispatch_chain::forgetalready binds the removedOption<Arc<Chain>>and drops it after unlockingCHAINS, because dropping aChainreleases its COM/D3D12 tail pipeline states (whoseReleasecan re-enterforget_pso->dispatch_chain::forget).dispatch_chain::buildcalledCHAINS.lock().unwrap().insert(...)directly, which would drop a replacedArc<Chain>while still holdingCHAINS.What is in it
replace_entryhelper incrates/xcl/src/hle/d3d12x/real/dispatch_chain.rsto bind and drop the previous map value after releasing the mutex, and use it when inserting intoCHAINSindispatch_chain::build.replacing_chain_entry_drops_previous_value_outside_lockverifying the map mutex is unlocked when a replaced entry'sDropruns.Tested
rustc 1.99.0 (b940084d7 2026-09-28), debug tests + release build (cargo build --release -p xcl: 0 new warnings vsmainbaseline of 39).scripts/test.ps1(workspace build,cargo bench -p xcl --bench '*' -- --test,cargo test --workspace -- --test-threads=4with DXC 1.9.2609): exit 0, 0 failed (replacing_chain_entry_drops_previous_value_outside_lockpassed; SKIPPED GPU tests: 22 no wave64, 2 no heap windows).Regression run
main:
PR:
Not tested
XCL_SAMPLE_*unset).dispatch_chain::buildend-to-end on a colliding key (the unit test exercises thereplace_entryhelper directly rather thanbuild).pso_release(real.rs) ordering whereReleaseruns beforeforget_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
replace_entryhelper and unit test, ran the local build/test/smoke verification, and drafted this PR description.Imported from ivall/xcl. Originally posted by SavageCore on 2026-12-02T04:30:46Z.
Review: APPROVED
sorry, ignore, agent went crazy
Imported from ivall/xcl. Originally posted by vasanex on 2026-12-02T07:43:59Z.
merge-check
base:
1c8b551475head:
0e5223e618merge: 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_lockran and passed.review: whole diff read (one file, +25/-1), no finding. In
replace_entrythe guard is a temporary of theletstatement, so it is released beforereplacedis dropped. The new test checks the helper with a value whoseDroptries 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.
Imported from ivall/xcl. Originally posted by Foniass on 2026-12-02T09:08:25Z.
Review (head
0e5223e6, diff read, not a merge-check):replacing_chain_entry_drops_previous_value_outside_lock(dispatch_chain.rs~697) fails ifreplace_entrygoes back to a one-statementmap.lock().unwrap().insert(..);, because the returned value is dropped before the guard and thetry_lockassert fires. It still passes ifbuild(~316) stops callingreplace_entryand goes back to inserting underCHAINSdirectly. Without a GPU there is probably no better test, but the description should not read as ifbuildwere covered.buildfinds 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 realReleasebeforeforget_pso, so another thread's new first piece can reuse the address in between. In case (2), after this PR the lateforget_psoon 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::PipelineStatehooksReleaseon 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::forgettakesCHAINSagain on the same thread. Theletbinding drops the guard beforedrop(replaced), the same asforget(~320). In-flight lists keep the replaced chain alive throughRecording.chain: Arc<Chain>(~393, dropped outsideRECORDINGSat ~490), so the early drop frees nothing the GPU still uses. No textual or functional overlap with #3528 (different hunks ofdispatch_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 oneCHAINSkey, 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.
Imported from ivall/xcl. Originally posted by Foniass on 2026-12-02T09:44:18Z.
merge-check
base:
1c8b551475head:
0e5223e618merge: 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
1c8b5514vs 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.
Imported from ivall/xcl. Originally posted by Decodere on 2026-12-02T09:58:28Z.
merge-check
base:
1c8b551475head:
0e5223e618merge: 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 --nocapturepassed 148/0 (20 ignored, including gpu_two_pass_cache_e2e), andcargo test -p xcl --lib -- --test-threads=2passed 2762/0 (31 ignored;--test-threads=4hit 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 displacedOption<V>returned bymap.lock().unwrap().insert(key, value)and drops it after theMutexGuardends. Usingreplace_entry(&CHAINS, *result as usize, chain)inbuildprevents self-deadlock when dropping a replacedChainreleases a tail PSO whose COM Release hook calls back intoforget_psoand locksCHAINS. The new unit test verifiestry_lock().is_ok()insideDropon 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
1c8b5514baseline 1462 frames), 14601 MB peak commit (-9.0% vs 16043 MB baseline), 0 device removals, 0 crashes.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)
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 316High: A key collision in CHAINS only happens in the
pso_releaserace (the driver Release returns 0 beforeforget_psoruns, real.rs:2615-2620), and in that race the staleforget_psothen 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 reachedforget_psoyet. Thread B creates a new split program, gets the same address P, andbuildcallsreplace_entry(&CHAINS, P, new_chain), dropping the old chain (no deadlock now). A then runsforget_pso(P)->dispatch_chain::forget(P), which removes B's live chain and releases its tail PSOs. Every laterdispatch(…, 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) likepso_manifest::ready_compute_generation, would fix the root cause. At minimum, logging whenreplaced.is_some()would make it visible.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 278Medium:
signature_forhas the same pattern this PR fixes:SIGNATURES.lock().unwrap().entry(key).or_insert(created)drops the losingcreatedroot 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'sor_insertdrops 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.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.crates/xcl/src/hle/d3d12x/real/dispatch_chain.rs:316: A key collision in CHAINS only happens in thepso_releaserace (the driver Release returns 0 beforeforget_psoruns, real.rs:2615-2620), and in that race the staleforget_psothen removes the newly built chain, so the PR turns the .... (inline)crates/xcl/src/hle/d3d12x/real/dispatch_chain.rs:278:signature_forhas the same pattern this PR fixes:SIGNATURES.lock().unwrap().entry(key).or_insert(created)drops the losingcreatedroot 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.
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.
Imported from ivall/xcl. Originally posted by Kalmarv on 2026-12-02T18:15:45Z.
Thanks @bmjask for the review notes — leaving
0e5223e6untouched (3safe to mergemerge-checks) since neither finding is a regression of this PR:dispatch_chain.rs:316(pso_releaserace / key collision): Both collision paths onmain(prepared pipeline reuse incompute_containerwhen the same split program is created twice, and the pre-existingpso_releasewindow whereReleasereturns 0 beforeforget_psoruns) exist independently of this PR, and thepso_releaseordering is already noted under Not tested (and in @Foniass's review above). Fixingpso_release/ generation-keying acrossforget_psois a separate logical change; this PR is intentionally scoped to removing the self-deadlock on replacement.dispatch_chain.rs:278(signature_for/SIGNATURES):Kind::RootSignaturedoes not hookRelease(real.rsoverridesfalls through to_ => {}) and noReleasehook touchesSIGNATURES, so dropping a duplicateID3D12RootSignatureinsideSIGNATUREScannot re-enterSIGNATURESthe wayKind::PipelineState'spso_releasehook re-entersCHAINS. Still good defensive hygiene to drop outside the lock in a follow-up if touched.View command line instructions
Checkout
From your project repository, check out a new branch and test the changes.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.