Skip to content

Tests: stop leaking imported heap resource references - #1520

Open
wileyGray wants to merge 1 commit into
xcl-dev:mainfrom
wileyGray:codex/test-heap-lifetime
Open

wileyGray wants to merge 1 commit into
xcl-dev:mainfrom
wileyGray:codex/test-heap-lifetime

Conversation

@wileyGray

@wileyGray wileyGray commented Nov 24, 2026 •

Copy link
Copy Markdown

Part of #1487. Four transition barriers in the imported-heap GPU test cloned a resource into ManuallyDrop and never released it. The test then freed the backing allocation with four resource references outstanding. This fixes that specific lifetime bug, also identified in the #175 investigation; it does not establish the cause of every intermittent access violation in #1487.

What is in it

  • crates/xcl/src/hle/d3d12x/list_x.rs: borrow the resource in the four test barriers while its owner remains alive through the existing fence waits, check VirtualFree, and add a CPU-only reference-count regression. All 75 changed lines are inside #[cfg(test)] mod tests.

Tested

  • Clean head 6159773d93e826b4c328ad7174334c7a12c75541, based on main b7aa9f3; Windows 11 Home build 26300, i9-14900K, RTX 4080 SUPER, driver 32.0.16.2114, Rust 1.93.0, DXC 1.9.2609.5, debug, XCL_ADAPTER=NVIDIA. All XCL_SAMPLE_* unset.
  • scripts/test.ps1 (workspace build and tests with four threads): 771 passed, 0 failed, 41 ignored. Includes four GPU/debug skips (two native-wave64 tests, two missing-debug-layer tests) and 33 sample-gated early returns; these are not executed coverage. Synthetic console-title e2e passed.
  • The new regression failed with the old clone behavior: reference counts (5, 4) after four barriers / dropping the owner, versus expected (1, 0). With the fix: 1 passed, 0 failed. Reproduce: cargo test -p xcl --lib imported_heap_test_barriers_do_not_retain_the_resource -- --nocapture.
  • The existing imported-heap GPU test passed individually. Ten processes each ran it alongside real::readback_routing_tests::coherent_buffer_sanitizes_console_flags_and_state with two threads: 20 passed, 0 failed, 0 skipped, no access violations or stalls (0.27-0.36 seconds per pair).
  • Independent diff review and git diff --check passed.
Paired-test reproduction and exact GPU/debug skips

Build with cargo test -p xcl --lib --no-run. Run the emitted library test executable ten times, one process at a time, with these arguments (each run must report exactly two passes and no skips):

--test-threads=2 --nocapture --exact
hle::d3d12x::list_x::tests::write_buffer_immediate_on_open_existing_heap_visible_to_cpu
hle::d3d12x::real::readback_routing_tests::coherent_buffer_sanitizes_console_flags_and_state

Full-suite GPU/debug skips: gpu_geometry_wave32_prefix_count_over_a_mask and gpu_wave32_program_in_wave64_hardware (no native wave64); uploads_start_from_each_subresources_state and test_title_draws_with_a_root_signature_under_the_debug_layer (debug layer unavailable).

Regression run

  • No retail smoke run. This diff only changes the inline test module; please review and apply the maintainer-controlled test-only label under CONTRIBUTING.md. The path-based description check otherwise requests retail smoke blocks. No runtime behavior changes.

Not tested

  • AMD, Intel, Wine/RADV, release, retail titles, long stress runs, or the reporter's hardware/driver. Ten repetitions do not prove the broader intermittent issue resolved. No proprietary game data or issue attachments were used.

AI use

  • Codex (GPT-6; exact variant unavailable) inspected the issue and code, implemented and reviewed the test-only fix, ran validation, and wrote this report under the user's direction.

Four transition barriers retained clones inside ManuallyDrop, so the test freed its backing allocation while resource references remained. Borrow the resource through the existing fence waits and add a CPU-only reference-count regression.

Part of xcl-dev#1487; the specific leak was also reported in xcl-dev#175. This does not establish the cause of every parallel access violation.

Assisted-by: Codex (GPT-6; exact variant unavailable)

@GonZiin GonZiin left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Independent full-suite run on AMD RDNA4. Both the reference-count regression and the imported-heap GPU test passed. I checked the diff and all changed Rust lines are inside the test module.

