Skip to content

D3D12.X: a queue the title releases is not kept alive by submit tracking - #1691

Open
b3IKK wants to merge 1 commit into
xcl-dev:mainfrom
b3IKK:d3d12x/released-queue-not-retained
Open

b3IKK wants to merge 1 commit into
xcl-dev:mainfrom
b3IKK:d3d12x/released-queue-not-retained

Conversation

@b3IKK

@b3IKK b3IKK commented Nov 24, 2026 •

Copy link
Copy Markdown
Contributor

Fixes #1668.

present::note_execute took a reference to every command queue a title submitted work to (so late_release can fence what it holds), and never dropped it. The queue's own last Release therefore only brought the count to 1, q_release never saw 0, and every queue a title ever submitted to — with the driver memory behind it — stayed allocated for the rest of the process.

That is the asymmetry in the issue: "create and release unused queues" passes 128/128 (no submit, so no reference is taken), while "create, submit, synchronize and release" fails on the 64th queue with E_OUTOFMEMORY.

This drops the reference when a release brings the count down to it (n == 1): the queue is forgotten by the submit tracking (and the present candidate that names it), the fence late_release keeps for that queue is dropped, and the reference goes. The presenter now also holds its own reference to the queue it makes a swap chain for, so that pointer cannot dangle once the title lets its queues go.

