Skip to content

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

Done

Description

@Kalmarv

d3d12x::sparse::init installs its new state with *STATE.lock().unwrap() = Some(s); (main 3516080, crates/xcl/src/hle/d3d12x/sparse.rs:549). The assignment drops the old State while the guard is still held. Dropping it releases its D3D12 resources. A placed buffer's release goes through XCL's hook real::res_release (real.rs:1547), which calls sparse::forget_buffer (real.rs:1563), and forget_buffer locks STATE again (sparse.rs:1472–1473). std::sync::Mutex isn't reentrant, so the thread waits for itself forever, and every later user of STATE waits behind it.

When it happens

  • sparse::init runs a second time in one process: every real::init() reaches it (real.rs:348), and several GPU tests call real::init();
  • and the state being replaced still holds placed buffers, i.e. resources whose release reaches forget_buffer.

So it depends on the order of the tests and on timing.

Seen
A cargo test --workspace -- --test-threads=4 run of #253 at 12328ac (on main 9ab7d76) hung until the GPU queue killed it at 3600 s. That PR's own test (eh::tests::thread_dumps_while_threads_run_and_exit) prints in-process thread dumps, and its last dump shows the state:

  • gpu::recomp::tests::gpu_uma_import_check_at_init (it calls real::init()) is in the middle of
    drop_glue<Option<sparse::State>> → drop_glue<sparse::State> → … → windows_core::IUnknown::drop → real::res_release → sparse::forget_buffer → Mutex::lock_contended.
  • gpu_sparse_vram_descriptors_buffers_and_xmem waits in sparse::before_execute → Mutex::lock_contended.
  • gpu_typed_buffer_loads_and_stores waits in res_release → forget_buffer → Mutex::lock_contended.

Those three, and gpu_unified_memory_arena (which started after the last dump, so its stack isn't known), were still listed as "running for over 60 seconds" when the run was killed. Symbols come from the PDB of that exact build. The other suite runs on this machine today did not hang, so it is rare.

Plan

  1. In sparse::init, take the old state out and drop it after the guard:
    let old = std::mem::replace(&mut *STATE.lock().unwrap(), Some(s)); drop(old);. Its resources' releases then find the new state, where they aren't recorded, and return.
  2. A test that sets up the sparse state twice in one process with a placed buffer in the first one. With real::init() twice, it should hang on main (a watchdog or a thread with a timeout turns that into a failure) and pass with the change.

Tested

  • RTX 5070 Ti, NVIDIA 610.74, Windows 11 Pro 26200; MSVC stable, debug.
  • The hang above: one run of the whole suite of xcl run: thread dumps unwind a copy of each stopped thread's stack #253 at 12328ac. The stack is from that run's own in-process dump, symbolised with the build's PDB.
  • On main 3516080, sparse.rs:549 still assigns under the guard, and forget_buffer still locks STATE.

Not tested

Searched before writing: no open issue or PR mentions forget_buffer, and no open PR head changes forget_buffer or the STATE assignment. #262 is a different deadlock (a cross-queue wait for CPU-write uploads).

AI use

  • Claude Code, Claude Opus 5.5: found the hang in a queued suite run, traced it through the run's thread dump, and wrote this issue.

🤖 Claude Opus 5.5

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions