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

Closed
github-bot wants to merge 1 commit from gh-pr/461/head into gh-pr/461/base
Owner

@⁠coenflict on GitHub · 2026-11-24 00:34 UTC · original
coenflict:sparse-init-drop-outside-lock → main · +29 −1 · 1 files · 1 commits · closed without merging

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: 9cc6a39e
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: 9cc6a39e
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

**@⁠coenflict** on GitHub · 2026-11-24 00:34 UTC · [original](https://github.com/xcl-dev/xcl/pull/461) `coenflict:sparse-init-drop-outside-lock` → `main` · +29 −1 · 1 files · 1 commits · closed without merging `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: 9cc6a39e 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: 9cc6a39e 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](https://claude.com/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` (#344).

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

Fixes #344

Co-Authored-By: Claude Opus 5.5 <[email protected]>
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 04:22:31 +00:00
github-bot closed this pull request 2026-11-26 04:22:31 +00:00
Author
Owner

@⁠MesshM on GitHub · 2026-11-24 00:52 UTC · original

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:

  • #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.
  • #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.

**@⁠MesshM** on GitHub · 2026-11-24 00:52 UTC · [original](https://github.com/xcl-dev/xcl/pull/461#issuecomment-7118525718) 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: - **#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. - **#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.
Author
Owner

@⁠upipibi on GitHub · 2026-11-24 05:28 UTC · original

For cleanup: #443, the same change to sparse::init that @⁠MesshM pointed out above, was merged at 03:38 UTC, and this PR now conflicts with main in sparse.rs. The only part not on main is the unit test; if it adds coverage #443 lacks, it could go in as a test-only PR, otherwise this one can close.

AI-assisted (Claude Code), read-only review.

**@⁠upipibi** on GitHub · 2026-11-24 05:28 UTC · [original](https://github.com/xcl-dev/xcl/pull/461#issuecomment-7122988173) For cleanup: #443, the same change to `sparse::init` that @⁠MesshM pointed out above, was merged at 03:38 UTC, and this PR now conflicts with `main` in `sparse.rs`. The only part not on `main` is the unit test; if it adds coverage #443 lacks, it could go in as a test-only PR, otherwise this one can close. AI-assisted (Claude Code), read-only review.
Author
Owner

@⁠raylitebull on GitHub · 2026-11-24 10:42 UTC · original

Closing as a duplicate of #443, which has already been merged into main.

**@⁠raylitebull** on GitHub · 2026-11-24 10:42 UTC · [original](https://github.com/xcl-dev/xcl/pull/461#issuecomment-7129134738) Closing as a duplicate of #443, which has already been merged into `main`.

Pull request closed

This discussion has been locked. Commenting is limited to contributors.
No description provided.