Since the review (@upipibi's race, validated by @DEViANCEVxXXxM on an RTX 3090): a tracked queue can now be freed, so the two readers of the list no longer take raw pointers out of it. late_release::drain_known and real::drain_gpu get their references from present::known_queues_held(), which takes them while the list is locked. A reader's reference either lands before the queue leaves the list — its release is then the one that sees n == 1 and lets the queue go — or the reader does not see the queue at all. The test no longer asserts the exact count the title's release returns (a reader in another test may hold a reference at that moment); it waits for the queue to leave the list.

What is in it

  • crates/xcl/src/hle/d3d12x/real.rs: in q_release, when the count reaches the one reference the submit tracking holds, hand the queue back (present::forget_queue) and drop it; drain_gpu signals queues it holds a reference to. GPU unit tests a_queue_the_title_releases_is_not_kept_alive_by_the_submit_tracking and a_tracked_queue_a_reader_holds_outlives_the_titles_release.
  • crates/xcl/src/hle/d3d12x/present.rs: forget_queue untracks a released queue and drops the present candidate naming it; known_queues_held returns the tracked queues with references taken under the lock; new_presenter holds its own reference to the queue and Presenter::drop releases it.
  • crates/xcl/src/hle/d3d12x/late_release.rs: forget_queue drops the fence kept for a queue that is gone (a sealed batch keeps its own clone); drain_known uses known_queues_held.

Tested

  • hardware (GPU + driver, CPU), Windows build, Rust version, debug / release: NVIDIA GeForce RTX 5080 (driver 32.0.16.1088), AMD Ryzen 9 9950X3D, Windows 11 10.0.26300, Rust 1.100.0 (stable-x86_64-pc-windows-msvc), DXC 1.9.2609.5, debug (scripts/test.ps1) and release (scripts/smoke.ps1)
  • .\scripts\test.ps1 (cargo build --workspace + cargo test --workspace -- --test-threads=4): 977 passed, 0 failed, 45 ignored (SKIPPED: the wave64 / direct-DXIL per-lane tests that need 64-lane hardware or a semantics mode this adapter refuses, no heap windows, and cannot disable the driver cache (Windows Developer Mode is required))
  • cargo test -p xcl --lib -- a_queue_the_title_releases a_tracked_queue_a_reader: 2 passed, 0 failed. With the n == 1 handoff disabled: 0 passed, 2 failed (a queue let go is not tracked any more, the reader's release lets the queue go).

Regression run

main (689f2db, clean tree, no local changes):

xcl-smoke v1
commit: 689f2db477c3c062cf162649781640ef8a977c18
gpu: NVIDIA GeForce RTX 5080
title: 9cc6a39e
seconds: 60
exit: 0x0
frames shown: 1643 (PresentX 1643)
presents per 2 s (last 3): 60 60 60
device removals: 0
crashes: 0
peak commit: 14617 MB

PR (81f6397, clean tree, no local changes):

xcl-smoke v1
commit: 81f639749f3528ba2b951d4c704cbcd5a7fe4618
gpu: NVIDIA GeForce RTX 5080
title: 9cc6a39e
seconds: 60
exit: 0x0
frames shown: 1641 (PresentX 1641)
presents per 2 s (last 3): 60 60 60
device removals: 0
crashes: 0
peak commit: 14482 MB

Not tested

  • the race itself: a freed queue does not reliably fault, so there is no test that fails on the previous head for it — the new test covers the ownership rule (a held queue outlives the title's release and goes with the reader's), not the interleaving
  • the reporter's synthetic loop (128 create/submit/sync/release) on this machine; @DEViANCEVxXXxM ran it on an RTX 3090 against the previous head and against the suggested patch this head follows
  • AMD GPUs and native wave64 hardware
  • other titles (Minecraft Preview, Unicorn Overlord, Fortnite, XDefiant) and Linux / Wine

AI use

  • OpenCode, Claude Fable 5.2: traced the queue lifetime from D3D12.X: E_OUTOFMEMORY after 63 submitted command queues #1668 through present/late_release/q_release, wrote the fix and the unit tests (and confirmed they fail with the handoff disabled), applied the reviewer's ownership change after checking the interleavings, rebased onto 689f2db, ran the test suite and both scripts/smoke.ps1 runs, and drafted this PR description.

@DEViANCEVxXXxM

Copy link
Copy Markdown

Thanks for the quick fix! I'm the original reporter of #1668.

I have an RTX 3090 available and can independently validate this PR using the original synthetic regression, concurrent stress, endurance, and GPU data integrity tests.

I'll post the results here for comparison.

Independent validation — PASS

Tested commit 1de417cca5163665c181d5a3557bff6df77c59e5 on my RTX 3090 (driver 621.01, Windows 26H1 build 28000.3501), with the D3D12 debug layer enabled.

Test | Result -- | -- Original unused queue regression | 128/128 Original submitted queue regression | 128/128 Concurrent stress repeatability | 10/10 runs Single-process endurance | 4096/4096 operations Concurrent GPU copy integrity | 1024/1024 copies Verified data values | 1,048,576/1,048,576 OOM, timeouts, reported D3D12 errors | None

The original failure from #1668 no longer reproduces. All 12 additional stress runs passed using unchanged synthetic test binaries.

One additional observation: during the endurance test, the device reference count was 112, 131, 142, and 161 at rounds 128, 256, 384, and 512 respectively. The final Live Producer refcount was 155. These snapshots were taken under concurrent load, so this is not conclusive evidence of a remaining leak, but may be worth investigating separately.

The original queue exhaustion regression is confirmed fixed in my test environment.

Thanks for the fix!

@upipibi

upipibi commented Nov 24, 2026

Copy link
Copy Markdown
Contributor

Tested on an RTX 3070 (driver 616.56, Windows 11) at 1de417c.

One race this PR opens. Before it, a queue in QUEUES was never freed, so known_queues() returning raw pointers was safe. Now q_release can free a queue right after it leaves the list, and both callers use the pointer after the lock is gone:

  • late_release::drain_known (runs on the late-release thread and on every note_execute): snapshot, then from_raw_borrowed(..).clone().
  • real::drain_gpu: snapshot, then Signal on the borrowed pointer without a reference of its own.

Interleaving: the reader snapshots → the title's last Release takes the count to 1 → forget_queue → the second f(this) frees the queue → the reader AddRefs freed memory. This is from reading the code, not observed. Two threads (one making, submitting to and releasing 3000 queues, one reading known_queues() like drain_known) didn't crash in 5 runs, but a freed queue doesn't reliably fault.

Suggested fix: take the references while the list is locked. forget_queue's return value then decides which thread does the final release, because a reader's reference either lands before the queue is removed (the title's second f(this) then returns 1 and the reader's drop frees it) or the reader doesn't see the queue at all.

diff --git a/crates/xcl/src/hle/d3d12x/late_release.rs b/crates/xcl/src/hle/d3d12x/late_release.rs
index c3fd9b3..eca1ca9 100644
--- a/crates/xcl/src/hle/d3d12x/late_release.rs
+++ b/crates/xcl/src/hle/d3d12x/late_release.rs
@@ -136,13 +136,7 @@ pub unsafe fn drain_known(seal_now: bool) {
     if HELD.lock().unwrap().as_ref().is_none_or(Held::is_empty) {
         return;
     }
-    let mut queues: Vec<ID3D12CommandQueue> = Vec::new();
-    for raw in super::present::known_queues() {
-        let raw = raw as *mut c_void;
-        if let Some(q) = ID3D12CommandQueue::from_raw_borrowed(&raw) {
-            queues.push(q.clone());
-        }
-    }
+    let mut queues: Vec<ID3D12CommandQueue> = super::present::known_queues_held();
     queues.extend(super::sparse::queue());
     queues.extend(super::title_textures::upload_queues());
     let gone = match HELD.lock().unwrap().as_mut() {
diff --git a/crates/xcl/src/hle/d3d12x/present.rs b/crates/xcl/src/hle/d3d12x/present.rs
index 81bf546..3138566 100644
--- a/crates/xcl/src/hle/d3d12x/present.rs
+++ b/crates/xcl/src/hle/d3d12x/present.rs
@@ -247,6 +247,19 @@ pub fn known_queues() -> Vec<usize> {
     QUEUES.lock().unwrap().clone()
 }
 
+/// The queues the title submitted work to, each with a reference of the caller's, taken while
+/// they are listed: since #1668 a queue the title lets go is released by `q_release`, so a raw
+/// pointer from [`known_queues`] may be freed before it is used.
+pub fn known_queues_held() -> Vec<ID3D12CommandQueue> {
+    let q = QUEUES.lock().unwrap();
+    q.iter()
+        .filter_map(|&raw| {
+            let raw = raw as *mut c_void;
+            unsafe { ID3D12CommandQueue::from_raw_borrowed(&raw) }.cloned()
+        })
+        .collect()
+}
+
 /// `queue` is down to the one reference `note_execute` took to keep it valid for `late_release`:
 /// the title is done with it. Forget it – and any use of it here – so the caller can let that
 /// reference go: otherwise every queue a title ever submitted to (and the driver memory behind
diff --git a/crates/xcl/src/hle/d3d12x/real.rs b/crates/xcl/src/hle/d3d12x/real.rs
index 9e894e6..60227d0 100644
--- a/crates/xcl/src/hle/d3d12x/real.rs
+++ b/crates/xcl/src/hle/d3d12x/real.rs
@@ -5434,7 +5434,6 @@ pub fn drain_gpu() {
         // a fence per queue (they finish in any order)
         let mut fences = Vec::new();
-        for q in super::present::known_queues() {
-            let qr = q as *mut c_void;
-            let Some(q) = ID3D12CommandQueue::from_raw_borrowed(&qr) else { continue };
+        for q in super::present::known_queues_held() {
+            let q = &q;
             let submit = || {
                 let f: ID3D12Fence = dev.CreateFence(0, D3D12_FENCE_FLAG_NONE)

With this on top (and #1523): hle::d3d12x:: 146/0, 3/3 runs, and the two-thread test passes.

A side effect on the new test: it asserts release(raw) == 0. While my reader thread ran in parallel, it got left: 2 once in 5 runs, because the reader held a reference at that moment. In a normal parallel suite, any test that calls note_execute (and so drain_known) can do the same. Checking !known_queues().contains(..) after the release, rather than the exact count, would avoid adding another #1487-style flake.

AI-assisted (Claude Code). No game files; synthetic tests only.

gustavsi added a commit to gustavsi/xcl that referenced this pull request Nov 24, 2026
@DEViANCEVxXXxM

Copy link
Copy Markdown

@upipibi I independently validated your exact suggested patch on top of 1de417c on my RTX 3090.

Synthetic regression and stress tests:

  • Original queue regression: 128/128 PASS.
  • Concurrent stress: 10/10 runs PASS.
  • Single-process endurance: 4096/4096 PASS.
  • GPU copy integrity: 1024/1024 copies, all 1,048,576 values verified.
  • No OOM, timeouts, or reported D3D12 errors in these workloads.

Regarding the refcount assertion:

I added an isolated test that explicitly retains an owned queue snapshot during the title's release. All five runs produced:

TITLE_RELEASE_COUNT=2
LISTED_DURING_READER=true
LISTED_AFTER_READER=false

This confirms your observation about the exact refcount assertion. One additional nuance: checking registry membership immediately after the title's release can also be unreliable while a snapshot remains alive. Checking membership after the temporary owner is dropped passed consistently.

Regarding #1523:

I tested all four combinations of the original PR, your ownership patch, and #1523 (1362129).

All eight serial suite runs passed. Four parallel runs also passed, but a subsequent parallel run with both patches encountered DXGI_ERROR_DEVICE_REMOVED / DXGI_ERROR_DEVICE_HUNG, accompanied by an NVIDIA driver event 153.

I stopped the remaining runs after the device loss. I did not reproduce the fake-fence access violation, and I cannot attribute the device loss to either patch.

Your ownership patch preserves the original queue exhaustion fix in my synthetic workloads. Thanks for the review!

@ivall ivall added the goal:world-load Bears on the retail title getting past world loading label Nov 24, 2026
@upipibi

upipibi commented Nov 24, 2026

Copy link
Copy Markdown
Contributor

Thanks, DEViANCEVxXXxM, for the runs and the membership nuance.

The device loss in the parallel run with both patches matches #1727. There, gpu_sparse_vram_descriptors_buffers_and_xmem re-initialises the backend while other tests use it, and parallel whole-lib runs lose the device (nvlddmkm 153). With #614 on top, 0 of 4 runs hung on an RTX 3070, against 3 of 4 on main (numbers). So the device loss is likely neither patch. If you re-run, adding #614 would check that.

@DavidM1911

Copy link
Copy Markdown
Contributor

Verification on RTX 3060 Ti (Windows 11), branch merged with current main:

  • cargo test -p xcl --lib: all passed, 0 failed, ~23 ignored.

Thanks!

@DEViANCEVxXXxM

Copy link
Copy Markdown

@upipibi Thanks for pointing me to #1727 and #614.

I ran additional validation on upstream c4d37be with the combined patches, using my RTX 3090 (driver 621.01) in release mode with the default translator.

D3D12.X subset | Passed | Failed | Ignored -- | -- | -- | -- Serial | 156 | 0 | 2 Parallel (4 threads), run 1 | 156 | 0 | 2 Parallel (4 threads), run 2 | 156 | 0 | 2

Both parallel runs completed with:

  • No device removal or hang.

  • No nvlddmkm event 153.

  • One sparse VRAM initialization per process.

  • The queue lifetime regression test passing.

One limitation: this subset does not include gpu_sparse_vram_descriptors_buffers_and_xmem. These results are consistent with improved stability, but do not independently establish #614 as the cause of the improvement.

Synthetic tests only. AI-assisted test planning and log analysis using ChatGPT (GPT-6.2).

present::note_execute AddRef'd every queue a title submitted work to and
never dropped that reference, so the queue's own Release never reached
zero, q_release never ran, and every queue (and the driver memory behind
it) lived on for the rest of the process. In the loop of xcl-dev#1668 - create,
submit, synchronise, release, 128 times - CreateCommandQueue failed with
E_OUTOFMEMORY on the 64th queue, while the same loop without a submit
(the path that takes no reference) passed all 128.

Drop the reference when a Release brings the count down to it (n == 1),
clear the present candidate that names the queue, and drop the fence
late_release keeps for it. The presenter now holds its own reference to
the queue it makes a swap chain for, so that pointer cannot dangle once
the title lets its queues go.

A tracked queue can now be freed, so its readers no longer take raw
pointers out of the list: late_release's drain and drain_gpu get their
references from known_queues_held(), which takes them while the list is
locked. A reader's reference either lands before the queue leaves the
list - its release is then the one that lets the queue go - or the
reader does not see the queue at all (review of xcl-dev#1691).

Assisted-by: OpenCode (Claude Fable 5.2)
@b3IKK
b3IKK force-pushed the d3d12x/released-queue-not-retained branch from 1de417c to 81f6397 Compare November 24, 2026 22:20
@b3IKK

b3IKK commented Nov 24, 2026

Copy link
Copy Markdown
Contributor Author

@upipibi you are right, and thanks @DEViANCEVxXXxM for running it. Before this PR nothing in QUEUES was ever freed, so the raw pointers known_queues() handed out were safe by accident; with the handoff in q_release they are not.

Pushed 81f6397 (rebased onto 689f2db), which takes your change as proposed:

  • present::known_queues_held() takes the references while the list is locked; late_release::drain_known and real::drain_gpu use it. known_queues() is left for membership checks only.
  • I went through the interleavings against the n == 1 rule. A reader's reference taken before the title's release: the title's release returns 2, the reader's drop goes through the hooked Release, sees 1, forgets the queue and frees it. A reader that gets in between the title's f(this) and forget_queue: the title's second f(this) returns 1 and the reader's drop frees it. In both the queue is removed from the list exactly once, by whichever release sees the count at the tracking's one reference.
  • The test no longer asserts release(raw) == 0. It waits (up to 5 s) for the queue to leave the list, which also covers the nuance about a snapshot still alive right after the release. A second test, a_tracked_queue_a_reader_holds_outlives_the_titles_release, holds a snapshot across the title's release: the release returns at least 2, the queue stays listed, and it goes when the snapshot is dropped.

Both tests fail with the handoff disabled (0 passed, 2 failed) and pass with it. Full suite on this head: 977 passed, 0 failed, 45 ignored; smoke blocks for 689f2db and 81f6397 are in the description (1643 and 1641 frames, 0 crashes, 0 device removals).

What this does not have is a test that fails on the previous head for the race itself: as you found, a freed queue does not reliably fault. That is stated under Not tested.

The device refcount growing during the endurance run (112 → 161) is not addressed here. I have not looked into it.

AI use: OpenCode, Claude Fable 5.2 made the change, ran the tests and wrote this comment.

@GustavoEllwanger

Copy link
Copy Markdown

Tested exact head 81f639749f3528ba2b951d4c704cbcd5a7fe4618 on AMD RDNA3 (RX 7600).

Tested

  • cargo test -p xcl --lib a_queue_the_title_releases_is_not_kept_alive_by_the_submit_tracking -- --nocapture: 1 passed, 0 failed.
  • cargo test -p xcl --lib a_tracked_queue_a_reader_holds_outlives_the_titles_release -- --nocapture: 1 passed, 0 failed.
  • XCL_HEAP_WINDOWS=1 cargo test -p xcl --test gpu_compute_e2e heap_window_out_of_memory_falls_back_to_the_switch_form -- --nocapture: 1 passed, 0 failed (RX 7600).
  • cargo test --workspace --no-fail-fast -- --test-threads=4 --skip fuzz_validate_and_gpu_1: 976 passed, 0 failed, 45 ignored, 1 filtered out, 0 wave64/mesh capability skips.
    • 8 SKIPPED lines reported in the default run (not counted as coverage): 6 per-lane #1426 refusals (f16_programs_on_the_gpu / f32_programs_on_the_gpu), 1 pso_measurement_compiles_an_exact_synthetic_record (Windows Developer Mode not enabled, 0x887A002D), 1 heap_window_out_of_memory_falls_back_to_the_switch_form (no heap windows, separately verified above with XCL_HEAP_WINDOWS=1).
  • Unfiltered .\scripts\test.ps1 hit only the pre-existing AMD amdxc64 failure in gpu::dxil::fuzz_tests::fuzz_validate_and_gpu_1 (seed 722: res[30] = 0x1, CPU 0x7fffffff, documented in 210f773). With XCL_ADAPTER=warp, fuzz_validate_and_gpu_1 validated all 500/500 modules (500..1000, GPU-compared 0).
  • XCL_SAMPLE_* variables were unset.
  • Machine: Windows 11 Home build 26200; AMD Ryzen 5 5600T; AMD Radeon RX 7600 (8133 MiB VRAM, RDNA3, Adrenalin 26.11.2 / driver 32.0.32029.2010); Rust 1.100.0 MSVC x64; DXC/DXIL 1.8.2502.11 (Windows SDK 10.0.26100.0); debug profile.

Not tested

  • Retail-title smoke or world-load run, release builds, or XCL_SAMPLE_* game-data tests.

AI use
Antigravity (Gemini 4 Neon) ran the commands and drafted this report; only the measured results above are posted.

@luckyteagirl

Copy link
Copy Markdown

Tested

  • Exact PR head 81f639749f3528ba2b951d4c704cbcd5a7fe4618 on Windows 11 build 26200, Ryzen 9 5980HX, AMD Radeon RX 6800M driver 30.0.13002.13003 plus an AMD integrated GPU, Rust 1.99.0 MSVC, DXC/DXIL 1.9.2609.5. XCL_ADAPTER=6800M, XCL_REQUIRE_GPU=1, and XCL_DIRECT_DXIL=0 were set for the test commands.
  • a_queue_the_title_releases_is_not_kept_alive_by_the_submit_tracking and a_tracked_queue_a_reader_holds_outlives_the_titles_release each passed as a focused, single-threaded test: 1 passed, 0 failed, 0 ignored per run, with no explicit skip. These tests call D3D12CreateDevice(None) at feature level 12.0 and have no WARP fallback, so they exercised an AMD hardware adapter on this machine. They do not use XCL_ADAPTER to select that device, and I did not capture its LUID to distinguish the RX 6800M from the integrated AMD GPU.
  • cargo test --locked -p xcl --lib -j 2 hle::d3d12x:: -- --test-threads=1 --show-output: 157 passed, 0 failed, 2 ignored. One explicit SKIPPED line reported that the adapter makes no reserved cube with mips. Both queue regressions passed in this broader run too.
  • cargo build --locked --workspace -j 2: passed.

Not tested

  • The original 128-queue submit/synchronize/release endurance reproduction from D3D12.X: E_OUTOFMEMORY after 63 submitted command queues #1668, the full workspace suite, a retail-title smoke pair, the exact LUID of the tests' default D3D12 adapter, or other drivers and GPUs. The focused tests check tracking and reference ownership for one submitted queue each.

AI use

OpenAI Codex (GPT-6, exact variant not exposed in this session) inspected the test and queue-lifetime paths, ran the AMD-machine tests and build, reviewed the logs, and drafted this report.

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

Labels

goal:world-load Bears on the retail title getting past world loading

Projects

None yet

Development

Successfully merging this pull request may close these issues.

D3D12.X: E_OUTOFMEMORY after 63 submitted command queues

7 participants