Skip to content

D3D12.X: replacing the sparse state drops the old one after the lock - #461

Open
coenflict wants to merge 1 commit into
ivall:mainfrom
coenflict:sparse-init-drop-outside-lock
Open

coenflict wants to merge 1 commit into
ivall:mainfrom
coenflict:sparse-init-drop-outside-lock

Conversation

@coenflict

Copy link
Copy Markdown

sparse::init assigned the new sparse state under the STATE guard, so the old state was dropped with the guard held. Dropping it releases its resources through real::res_release → sparse::forget_buffer, which locks STATE again and waits for itself. The old state is now taken out under the guard and dropped after it. Fixes #344

What is in it

  • crates/xcl/src/hle/d3d12x/sparse.rs: replace_unlocked puts the new value in and drops the old one after the guard; init uses it. A unit test replaces a value whose Drop locks the same mutex, twice, on a thread with a 10 s timeout.

Tested

  • NVIDIA GeForce RTX 5090, Windows 11 Pro 26340, Rust 1.97.1 MSVC, debug (tests) / release (smoke)
  • cargo test -p xcl --lib replacing_drops_the_old_value: with the old assignment it fails (deadlock, timeout after 10 s); with the fix it passes
  • cargo test --workspace -- --test-threads=4: 677 passed, 0 failed, 41 ignored (lib: 637 passed, 0 failed, 21 ignored)
  • SKIPPED: gpu_geometry_wave32_prefix_count_over_a_mask, gpu_wave32_program_in_wave64_hardware (no wave64)

Regression run

main:

xcl-smoke v1
commit: a6f5f7a74cc620c1f88a9e3669a633d10c3c8212
gpu: NVIDIA GeForce RTX 5090
title: GTA6.Xbox.v1.0.2.1
seconds: 60
exit: 0x0
frames shown: 1498 (PresentX 1498)
presents per 2 s (last 3): 60 60 60
device removals: 0
crashes: 0
peak commit: 14770 MB

this PR:

xcl-smoke v1
commit: 682d06abba083b18be56d4149de7ad5425c81d3b
gpu: NVIDIA GeForce RTX 5090
title: GTA6.Xbox.v1.0.2.1
seconds: 60
exit: 0x0
frames shown: 1531 (PresentX 1531)
presents per 2 s (last 3): 60 60 60
device removals: 0
crashes: 0
peak commit: 14758 MB

This PR's head ran four times: 1149, 887, 1506 and 1531 frames. The first two runs came before any run of main. The 887 run fell to 8 presents per 2 s in its last seconds. After that, alternating main and PR runs gave 1499 and 1498 frames on main and 1506 and 1531 on this PR. main ran three times in all, with 1495 to 1499 frames. The low runs were not seen once the runs alternated, and the change only acts when sparse::init runs twice.

Not tested

  • The deadlock itself on a real double init: the test exercises the replace on its own, not two real::init() calls racing.
  • The title only calls sparse::init once, so the smoke run checks for regressions and does not reach the fixed path.
  • AMD and Intel GPUs.

AI use

  • Claude Code, Claude Opus 5.5: wrote the fix, the test and this description, and ran the tests and smoke runs.

🤖 Generated with Claude Code

`sparse::init` assigned the new state under the `STATE` guard, so the old
state was dropped while the guard was held. Dropping it releases its
resources through `real::res_release`, which calls `forget_buffer`, which
locks `STATE` again: the thread waited for itself, and every later user
of `STATE` waited behind it. Seen as a suite run hung for 3600 s, with
two tests calling `real::init()` at once and both reaching
`sparse::init` (ivall#344).

The old state is now taken out under the guard and dropped after it.

Fixes ivall#344

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@coenflict
coenflict marked this pull request as ready for review November 24, 2026 00:40
@MesshM

MesshM commented Nov 24, 2026

Copy link
Copy Markdown

Heads-up for whoever merges: this PR and #443 (opened ~12 minutes earlier) fix the same thing, #344, with the same change in sparse::init: take the old state out under the STATE guard and drop it after the guard. Only one of the two should land.

What differs, so either can be picked:

  • D3D12.X: the sparse state is replaced outside its lock #443: the change inline in sparse::init, plus an integration test (crates/xcl/tests/sparse_reinit.rs) that runs the real path on a device: a sparse block written and read back, then sparse::init again on a second thread. I ran it on an AMD RX 9060 XT (RDNA4) and posted the results there: full suite 677 passed / 0 failed / 0 SKIPPED, and with main's sparse.rs the test fails after 60 s. Its regression run is still pending.
  • D3D12.X: replacing the sparse state drops the old one after the lock #461 (this one): a replace_unlocked helper and a unit test with a Drop that relocks the mutex (no device needed), and it already has a regression run on a retail title (RTX 5090).

I have not run this PR's head. Written with Claude Code (Claude Opus 5.5); the comparison was checked against both diffs.

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

D3D12.X: replacing the sparse state deadlocks when the old state still holds placed buffers

2 participants