Tested

  • Head 6159773d93e826b4c328ad7174334c7a12c75541, clean checkout. Windows 11 Pro build 26300, Ryzen 7 7800X3D, Radeon RX 9070 XT (driver 32.0.32029.2010), Rust 1.100.0 MSVC, DXC 1.8.2502.11, debug.
  • .\scripts\test.ps1 exited 0. Workspace totals were 771 passed, 0 failed, 41 ignored and 0 reported GPU skips. The bundled title end-to-end test passed.
  • The PR description job is asking for retail smoke blocks because the tests live inside list_x.rs. A maintainer can apply the documented test-only label after checking this revision.

Not tested

Retail smoke and the opt-in sample data tests.

AI use

Codex (gpt-6.2-sol) inspected the diff, ran the Windows tests, and wrote this review.

@Bullet18x

Copy link
Copy Markdown

Test report for head 6159773, with one finding about the new VirtualFree assert.

Tested

Cause (same as #1590)
D3D12CreateDevice returns the process's one device. Once another test has adopted it, buf is adopted too, and its last Release goes to late_release::hold. While another test's queue still has work, the resource is held and the imported heap keeps mem pinned. So the first VirtualFree fails with 87, even with the leak fixed.

Check
On the combined branch, I let this test retry VirtualFree for up to 10 s, the same as #1590 does for coherent_buffer_sanitizes_console_flags_and_state, and ran it 20 more times at 32 threads. The test failed 0 times. The first VirtualFree failed in 4 runs, and the retry freed the memory each time, after 5 to 27 ms.

Suggestion
Keep the assert, but wait for the held release first:

let start = std::time::Instant::now();
let mut freed = VirtualFree(mem, 0, MEM_RELEASE);
while freed == 0 && start.elapsed() < std::time::Duration::from_secs(10) {
    std::thread::sleep(std::time::Duration::from_millis(5));
    freed = VirtualFree(mem, 0, MEM_RELEASE);
}
assert_ne!(freed, 0, "the heap's pages were not let go within 10 s");

Not tested

  • AMD and Intel, the release build, Wine.

AI use: Claude Code with Claude Opus 5.5 ran the tests, found the cause and wrote this report. I checked each point.

@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, ~21 ignored. (Intermittent single-test flake / 0xc0000005 abort under parallel scheduling; clean on single-threaded rerun — same flake seen on unmodified main.)

Thanks!

@crastinator crastinator left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tested on RDNA2 under Wine (RADV). This also fixes the parallel device removal I traced in #1246: the VirtualFree of this test's imported memory while another test's GPU work ran on the shared device.

Tested (head 6159773, merged onto main c160d86: no conflicts)

  • AMD Radeon RX 6700 XT (RDNA2, gfx1031, RADV NAVI22, Mesa 26.3.1), Ryzen 7 5700X, Linux 7.3.5. Proton Experimental 11.0-20261112's Wine, vkd3d-proton v3.1 + #257's patch (-Db_ndebug=true, dxil-spirv 53aac418), DXVK v3.1.1-209 dxgi, DXC 1.9.2609.5. Cross-built for x86_64-pc-windows-msvc, Rust 1.99.1, debug.
  • The #1246 pair, --test-threads=2 --exact gpu::recomp::tests::jumps_to_outer_loop_heads_are_structured hle::d3d12x::list_x::tests::write_buffer_immediate_on_open_existing_heap_visible_to_cpu: main fails 3/3 (kernel dispatch: D3D12 device removed after ~0.15 s). With this PR it passes 3/3.
  • cargo test -p xcl --lib -- --test-threads=4, 2 runs: 801 / 9 / 22 and 804 / 6 / 22 (passed / failed / ignored), no device removal. The 6 are the Wine-lane set: #530 ×2, #676, #890 ×2, gpu_depth_buffer_created_for_unordered_access_is_placed (#1475). Run 1 also had the #718 trio. main in the same setup: 795 / 14 / 22, with 5 device-removed lines (the #1246 cascade through sparse, title_textures and vram).
  • This also explains what I couldn't in #1246. Dropping every object the test made before the VirtualFree didn't help there, because the four ManuallyDrop barrier clones still held the resource, so the imported heap never went away.

Not tested

  • Windows. A retail title (test-only change).
  • Anything that needs game files: no XCL_SAMPLE_* set.

AI use

  • Claude Code (Claude Opus 5.5) ran the tests and wrote this report; I checked the results.

@Toons29

Toons29 commented Nov 25, 2026

Copy link
Copy Markdown

Regression run of 6159773d on the retail title, for this PR's Regression run section. Both blocks are from scripts/smoke.ps1 as printed, not edited: main on 4178e888, this PR on its head commit with no uncommitted changes, same title and time.

xcl-smoke v1
commit: 4178e888c0b5be50a2887eda45cdb832763b68a4
gpu: AMD Radeon RX 9070
title: game.exe
seconds: 60
exit: 0x0
frames shown: 1618 (PresentX 1618)
presents per 2 s (last 3): 61 60 60
device removals: 0
crashes: 0
peak commit: 14831 MB
xcl-smoke v1
commit: 6159773d93e826b4c328ad7174334c7a12c75541
gpu: AMD Radeon RX 9070
title: game.exe
seconds: 60
exit: 0x0
frames shown: 1629 (PresentX 1629)
presents per 2 s (last 3): 60 60 60
device removals: 0
crashes: 0
peak commit: 14816 MB

Against the limits in CONTRIBUTING.md: 101 % of main's frames, 100 % of its peak commit, no crash, no device removal.

AMD Radeon RX 9070 (RDNA4, driver 32.0.32029.2010), Ryzen 7 7800X3D, 32 GB, Windows 11 build 26300, Rust 1.100.0, cargo build --release -p xcl, 60 s, XCL_NETWORK=0. This is a regression check only: it does not show whether the run reaches the code this PR changes.

AI use: Claude Code (Claude Opus 5.5) built both trees and ran scripts/smoke.ps1; I checked the blocks before posting.

@JJAutoCompare

Copy link
Copy Markdown

Empirical test report (6159773)

Verified PR #1520 at 6159773d93e826b4c328ad7174334c7a12c75541.

Hardware / environment

  • Windows x64 build 26200 on AMD64 (family 23, model 113, stepping 0). Rust 1.100.0 (0ef1bc386, 2026-11-09). DXC 1.9.2609.5 (01b62ad47). Unoptimized test profile with debug info and incremental compilation disabled.
  • The focused run took the Intel Arc A770 test lock with XCL_ADAPTER=A770 and RUST_TEST_NOCAPTURE=1. The imported-heap fixture creates a device on the Windows default D3D12 adapter, with WARP as a fallback. It does not read XCL_ADAPTER or log the resulting device, so the adapter and driver used by that fixture were unverified.

Commands and results

  • cargo test --workspace --no-run exited 0 outside the GPU lock.
  • Ran the prebuilt xcl library test executable under the lock, with these two exact filters. The executable and wrapper both returned exit code 0.
& Invoke-ArcTest.ps1 -Test {
    & $xclLibTest --exact `
        hle::d3d12x::list_x::tests::imported_heap_test_barriers_do_not_retain_the_resource `
        hle::d3d12x::list_x::tests::write_buffer_immediate_on_open_existing_heap_visible_to_cpu
}
  • 2 passed, 0 failed, 0 ignored, 0 skipped, and 742 filtered out. The harness reported 0.16 s. Skips were counted from case-sensitive SKIPPED log lines.

Observed behavior

  • The COM reference-count regression passed. The transition barriers left the resource at one reference, and dropping the resource brought the count to zero.
  • The imported-heap fixture passed its CPU readback assertions for DIRECT and COMPUTE writes and the DIRECT copy. Its VirtualFree(..., MEM_RELEASE) assertion also passed after the resource and heap were dropped.

Not tested

The full GPU workspace suite and game smoke runs were omitted.

AI use

Verified with T3 Code / Codex (gpt-6.2-sol, reasoningEffort: high). Coordinator #2 (T3 Code / Antigravity, gemini-4-neon) verified counts against pr1520-focused.log and published.

@throny throny added the off-goal Does not bear on the current goal (world loading); waits until it is reached label Nov 25, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

off-goal Does not bear on the current goal (world loading); waits until it is reached

Projects

None yet

Development

Successfully merging this pull request may close these issues.

8 participants