Tests: stop leaking imported heap resource references #1520

Open
github-bot wants to merge 1 commit from github-bot/gh-1520 into main AGit
Owner

@⁠wileyGray on GitHub · 2026-11-24 17:12 UTC · original
wileyGray:codex/test-heap-lifetime → main · +57 −18 · 1 files · 1 commits · open

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.
**@⁠wileyGray** on GitHub · 2026-11-24 17:12 UTC · [original](https://github.com/xcl-dev/xcl/pull/1520) `wileyGray:codex/test-heap-lifetime` → `main` · +57 −18 · 1 files · 1 commits · open 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](https://github.com/ivall/xcl/issues/175#issuecomment-7125561381); 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. <details> <summary>Paired-test reproduction and exact GPU/debug skips</summary> 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): ```text --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). </details> **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 #1487; the specific leak was also reported in #175. This does not establish the cause of every parallel access violation.

Assisted-by: Codex (GPT-6; exact variant unavailable)
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 04:56:14 +00:00
Author
Owner

@⁠Bullet18x on GitHub · 2026-11-24 18:25 UTC · original

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

Tested

  • RTX 5090, driver 32.0.16.2114, Windows 11 Pro 10.0.28000, Rust 1.100.0 MSVC, debug, all XCL_SAMPLE_* unset.
  • imported_heap_test_barriers_do_not_retain_the_resource passes. The barriers no longer keep the resource alive.
  • cargo test -p xcl --lib -- --test-threads=32, 10 runs on 6159773: write_buffer_immediate_on_open_existing_heap_visible_to_cpu failed or was among the failures in 4 runs. In 1 of those 4 it was the only failure, on the new assert_ne!(VirtualFree(mem, 0, MEM_RELEASE), 0) (left: 0, right: 0). The other 3 also had an access violation (#1523's cause).
  • main 9e3831e + #1520 + #1523 + #1590, 25 runs at 32/16 threads: this assert failed 4 times, and nothing else in this test failed.

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.

**@⁠Bullet18x** on GitHub · 2026-11-24 18:25 UTC · [original](https://github.com/xcl-dev/xcl/pull/1520#issuecomment-7140619256) Test report for head `6159773`, with one finding about the new `VirtualFree` assert. **Tested** - RTX 5090, driver 32.0.16.2114, Windows 11 Pro 10.0.28000, Rust 1.100.0 MSVC, debug, all `XCL_SAMPLE_*` unset. - `imported_heap_test_barriers_do_not_retain_the_resource` passes. The barriers no longer keep the resource alive. - `cargo test -p xcl --lib -- --test-threads=32`, 10 runs on `6159773`: `write_buffer_immediate_on_open_existing_heap_visible_to_cpu` failed or was among the failures in 4 runs. In 1 of those 4 it was the only failure, on the new `assert_ne!(VirtualFree(mem, 0, MEM_RELEASE), 0)` (`left: 0, right: 0`). The other 3 also had an access violation (#1523's cause). - main `9e3831e` + #1520 + #1523 + #1590, 25 runs at 32/16 threads: this assert failed 4 times, and nothing else in this test failed. **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: ```rust 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.
Author
Owner

@⁠DavidM1911 on GitHub · 2026-11-24 18:54 UTC · original

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!

**@⁠DavidM1911** on GitHub · 2026-11-24 18:54 UTC · [original](https://github.com/xcl-dev/xcl/pull/1520#issuecomment-7141308228) 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!
Author
Owner

@⁠Toons29 on GitHub · 2026-11-25 03:32 UTC · original

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.

**@⁠Toons29** on GitHub · 2026-11-25 03:32 UTC · [original](https://github.com/xcl-dev/xcl/pull/1520#issuecomment-7152014203) 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.
Author
Owner

@⁠JJAutoCompare on GitHub · 2026-11-25 05:26 UTC · original

Empirical test report (6159773d)

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.

**@⁠JJAutoCompare** on GitHub · 2026-11-25 05:26 UTC · [original](https://github.com/xcl-dev/xcl/pull/1520#issuecomment-7153769948) ### Empirical test report (6159773d) 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. ```powershell & 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.
Author
Owner

@⁠ivall on GitHub · 2026-11-25 19:32 UTC · original

The leak fix is right and worth having (it also explains the device removal of #1246). What holds it is the new VirtualFree assert: it fails at times because another test's work still holds the last Release (Bullet18x: 4 of 25 runs at 16 and 32 threads, 0 of 20 with a bounded retry). #1590 is merged and retries up to 10 s in coherent_buffer_sanitizes_console_flags_and_state; please do the same here. The diff is inside the test module, so the test-only label can be set after that push.

AI use: Claude Code (Claude Opus 5.5) wrote this on the maintainer's behalf.

**@⁠ivall** on GitHub · 2026-11-25 19:32 UTC · [original](https://github.com/xcl-dev/xcl/pull/1520#issuecomment-7172440984) The leak fix is right and worth having (it also explains the device removal of #1246). What holds it is the new `VirtualFree` assert: it fails at times because another test's work still holds the last `Release` (Bullet18x: 4 of 25 runs at 16 and 32 threads, 0 of 20 with a bounded retry). #1590 is merged and retries up to 10 s in `coherent_buffer_sanitizes_console_flags_and_state`; please do the same here. The diff is inside the test module, so the `test-only` label can be set after that push. AI use: Claude Code (Claude Opus 5.5) wrote this on the maintainer's behalf.
This pull request can be merged automatically.
This branch is out-of-date with the base branch
You are not authorized to merge this pull request.
View command line instructions

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u origin '+refs/pull/1520/head:github-bot/gh-1520'
git switch 'github-bot/gh-1520'

Merge

Merge the changes and update on Forgejo.
git switch 'main'
git merge --no-ff 'github-bot/gh-1520'
git switch 'github-bot/gh-1520'
git rebase 'main'
git switch 'main'
git merge --ff-only 'github-bot/gh-1520'
git switch 'github-bot/gh-1520'
git rebase 'main'
git switch 'main'
git merge --no-ff 'github-bot/gh-1520'
git switch 'main'
git merge --squash 'github-bot/gh-1520'
git switch 'main'
git merge --ff-only 'github-bot/gh-1520'
git switch 'main'
git merge 'github-bot/gh-1520'
git push origin 'main'
This discussion has been locked. Commenting is limited to contributors.
No description provided.