Skip to content

D3D12.X: drop alloc_pool fences on queue release and skip real::FENCES - #1945

Open
Decodere wants to merge 1 commit into
xcl-dev:mainfrom
Decodere:fix-1909-alloc-pool-fence-leak
Open

Decodere wants to merge 1 commit into
xcl-dev:mainfrom
Decodere:fix-1909-alloc-pool-fence-leak

Conversation

@Decodere

@Decodere Decodere commented Nov 24, 2026 •

Copy link
Copy Markdown
Contributor

Stops allocator-pool internal fences from leaking with Refcount: 2 across released command queues.

Two compounding paths kept every fence created in alloc_pool::executed alive after its command queue hit final Release:

  1. Calling d.CreateFence on the adopted title device went through real::dev_create_fence, which permanently AddRefd the fence into real::FENCES (alloc_pool only signals and polls these internal fences and never waits on them, so end_title does not need them).
  2. alloc_pool::State.fences (and State.last_submit, added in D3D12.X: a list reset while its last submit runs records on a spare #2163) kept the per-queue ID3D12Fence after queue release because real::q_release never notified alloc_pool when a queue's refcount reached zero.

Fixes #1909

What is in it

  • crates/xcl/src/hle/d3d12x/real.rs: add create_pc_fence calling the native CreateFence slot via orig_fn on adopted devices (falling back to dev.CreateFence on unhooked test devices), notify alloc_pool::forget_queue in q_release when n == 0, and expose is_fence as pub(super) for testing.
  • crates/xcl/src/hle/d3d12x/alloc_pool.rs: use real::create_pc_fence in executed, add forget_queue to remove State.fences, prune completed Backing.marks (moving still-pending GPU marks to Backing.retired so queue-address reuse cannot overwrite in-flight work before completion), and prune completed State.last_submit entries for the released queue (clearing last.queue = 0 on still-in-flight entries so queue-address reuse cannot report same_queue in note_resubmits, and reaping orphaned completed entries on subsequent releases). Clear retired on backing reuse in title_reset_in, drop removed/replaced fences outside STATE.lock() in forget_list, forget_queue, and executed, clean up the unadopted list and queue in a_recording_outlives_the_reset_of_the_titles_allocator via forget_list/forget_queue before dropping them so recycled addresses do not inherit stale state across tests, and add CPU (AtomicU64/AtomicU32 fake COM fence) and GPU regression tests (forget_queue_prunes_completed_and_preserves_pending_across_address_reuse and released_queue_frees_alloc_pool_fence_and_skips_title_fences).

Tested

  • NVIDIA GeForce RTX 4060 8 GB (driver 621.14), Intel Core i5-11400F, 32 GB RAM, Windows 11 Home 26200, Rust 1.100.0 (stable-x86_64-pc-windows-msvc), DXC 1.9.2609.5; release build.
  • Pre-fix reproduction: confirmed released_queue_frees_alloc_pool_fence_and_skips_title_fences fails independently without create_pc_fence (fence registered in real::FENCES) and without q_release → alloc_pool::forget_queue (State.fences and State.last_submit retain released queue's fence), and reproduced the deterministic left: 4, right: 1 test-order failure under --test-threads=1 before adding forget_queue cleanup to a_recording_outlives_the_reset_of_the_titles_allocator.
  • cargo test --release -p xcl --lib hle::d3d12x::alloc_pool::tests:
    • -- --test-threads=1 --nocapture (3 consecutive runs, RTX 4060): 6 passed, 0 failed, 0 ignored each run.
    • -- --test-threads=4 --nocapture (RTX 4060): 6 passed, 0 failed, 0 ignored.
    • XCL_ADAPTER="Microsoft Basic Render Driver" -- --test-threads=1 --nocapture (WARP): 6 passed, 0 failed, 0 ignored.
  • cargo build --release --workspace + cargo test --release --workspace --no-fail-fast -- --test-threads=4 --nocapture: 1243 passed, 2 pre-existing failures on parent 96331383 (fixed on main immediately after in Tests: two expectations that no longer match the code #2245: gpu::recomp::pso::shader_cache_tests::rerouted_disk_entries_follow_the_direct_translator and hle::d3d12x::device_x::tests::pc_resource_desc_strips_uav_on_unsupported_formats_and_msaa), 46 ignored across 23 test binaries.
  • SKIPPED lines on stderr (27 total):
    • 20 no wave64 (gpu::recomp::lower::tests (4), gpu::recomp::lower_dxil::tests (14), gpu::recomp::tests (2))
    • 3 wave32 per-lane #1426 refusals (gpu::recomp::lower_dxil::tests: f16_programs_on_the_gpu (2), f32_programs_on_the_gpu (1))
    • 1 shaders::tests::pso_measurement_compiles_an_exact_synthetic_record (cannot disable driver cache without Windows Developer Mode)
    • 1 heap_window_out_of_memory_falls_back_to_the_switch_form (no heap windows)
    • 2 D3D12 debug-layer tests (test_title_draws_with_a_root_signature_under_the_debug_layer, uploads_start_from_each_subresources_state: no d3d12SDKLayers.dll)

Regression run

main:

xcl-smoke v1
commit: 963313835813f475037b3a9f5fcc3cb591696928
gpu: NVIDIA GeForce RTX 4060
title: 29640639
seconds: 60
exit: 0x0
frames shown: 1526 (PresentX 1526)
presents per 2 s (last 3): 60 60 60
device removals: 0
crashes: 0
peak commit: 14482 MB

this PR:

xcl-smoke v1
commit: ace754ce635c06a03068601c12afcdc1ff60770c
gpu: NVIDIA GeForce RTX 4060
title: 29640639
seconds: 60
exit: 0x0
frames shown: 1530 (PresentX 1530)
presents per 2 s (last 3): 60 60 60
device removals: 0
crashes: 0
peak commit: 14468 MB

Not tested

  • AMD and Intel GPUs, or wave64-capable hardware (this pass ran on RTX 4060 and WARP only).
  • Running with the D3D12 Debug Layer live-object reporter (d3d12SDKLayers.dll is not installed on this machine), debug-profile full workspace test run, or stacked on top of open #1691's queue-retention fix across multi-cycle synthetic copy workloads; the smoke run above is a regression check only.

AI use

@DEViANCEVxXXxM

Copy link
Copy Markdown

Independent validation — PASS

Tested exact commit 907cf34cc587eb74c763ce87de4911c55037cbf1 on my RTX 3090, driver 621.01, Windows 26H1.

  • All five allocator-pool tests passed.
  • Both new regression tests passed.
  • No skipped GPU tests.
  • No reported device errors.
  • No local source modifications.

I also checked three-way merge compatibility with #1691 (81f6397). The only reported content conflict is in real.rs, inside q_release().

When integrating both PRs, allocator-pool cleanup needs to be preserved on both final-release paths introduced by #1691.

I have a prepared three-cycle synthetic workload with D3D12 live-object reporting that can validate the combined fix once this integration is resolved.

Thanks for addressing #1909!

AI assistance: ChatGPT (GPT-6.2). Synthetic tests only.

@futurepro62

futurepro62 commented Nov 24, 2026 •

Copy link
Copy Markdown

Full-suite run, since this PR has no test report yet. Tested exact head 907cf34 (on its base 20cf130).

Tested

  • NVIDIA GeForce RTX 5070 Ti 16 GB (Blackwell, 32-lane waves only, mesh shaders), driver 621.40, Ryzen 7 9800X3D, Windows 11 Home 26300, Rust 1.100.0 MSVC, DXC + dxil.dll 1.9.2602.17 (Windows SDK 10.0.26100), debug, D3D12 debug layer installed.
  • cargo build --workspace, then XCL_REQUIRE_GPU=mesh cargo test --workspace -- --test-threads=4 --skip heap_window_out_of_memory_falls_back_to_the_switch_form --skip gpu::recomp::lower_dxil::tests::f16_programs_on_the_gpu --skip gpu::recomp::lower_dxil::tests::f32_programs_on_the_gpu --skip gpu::recomp::lower_dxil::tests::integer_programs_on_the_gpu --skip gpu::recomp::lower_dxil::tests::f64_programs_on_the_gpu --skip gpu::dxil::fuzz_tests: 968 passed, 0 failed, 43 ignored. SKIPPED (no wave64): a_2d_or_3d_group_agrees_with_the_hlsl_path_on_the_gpu, a_per_lane_2d_or_3d_group_runs_on_the_gpu, coalesced_guarded_runs_agree_with_the_hlsl_path_on_the_gpu, cross_lane_reads_agree_with_the_hlsl_path_on_the_gpu, gpu_geometry_wave32_prefix_count_over_a_mask, gpu_wave32_program_in_wave64_hardware, the_wave64_lowering_agrees_with_the_hlsl_path_on_the_gpu.
  • The new tests ran and passed: forget_queue_prunes_completed_and_preserves_pending_across_address_reuse, released_queue_frees_alloc_pool_fence_and_skips_title_fences.
  • Same head on the AMD Radeon integrated GPU (iGPU) of the Ryzen 7 9800X3D (RDNA2, XCL_ADAPTER=radeon), cargo test --workspace -- --test-threads=4 --skip gpu::dxil::fuzz_tests::fuzz_validate_and_gpu_1: 977 passed, 0 failed, 45 ignored. SKIPPED: f16_programs_on_the_gpu (f16 arithmetic, wave32, per-lane form: refused (unsupported V_MUL_F16 (its gpu: direct RDNA2 -> DXIL backend (no HLSL / DXC), behind XCL_DIRECT_DXIL #1426 semantics differ from the console's))), f16_programs_on_the_gpu (f16 arithmetic, wave64, per-lane form: refused (unsupported V_MUL_F16 (its gpu: direct RDNA2 -> DXIL backend (no HLSL / DXC), behind XCL_DIRECT_DXIL #1426 semantics differ from the console's))), f16_programs_on_the_gpu (f16 packed, wave32, per-lane form: refused (unsupported V_PK_MUL_F16: VOP3 modifiers (its gpu: direct RDNA2 -> DXIL backend (no HLSL / DXC), behind XCL_DIRECT_DXIL #1426 semantics differ from the console's))), f16_programs_on_the_gpu (f16 packed, wave64, per-lane form: refused (unsupported V_PK_MUL_F16: VOP3 modifiers (its gpu: direct RDNA2 -> DXIL backend (no HLSL / DXC), behind XCL_DIRECT_DXIL #1426 semantics differ from the console's))), f32_programs_on_the_gpu (f32 conversions, wave32, per-lane form: refused (unsupported V_CVT_F16_F32 (its gpu: direct RDNA2 -> DXIL backend (no HLSL / DXC), behind XCL_DIRECT_DXIL #1426 semantics differ from the console's))), f32_programs_on_the_gpu (f32 conversions, wave64, per-lane form: refused (unsupported V_CVT_F16_F32 (its gpu: direct RDNA2 -> DXIL backend (no HLSL / DXC), behind XCL_DIRECT_DXIL #1426 semantics differ from the console's))), heap_window_out_of_memory_falls_back_to_the_switch_form (no heap windows).
  • --skip leaves out tests that fail on main 62265d3 too, not because of this PR. On NVIDIA: heap_window_out_of_memory_falls_back_to_the_switch_form (Tests: heap_window_out_of_memory_falls_back_to_the_switch_form fails under XCL_REQUIRE_GPU=mesh (opt-in skip counted as missing coverage) #1651) and the four gpu::recomp::lower_dxil::tests::*_programs_on_the_gpu tests (follow-up on #1651) fail because runner::skip_test turns their skips into failures under XCL_REQUIRE_GPU=mesh, and gpu::dxil::fuzz_tests seeds 70, 545, 1019 and 1507 fail CreateComputePipelineState with E_OUTOFMEMORY on this card (drivers 616.64 and 621.40). On AMD: fuzz_validate_and_gpu_1 fails on seed 540 (res[833] 0x7 vs CPU 0x6). The AMD run does not set XCL_REQUIRE_GPU.

Not tested

  • a release build, a game (test run only).

AI use

  • Claude Code (Claude Opus 5.5) ran the tests and wrote this report. I checked it before posting.

@upipibi

upipibi commented Nov 24, 2026

Copy link
Copy Markdown
Contributor

Heads-up from reading the diff (not run): alloc_pool::forget_queue goes in the if n == 0 branch of q_release. #1819 and #1899 put their hooks on that same line, so the four PRs conflict there (git merge-tree of this head 907cf34 with each of #1691, #1819 and #1899 reports a conflict).

The other point is when that branch runs. On main, present keeps a reference to every queue the title submitted to (#1668), so a queue the title submitted to rarely gets to n == 0, and forget_queue would rarely run until #1691 is in. #1691 adds a second path, n == 1 && present::forget_queue(this), which releases again and calls list_x::on_queue_release when the count reaches 0. A hook only in the first branch misses that path. Details are in #1899 (comment). A single per-queue cleanup function, called from both places where n reaches 0, would let these PRs merge in any order.

AI-assisted (Claude Code).

@raylitebull

Copy link
Copy Markdown
Member

Code review on 907cf34:

  • forget_queue + Backing.retired + create_pc_fence (crates/xcl/src/hle/d3d12x/alloc_pool.rs, real.rs) cleanly handles queue address reuse while marks are still in flight and keeps internal tracking fences out of real::FENCES, with strong unit and GPU test coverage (forget_queue_prunes_completed_and_preserves_pending_across_address_reuse, released_queue_frees_alloc_pool_fence_and_skips_title_fences).
  • Diff audited and CI workflow approved (running). Note #1967 touches the same q_release hook.

Remaining before merge: Build and test finishing green plus AMD verification on 907cf34.

@raylitebull

Copy link
Copy Markdown
Member

CI update on 907cf34:

  • Build and test (Windows, no GPU) failed in hle::d3d12x::alloc_pool::tests::released_queue_frees_alloc_pool_fence_and_skips_title_fences at crates/xcl/src/hle/d3d12x/alloc_pool.rs:646 (left: 4, right: 1 on the no-GPU/WARP runner — likely WARP retaining internal fence references across queue/command work before device destruction).
  • Also note @upipibi's point above about q_release's present::forget_queue(this) branch (n == 1) if #1691 lands alongside this.

@Decodere
Decodere force-pushed the fix-1909-alloc-pool-fence-leak branch from 907cf34 to f581057 Compare November 25, 2026 02:13
@Decodere

Copy link
Copy Markdown
Contributor Author

@raylitebull Found and fixed the exact cause of the Build and test failure (alloc_pool.rs:646: left: 4, right: 1) and rebased cleanly onto main (3da2abb8) at f5810578:

  • Root cause (signaled == 4 vs 1): Under --test-threads=1 (as on CI), a_recording_outlives_the_reset_of_the_titles_allocator ran earlier without real::adopt(dev), called executed(queue, ...) 3 times, and dropped queue without going through q_release, leaving (q_key -> (fence, 3)) in STATE.fences. When released_queue_frees_alloc_pool_fence_and_skips_title_fences then created a queue at the recycled pointer q_key, executed incremented 3 -> 4 (rather than WARP retaining a fence ref).
  • Fix: Cleaned up forget_queue(queue.as_raw() as usize); before drop(queue); in a_recording_outlives_the_reset_of_the_titles_allocator and added defensive forget_queue(q_key) / forget_queue(q2_key) guards in released_queue_frees_alloc_pool_fence_and_skips_title_fences; runtime code in alloc_pool.rs and real.rs is unchanged from 907cf34.
  • Re-verified (release): Reproduced left: 4, right: 1 deterministically under --test-threads=1; after the fix, hle::d3d12x::alloc_pool::tests passed 5/5 x3 under --test-threads=1 (RTX 4060), 5/5 parallel (RTX 4060), and 5/5 under --test-threads=1 on WARP (XCL_ADAPTER="Microsoft Basic Render Driver"), plus full workspace 1,113 passed, 0 failed, 46 ignored and paired 60 s scripts/smoke.ps1 (1,582 -> 1,547 frames, 0 crashes/removals).
  • #1691 / #1967 note: Checked live main (#1691 is still open so present::forget_queue(this) is not in q_release yet); if #1691 merges first, we will rebase immediately so both paths stay intact on final queue release. (Note futurepro62 also verified the identical runtime fix on AMD Radeon iGPU in #issuecomment-7147975903.)

Ready for CI re-run!

AI use: Google Antigravity (Gemini 4 Neon).

@DEViANCEVxXXxM

Copy link
Copy Markdown

Independent validation of f5810578ae1da2a34808ea6c75373cc9e5b36050 on RTX 3090 / Windows.

  • Release build: successful.
  • Individual allocator-pool tests in separate processes: 5/5 passed.
  • Three runs with --test-threads=1: 15/15 passed.
  • Three runs with --test-threads=5: 15/15 passed.
  • No detected device errors, timeouts, or skipped tests.

Total: 35/35 passed.

The exact upstream revision was tested without source modifications.

The updated test cleanup appears stable in these runs. However, passing repeated tests does not establish the absence of data races or resolve the separate Cell<u64> synchronization concern.

Testing and report analysis assisted by ChatGPT (GPT-6.2).

@DavidM1911

Copy link
Copy Markdown
Contributor

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

Thanks!

@DEViANCEVxXXxM

Copy link
Copy Markdown

Follow-up validation for #1909.

I tested the updated PR revision f581057, both independently and combined with #1691 (81f6397), against main cbc296ab.

Results:

  • All five allocator-pool tests passed.
  • The combined D3D12.X regression suite passed: 181 passed, 0 failed, 2 expected timing tests ignored.
  • 210/210 focused stress test executions passed.
  • 96 completed-fence lifecycle cycles passed.
  • 96 simulated pending-fence lifecycle cycles passed.

The additional cross-PR tests exercised submit tracking and allocator-pool cleanup on the same command queue, with and without an outstanding reader reference.

Verified behavior includes:

  • Internal allocator fences are not registered in real::FENCES.
  • Completed marks are removed on final queue release.
  • Pending marks survive queue release in Backing.retired.
  • Pending marks prevent premature allocator reuse.
  • Retired marks are reclaimed after completion.
  • The internal fence reaches COM reference count zero after cleanup.

Pending completion was modeled using a native D3D12 fence with CPU signaling, rather than deliberately blocking outstanding GPU work.

Integration note: #1691 and #1945 conflict in real.rs::q_release. Moving the final-zero cleanup after #1691's tracking-reference removal preserves both fixes. The resulting combined tree (81f0dbb) passed all the above checks.

The retail-title crash remains present on main, both individual PRs, and the combined build. This should be investigated separately and does not invalidate the allocator lifecycle test results.

AI assistance: ChatGPT (GPT-6.2). All reported builds and tests were executed on actual Windows hardware.

@upipibi

upipibi commented Nov 25, 2026

Copy link
Copy Markdown
Contributor

Test report at the current head f581057: RTX 3070 (Ampere, driver 616.56), Windows 11, debug.

  • cargo test -p xcl --lib -- hle::d3d12x --test-threads=4, 3 runs: 166 passed, 0 failed each time. main 6ea813a with the same command, alternated with these runs: 177 passed, 0 failed each time.
  • One earlier full cargo test -p xcl --lib -- --test-threads=4 run ended in a device hang (0x887a0006) right after a Wait64BitValueX was bridged to a GPU queue wait. That is the fence area this PR changes, but it didn't come back in the 3 targeted runs above, so I can't attribute it to the PR.
  • XCL_DIRECT_DXIL=1 ... gpu_: 125 passed, 2 failed. Both come from the PR's older base: gpu_fmt_encode_int_sat_and_f16_rtz is fixed on main by Recompiler: saturate UINT/SINT and use RTZ in direct DXIL fmt_encode #2043, and gpu_image_get_resinfo_1d_array also fails on main (Direct DXIL: image_get_resinfo of a 1D array gives the layers in y #2046).

AI-assisted (Claude Code). Synthetic tests only.

Internal per-queue fences created in alloc_pool::executed leaked with
Refcount: 2 across released command queues for two compounding reasons:
1. Calling ID3D12Device::CreateFence on the adopted title device went
   through real::dev_create_fence, which permanently AddRef'd the fence
   into real::FENCES (alloc_pool never waits on these fences, so
   end_title does not need them).
2. alloc_pool::State.fences (and State.last_submit) retained each
   queue's ID3D12Fence forever because real::q_release never notified
   alloc_pool on final release.

Bypass dev_create_fence with real::create_pc_fence (invoking the
original native CreateFence on adopted devices), add
alloc_pool::forget_queue called from real::q_release when n == 0 to drop
State.fences, completed Backing.marks, and completed State.last_submit
entries outside STATE while moving still-pending GPU marks to
Backing.retired and clearing last_submit.queue on in-flight entries so
queue-address reuse cannot clobber or spoof them, and cover both leak
paths with CPU and GPU regression tests.

Fixes xcl-dev#1909
Assisted-by: Google Antigravity (Gemini 4 Neon)
@Decodere

Copy link
Copy Markdown
Contributor Author

Rebased onto 96331383 (clean against main 426a5a74) -> ace754ce:

  • Resolved the alloc_pool.rs conflict with D3D12.X: a list reset while its last submit runs records on a spare #2163 (State.last_submit / note_resubmits): forget_queue now also prunes completed State.last_submit entries for the released queue (and completed orphaned entries), zeroes last.queue = 0 on still-in-flight entries so queue-address reuse cannot report same_queue, and drops all removed/replaced COM fence handles outside STATE.lock().
  • Re-verified alloc_pool::tests (6/6 x3 --test-threads=1, 6/6 --test-threads=4, 6/6 WARP) and paired 60 s RTX 4060 smoke (1526 -> 1530 frames, 0 removals/crashes, 14482 -> 14468 MB).

AI use: rebased, tested, and drafted with Google Antigravity (Gemini 4 Neon).

@thehollowedone

Copy link
Copy Markdown

Independent Windows run on ace754c. Windows 11 build 26300, Ryzen 7 9800X3D, RTX 3080 Ti (621.35), Rust 1.99.1 MSVC, debug.

cargo build --offline --locked --workspace passed on the exact revision. cargo test --offline --locked --workspace --no-fail-fast -- --test-threads=4 --nocapture reported 1,243 passed, 2 failed, 46 ignored. Both failures were the stale assertions fixed by #2245. With this revision merged locally into 426a5a7, the same commands reported 1,245 passed, 0 failed, 46 ignored. Each run had 25 runtime skips included in Cargo's pass count.

I reran the simulated queue-key reuse check with native D3D12 fences. Releasing the old key left its old list marked in flight and cleared the queue identity from that submission. Finishing the newer native submission kept both the older list in flight and the backing busy. Signaling the old fence cleared both conditions, and the next queue cleanup removed the old submission record. No live work was blocked and no allocator was reset.

On the exact revision, cargo test --offline --locked -p xcl --lib released_queue_key_reuse_keeps_the_older_pending_fence -- --test-threads=1 --nocapture passed 1/0/0 with zero skips. Earlier side-by-side check on #1967.

Synthetic coverage only. AI-assisted. Codex (GPT-6, exact variant unavailable) extended the fixture, ran the tests, 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: allocator pool retains internal fences after command queue release

8 participants