Skip to content

d3d12-gpu: fix parallel unit test races in watch, descriptor, and pool - #2854

Open
lixonik wants to merge 4 commits into
ProjectLeonida:mainfrom
lixonik:fix/leonida-gpu-parallel-test-flakes
Open

lixonik wants to merge 4 commits into
ProjectLeonida:mainfrom
lixonik:fix/leonida-gpu-parallel-test-flakes

Conversation

@lixonik

@lixonik lixonik commented Dec 1, 2026 •

Copy link
Copy Markdown
Contributor

Claim: D3D12-GPU-TEST-PARALLEL-FLAKES (#2570 (comment))

Fixes three process-global parallel test issues in leonida-gpu reported in #2570 (where cargo test -p leonida-gpu --lib flaked under default parallel test threads while passing under --test-threads=1).

Root causes & fixes

  1. watch::tests (d3d12/crates/leonida-gpu/src/watch.rs)

    • drop_views called title_memory_remapped instead of title_memory_unmapping, deterministically leaking clean, PUBLISHED, and VIEWS state for an unmapped view address. Switched drop_views to title_memory_unmapping.
    • nothing_is_guarded_between_our_forgetting_it_and_its_unmapping and writes_to_views_are_not_lost called UnmapViewOfFile before dropping Remapping, returning the virtual address to Windows while g.changing still marked its block as left_alone. Ended the remapping window before UnmapViewOfFile (note: this race-window tightening is not directly covered by a unit test; reverting it leaves the suite green).
  2. descriptor::tests (d3d12/crates/leonida-gpu/src/descriptor.rs)

    • records_copied_as_bytes_are_made_at_the_next_sync raced with the 4 ms background "leonida-heap-sync" thread and concurrent test submissions calling sync_heaps() over process-global SHADER_HEAPS: a competing sync round could consume the write-watch reset for slot 5 before publishing its host descriptor, causing the test's post-sync assert!(host.made(5)) to observe false. Added a #[cfg(test)] TEST_SYNC_LOCK around sync_heaps() and held it across the raw copy, pre-sync check, and sync call.
  3. pool::Reclaim / texture::alias_cast_tests (d3d12/crates/leonida-gpu/src/pool.rs)

    • Reclaim::release returned a pooled piece without removing the released ID3D12Resource raw pointer from RECENT or UNSEEN. When a subsequent committed texture reused that COM pointer within 1 second, pool::fresh falsely reported true, failing assert_eq!(pool::fresh(&copy.texture), false) in committed_and_placed_r32_uint_textures_cast_to_r9g9b9e5_under_legacy_barriers. Stored raw: usize in Reclaim and called forget_released(gone.raw) before returning gone.piece to TEXTURES on final release.
    • Added unit test released_texture_pointer_is_not_kept_in_recent_or_unseen (tests forget_released directly, inserting (raw, u64::MAX) into UNSEEN without calling note_unseen's eviction sweep) and COM last-reference drop regression test dropping_last_pooled_texture_reference_forgets_recent_and_unseen (allocates a 32x32 RGBA8 placed texture via pool::texture, holds two COM references, confirms entries remain after dropping the first reference, and confirms RECENT and UNSEEN are cleared after dropping the final reference triggers Reclaim::release).

Verification

  • Before (main ad7c62edb, 8 parallel runs): watch::tests::other_memory_at_a_guarded_address_keeps_its_protection and watch::tests::what_is_put_in_beside_the_guards_is_told_at_every_view panicked on parallel run 2/8.
  • Red/green for dropping_last_pooled_texture_reference_forgets_recent_and_unseen:
    • Without forget_released(gone.raw) in Reclaim::release: 0 passed; 1 failed (panicked on post-drop RECENT assertion; independently reproduced by @Neutron-0 and @KelboLive).
    • With forget_released(gone.raw) before lock(&TEXTURES).give(...) (81a6e94a9):
    • (depth_bounds_pipeline_loads_and_filters_draws fails deterministically on Windows 10 inbox D3D12 without Agility SDK; tracked in d3d12-gpu: drop unused SM 6.6 root signature flags in depth_bounds test #2855.)

GPUs tested: NVIDIA GeForce RTX 5070 12 GB (driver 621.40, Windows 10 Pro 22H2 build 19045, Ryzen 9 5950X 16C/32T)

AI-assisted: Antigravity (plus separate read-only reviewer subagent)

@MoonEyes

MoonEyes commented Dec 1, 2026

Copy link
Copy Markdown
Collaborator

Your claim is there (D3D12-GPU-TEST-PARALLEL-FLAKES, #2570 (comment)), but the description does not name it. Could you add the ID and that link, so the merge checklist finds it?

One small thing while reading the diff: TEST_SYNC_LOCK sits between the doc comment of sync_heaps and the function, so those lines now document the static.

This one changes pool.rs outside the tests, so it is in the in-game run I have queued on an RTX 4060.

AI-assisted (Claude Code).

@Kal0s-design

Copy link
Copy Markdown
Contributor

Independent Windows check, A/B over repeated runs: 99ed48337 merged onto current main 653f60d37 (no conflict, 1b2be1bfc) against main itself. Native Windows 11, MSVC x64, Rust 1.98.1. I built the leonida-gpu lib test binary once per side (debug, default parallel test threads) and ran each whole suite 40 times back to back, alternating nothing else.

runs with a failure tests that failed
main 653f60d37 4 / 40 watch::tests::what_the_kernel_writes_through_is_not_guarded_for_another_view, watch::tests::kernel_reads_into_guarded_blocks_after_opening, watch::tests::a_write_is_reported_at_every_checked_view (one per run; one run's name wasn't captured)
main + this PR 0 / 40 none

So the watch cross-test interference reproduces on main here (about 1 run in 10), and with this PR it doesn't show in 40 runs. On another day I also saw descriptor::tests::records_copied_as_bytes_are_made_at_the_next_sync fail once in 9 runs on a branch, which is the second race this PR addresses. It didn't come up in these 40 main runs, so I can't add a count for it.

GPUs tested: n/a (unit tests). AI-assisted: yes.

@lixonik

lixonik commented Dec 1, 2026

Copy link
Copy Markdown
Contributor Author

@MoonEyes Thanks for catching both!

  • Added the claim ID (D3D12-GPU-TEST-PARALLEL-FLAKES) and link to the PR description.
  • Moved TEST_SYNC_LOCK above sync_heaps's doc comment so those doc lines stay attached to pub(crate) fn sync_heaps(), and re-verified records_copied_as_bytes_are_made_at_the_next_sync.

@Kal0s-design Thanks for the independent 40-run Windows 11 A/B verification (4/40 -> 0/40)!

AI-assisted: Antigravity

@enlight3d

Copy link
Copy Markdown
Contributor

RUN (independent run on Windows, #2854 head 99ed483, merged locally into main 3393cee with git merge --no-ff, no conflicts. The head still merges cleanly into main bf30e25. CI on the head: change-scope, check, linux-python-and-viewer, no-game-data and semantic-tests all pass.)

Commands, in d3d12/, target x86_64-pc-windows-msvc, toolchain 1.98.1, all with --locked, at low priority. cargo test -p leonida-gpu --lib --no-run of main and of the merge; then the two test binaries were run alternately, each with the default thread count (32 here, no --test-threads), as cargo test -p leonida-gpu --lib runs them:

  • Default build, 32 runs each side: main 31 clean, 1 failure (watch::tests::what_the_kernel_writes_through_is_not_guarded_for_another_view, watch.rs:6010, left Some([..]), right Some([])); merge 32 clean (602 passed / 0 failed / 55 ignored, the extra test being the new one). So no new failure, and the one parallel failure seen on main is a watch test. The flake rate here is low (1 in 32 on main), so these runs alone cannot prove the fix; the two tests named in the body did not fail on main in 32 runs here.
  • --features diagnostics build, 10 runs each side: main 2 clean, merge 2 clean. timeline::record::tests::global_stall_routing_retains_spans_waits_wakes_and_sent_fences_without_stacks fails in parallel on both sides (main 7 of 10, merge 8 of 10, record.rs:2325, no "synthetic stall span"): a parallel race this pull request does not touch. descriptor::tests::records_copied_as_bytes_are_made_at_the_next_sync, which this pull request fixes, failed once on main and never on the merge.
  • Red, by assertion: forget_released emptied (fn forget_released(_raw: usize) {}), the new test kept: released_texture_pointer_is_not_kept_in_recent_or_unseen fails on its first assertion (!RECENT...any(|r| r.0 == raw), pool.rs:1295). Green after restoring: 1 passed.
  • What that test does not cover: it calls forget_released directly. With the call taken out of release (let _ = gone.raw;), the whole suite still passes (602 / 0 / 55, one run). So no test checks that the last release of a pooled texture really forgets it; a test that makes a pooled texture, drops it and then asks fresh for its pointer would.
  • Lock order, read through pool.rs (RECENT, UNSEEN, TEXTURES): RECENT and UNSEEN are private to pool.rs. They are taken in fresh, made_now, begun_before, forget_released and the new test, and each guard covers only work on its own list of numbers (under UNSEEN, made_now also calls host::submissions(), which reads two atomics). No other lock is taken and no COM object is released while either is held, and the two are never held together (made_now drops RECENT before it takes UNSEEN; forget_released takes one, then the other). In release, the TEXTURES guard is a temporary dropped at the end of its statement, before forget_released. So both stay leaf locks: no reversed order and no re-entry is possible. A caller that holds some other lock when it drops the last reference to a pooled texture only adds an edge into these two leaf locks, which cannot close a cycle.
  • descriptor.rs: TEST_SYNC_LOCK and its use in sync_heaps are #[cfg(test)], so a normal build does not change. But the new static sits between the doc comment of sync_heaps and the function: that whole doc block (including the LEONIDA_HEAP_SYNC=0 line) now documents TEST_SYNC_LOCK, and sync_heaps has no doc comment in a normal build. Moving the static above that block would fix it. python -I tools/d3d12_switches.py --check d3d12/docs/switches.md is still clean on the merge.
  • cargo clippy -p leonida-gpu --all-targets, default and --features diagnostics, on main and on the merge: the same warnings on both sides (69 and 68 locations, counting those the command reports in leonida-shader and leonida-lua), none new.
  • Not covered here: the game. forget_released now runs at every last release of a pooled texture (a scan of RECENT, the textures made in the last second, under its lock).

Since this run the head moved to eab9e2a, which only moves TEST_SYNC_LOCK above the doc comment of sync_heaps (5 lines in descriptor.rs): that fixes the doc point above and changes nothing else tested here.

Machine: Windows 11 Pro 26300, i9-13900K, 64 GB, RTX 3090 24 GB, NVIDIA 621.40, Rust 1.98.1.

@MoonEyes

MoonEyes commented Dec 1, 2026

Copy link
Copy Markdown
Collaborator

One thing before a merge, from enlight3d's check and confirmed by reading: taking the call of forget_released out of release leaves the whole suite green, since the new test calls the function directly. Could you add a test that drops the last reference of a pooled texture (or a Reclaim) and fails without that call? The rest reads fine: the locks are leaves and the entry goes before the address can be reused.

AI-assisted (Claude Code).

@merzleckeier

Copy link
Copy Markdown
Contributor

I tested the requested last-reference regression at eab9e2ab + #2884 on b3910e5e. The fixture makes a real 32×32 RGBA8 placed texture through pool::texture, requires that pooled allocation to succeed, and holds two COM references. It checks that both tracking entries exist initially and after the first drop. After dropping the last reference, it checks that HELD has returned to its original count and both RECENT and UNSEEN no longer contain the key.

Device under Wine 11.22 With the PR With only forget_released(gone.raw) removed from release
AMD Raphael / RADV 26.3.1 returned pool piece, both entries cleared returned pool piece, both entries remained
RTX 4070 SUPER / 620.73.04 returned pool piece, both entries cleared returned pool piece, both entries remained

That gives a red/green test through COM destruction without relying on pointer reuse. The separate full diagnostics runs with #2884 kept the same eight non-target Wine failures seen with #2884 alone. All three combined runs passed the new stock bookkeeping test and the timeline-record tests. Win64 MSVC target, Rust 1.98.1, synthetic inputs. MEASURED, AI-assisted (Codex).

@lixonik

lixonik commented Dec 1, 2026

Copy link
Copy Markdown
Contributor Author

@MoonEyes Added pool::tests::dropping_last_pooled_texture_reference_forgets_recent_and_unseen in bce4a898b:

  • Allocates a real 32x32 RGBA8 placed texture via pool::texture, holds two COM references, verifies RECENT and UNSEEN stay populated after dropping the first reference, and verifies both entries are cleared when dropping the final reference triggers Reclaim::release.
  • Red/green: Removing forget_released(gone.raw) from Reclaim::release fails the test (0 passed; 1 failed); restoring it passes (1 passed under both default features and --features diagnostics, and 20/20 parallel runs of pool::).

@merzleckeier Thanks for testing the last-reference drop path independently as well!

@DIGITALWORLD-PL

Copy link
Copy Markdown

For comparison with the test-only lock in this PR, we tested an alternative for records_copied_as_bytes_are_made_at_the_next_sync on Windows with a GeForce RTX 5090 (driver 621.01).

We paused a competing synchronization round after it had taken the dirty page for slot 5 and before it published the host descriptor. Under that forced interleaving, the original test failed 6/6 runs at assert!(host.made(5)) after its own sync_heaps() returned. With our patch, the test passed 6/6 runs under the same schedule: three with the helper thread and three with inline synchronization.

Our one-file patch checks the fresh slot before the raw copy, submits a closed command list through Host::execute_sync_internal, and checks publication after it returns. The experiment exercised CPU-side descriptor publication on the RTX-backed D3D12 device. We tested the original code and our patch; we have not run this experiment against #2854's TEST_SYNC_LOCK implementation.

AI-assisted testing and independent review with OpenAI Codex (GPT-6.2 Astra).

@KelboLive KelboLive left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

The strongest hunk is drop_views → title_memory_unmapping — open/remapped never cleared clean, PUBLISHED or VIEWS, so a freed address kept stale state; that is a deterministic leak, not a race. The descriptor lock is real too: my parent-commit A/B caught assert!(host.made(5)) failing 1/60 full-suite runs, and the head is 0/40. One correction: the body describes that race backwards — the observed failure was the post-sync assert seeing false because another sync had consumed the write-watch reset, not !made seeing an early mirror. Same root cause, opposite direction.

Two nits in pool.rs:

  1. Call forget_released(gone.raw) before lock(&TEXTURES).give(...) (:713-714). As written, a texture created at the same COM address in that gap can have its fresh RECENT/UNSEEN entry wiped by the release. The mechanism is right (wrapper reuse is independent of the 2.5 s piece REST), just the order.
  2. The new tests are not hermetic: dropping_last_... asserts on process-global RECENT/UNSEEN after freeing a COM object (a parallel pool test landing on the same wrapper address re-adds raw), and released_texture_pointer_... calls note_unseen(..., 10, 5) (:1295), whose retain evicts every other UNSEEN entry with begun <= 5. Low probability, but this is a flake PR.

The watch reorder (:5190) is not covered: reverting it leaves the whole 602-test suite green. That is acceptable for a race, but please don't count it as tested — it removes the specific window where the address is back with Windows while guards().changing still makes left_alone() skip guarding; the residual window between drop(changing) and UnmapViewOfFile is argued, not closed.

Mutation coverage otherwise holds: dropping forget_released from release fails the new test at pool.rs:1345, and dropping just UNSEEN.remove fails both. Merge is clean against current main; a local merge builds and passes 635/0/59 despite the 384-commit gap. #2896 is complementary (different hunks), but note #2908's require_host() sweep would leave the new pool.rs:1313 test on the old silent form.

@Neutron-0

Neutron-0 commented Dec 1, 2026 •

Copy link
Copy Markdown

Verified on Windows 11, Rust 1.98.1, no GPU and no game files.

The fix is shown to be needed, by reverting only the source the change touches. The pull request's own new test pool::tests::dropping_last_pooled_texture_reference_forgets_recent_and_unseen passes on the head bce4a898b, and fails when the one line the fix adds to Reclaim::release is taken out of the head's d3d12/crates/leonida-gpu/src/pool.rs (forget_released(gone.raw);), with the rest of the head untouched:

result
the head bce4a898b test result: ok. 1 passed; 0 failed
the head with that one line removed test result: FAILED. 0 passed; 1 failed

That test builds a real 32x32 RGBA8 texture, keeps a second COM reference so the first drop does not release it, drops the last reference, and then checks that the raw pointer is gone from both RECENT and UNSEEN - so it does cover the wiring, not only the helper.

The other new test covers less than its comment says. pool::tests::released_texture_pointer_is_not_kept_in_recent_or_unseen passes on the head, and it still passes with that same line removed: it calls forget_released(raw) itself rather than releasing anything, so it tests the helper and not the path that calls it. Its doc comment ("Releasing a pooled texture clears its raw pointer from RECENT and UNSEEN") describes the other test. Not blocking - the second test covers the wiring - but the comment overstates what is checked.

What the parallel runs showed. The whole leonida-gpu test binary was run in parallel, with the shape the report uses (cargo test -p leonida-gpu --lib --features diagnostics -- --skip depth_bounds_pipeline_loads_and_filters_draws), so that the other test threads are there to contend. This was done three times, twenty runs on the head and forty on the base:

runs
the head bce4a898b 20 passed, 0 failed
the base 7fbcd456e 33 passed, 7 failed

The seven failures on the base were in four different tests, and only one of them is named in the report:

texture::alias_cast_tests::committed_and_placed_r32_uint_textures_cast_to_r9g9b9e5_under_legacy_barriers
  panicked at crates\leonida-gpu\src\texture.rs:5664:17
  assertion `left == right` failed: left: true, right: false
  (the pool race this pull request fixes: assert_eq!(pool::fresh(&copy.texture), false) reading true)   [3 runs]

watch::tests::what_the_kernel_writes_through_is_not_guarded_for_another_view
  panicked at crates\leonida-gpu\src\watch.rs:6010:9
  assertion `left == right` failed: left: Some([42241094]), right: Some([])
  (the second guard() query expected the block to be free, and it was still guarded)                     [1 run]

watch::tests::a_block_guarded_before_we_were_told_of_its_view_can_be_written
  panicked at crates\leonida-gpu\src\watch.rs:5343:9
  assertion failed: !writable(at) && !writable(at + ...)                                                 [1 run]

timeline::report::tests::real_waits_are_recorded_with_who_ended_them
  panicked at crates\leonida-gpu\src\timeline\report.rs:1459:9
  assertion `left == right` failed: left: None, right: Some(20124)                                      [2 runs]

The two watch failures are in the file this pull request changes, under names the report does not list. The report names watch::tests::nothing_is_guarded_between_our_forgetting_it_and_its_unmapping and watch::tests::writes_to_views_are_not_lost; the ones that actually fired here are what_the_kernel_writes_through_is_not_guarded_for_another_view and a_block_guarded_before_we_were_told_of_its_view_can_be_written. The first is the same shape the report describes - a view's address reused by another thread while this thread's guard state still listed the block, so the second guard() query found the block still guarded by somebody else - and the pull request's watch.rs change (ending the remapping window before UnmapViewOfFile returns the address to Windows, and title_memory_unmapping where title_memory_remapped was called) is aimed at exactly that window. Both of these tests should be named in the report, because they are the ones that failed here.

The timeline failure is a different race, in a file this pull request does not touch, and its cause is known. crates/leonida-gpu/src/timeline/report.rs:1459 is not covered by this pull request, and neither by #2884 (which fixes the ROLLING.dropped_contention counter in timeline/record.rs). The assertion reads left: None, so there was no wait of 10 ms or more to attribute at all: the test spawns the thread that is to wait on the mutex, sleeps a fixed 20 ms and then releases the lock, and under the load of the whole binary running in parallel the new thread sometimes has not reached the lock by then. It is filed as a separate finding: in #2570, because cargo test --workspace on main is red at random when it fires. Note the shape of the four pull requests: #2854 fixes the pool and watch races, #2884 fixes the timeline/record.rs race, and the timeline/report.rs race is a fourth one that nobody has taken.

A note on the rates. A race that fires in one run out of ten does not fire in every set of ten, so the difference between the head and the base here is seven failures in forty against none, not a clean per-test verdict for each of the races. The deterministic control above is what shows the change is needed; the parallel runs show the suite is not yet clean.

Not checked: the descriptor race in this pull request (it needs the same contention and did not fire in these sixty runs); the GPU crates under a real adapter (no GPU device here).

@lixonik

lixonik commented Dec 1, 2026

Copy link
Copy Markdown
Contributor Author

@KelboLive @Neutron-0 Thanks for the thorough verification and mutation checks! Addressed the feedback in 81a6e94a9 and updated the PR description:

  1. Release ordering (pool.rs): moved forget_released(gone.raw) before lock(&TEXTURES).give(gone.piece, now()) so a texture reusing the COM address after give cannot have its fresh RECENT/UNSEEN entry wiped.
  2. Hermetic UNSEEN test (pool.rs): released_texture_pointer_is_not_kept_in_recent_or_unseen now inserts (raw, u64::MAX) directly into UNSEEN instead of calling note_unseen(..., 10, 5) (which ran retain across other tests' entries), and its doc comment now accurately states it tests forget_released directly while dropping_last_pooled_texture_reference_forgets_recent_and_unseen covers the COM release path.
  3. PR description: corrected the descriptor.rs race direction (assert!(host.made(5)) seeing false after a competing sync consumed the write-watch reset) and explicitly noted that the watch.rs remapping-drop reorder is not directly covered by a unit test.

Re-verified cargo test --locked -p leonida-gpu --lib pool:: (10/10 parallel runs, 19 passed, 0 failed) and scripts/check_no_game_data.py.

@DIGITALWORLD-PL Thanks for sharing the forced-interleaving confirmation on RTX 5090 -- that cleanly reproduces the exact assert!(host.made(5)) race direction!

@merzleckeier

Copy link
Copy Markdown
Contributor

Real final-reference check at the pool return boundary on 81a6e94a merged into 1fff69129. The stock 32×32 real pooled texture test passed 3/3 on each of RTX 4070 SUPER (620.73.4) and Radeon RADV (26.3.1), Win64 Wine 11.22 / VKD3D-Proton 3.1.0.

I added the same observer immediately before TEXTURES.give, restricted to the test texture’s actual COM pointer. Both RECENT and UNSEEN were already empty for that pointer in all six runs with the updated order. Moving only forget_released back below give left both entries present at that point in all six comparison runs. Both arms still passed the assertions before final release and after cleanup. This distinguishes the ordering change from eventual cleanup. The probe did not force concurrent COM-pointer reuse.

Separately, the selected descriptor sync, alias cast and kernel-write watch tests passed 3/3 in all 24 starts across both trees, both GPUs, and one/eight test threads. The full serial Wine-builtin no-device suite reproduced the same three watch assertions on both trees in every repeat (holes_are_known_until_the_runtime_tells, no_mapping_is_lost_between_askers, what_is_left_alone_is_no_known_hole). Main reported 640 passes and the PR 642, both with three failures and 59 ignored.

MEASURED with synthetic inputs, OpenAI Codex and a separate read-only review.

@imShangLi

Copy link
Copy Markdown
Contributor

VERIFY-2854 — Linux, Fedora 45, Rust 1.98.1 (the pinned toolchain), Wine 11.21, no GPU device, no game files

Verdict: PASS. The pooled-texture fix is a real bug fix and the ordering fix in the watch tests closes a window where an address could be handed back to Windows while a guard still held it. Nothing that passed before fails now.

Head bce4a898, base the current main, merge base 7fbcd456, 3 files, +92/−5: d3d12/crates/leonida-gpu/src/pool.rs, d3d12/crates/leonida-gpu/src/descriptor.rs, d3d12/crates/leonida-gpu/src/watch.rs.

1. What it changes

A pooled texture's address could be mistaken for a new one. pool.rs keeps two tables of raw pointers: RECENT (textures made in the last second) and UNSEEN (ones the GPU has not touched). When the last COM reference to a pooled texture went, Reclaim::release gave the piece back to the pool but left the raw pointer in both tables — and a new resource allocated at the same address would then be treated as one that was "made in the last second", which is what the commit and write-back heuristics read. The change removes it:

/// Removes a released pooled texture pointer from `RECENT` and `UNSEEN`
/// so a new resource allocated at the same address is not mistaken for it.
fn forget_released(raw: usize) {
    RECENT.lock().unwrap_or_else(|e| e.into_inner()).retain(|r| r.0 != raw);
    UNSEEN.lock().unwrap_or_else(|e| e.into_inner()).remove(&raw);
}

called from Reclaim::release, with the raw pointer carried in the Reclaim box.

An address could be handed back to Windows while a guard still held it. In watch.rs's tests the view was unmapped before the remapping guard was dropped, so another thread could take the address while g.changing still left its block alone. The change drops the guard first:

// End the remapping window before returning `at` to Windows so another
// thread cannot reuse the address while `g.changing` still leaves its block alone.
drop(changing);
assert!(!guards().left_alone(at / BLOCK));
// SAFETY: our view.
unsafe { UnmapViewOfFile(view) };

and the same order is used in many_askers_tests.

A test that raced the background sync thread. descriptor.rs gains a test-only mutex so a test that inspects a descriptor slot between two syncs cannot race the sync that writes it.

2. Red and green, run under Wine

leonida-gpu does not build on Linux (upstream: "The crates are Windows-only today … Linux follows #2577"), so I cross-compiled it for x86_64-pc-windows-gnu and ran the tests under Wine 11.21.

The red side (the merge base with only this pull request's test changes) does not compile: the new test calls forget_released, which the change introduces (error[E0425]: cannot find function 'forget_released' in this scope). So the usual "the test fails on the base" cannot be shown, and I compared the base with the pull request's head instead:

the merge base 655 tests
the pull request's head 657 tests
the new tests, only on the head 2 — pool::tests::released_texture_pointer_is_not_kept_in_recent_or_unseen and pool::tests::dropping_last_pooled_texture_reference_forgets_recent_and_unseen, both pass
tests that pass on the base and fail on the head 0
failing on both sides 3 (see below)

The three that fail on both sides are watch::many_askers_tests::holes_are_known_until_the_runtime_tells, watch::many_askers_tests::no_mapping_is_lost_between_askers and watch::tests::what_is_left_alone_is_no_known_hole — they are about which pages the runtime reports as known holes, and they fail identically with and without this change, so they are not this change's doing. They are the tests this pull request is about (it fixes races in them), and they need Microsoft's VirtualAlloc/VirtualQuery behaviour, which Wine does not have.

3. What I did not check

  • Whether the race the pull request fixes is the cause of the failures above. The three watch tests fail here on both sides, so I could not see them turn green; the pull request's own claim (that they are flaky under parallel runs) is not something I could confirm or refute here.
  • The pooled-texture path with a real device. pool::tests::dropping_last_pooled_texture_reference_forgets_recent_and_unseen needs a host device (crate::host::host()) and returns early without one; under Wine a device exists, but I did not check that the texture really came back through the pool rather than through another path.
  • The commit and write-back heuristics that read RECENT and UNSEEN. I checked that the stale pointer is gone; I did not trace what the heuristics now decide differently.
  • Windows and MSVC. Everything above is Linux with Wine 11.21 (staging) and no GPU device.

AI-assisted: MiniMax M3.1 Flash.

@andrew-ai

andrew-ai commented Dec 2, 2026 •

Copy link
Copy Markdown

Native Windows verification with separate leonida-gpu library test executables at merge base 7fbcd45 and PR head 81a6e94. Each invocation started a fresh process with --test-threads 16.

Coverage Runs per revision Base, each run PR, each run
watch::tests:: 10 31 passed, 0 ignored 31 passed, 0 ignored
Full library, default features 3 600 passed, 55 ignored 602 passed, 55 ignored

No failures in either group. The full-suite runs exercised concurrent tests from other modules as well. The baseline flake did not reproduce here, so these results verify repeated passes on Windows without establishing a change in failure rate. Codex-assisted.

Neutron-0 added a commit to Neutron-0/mandy that referenced this pull request Dec 2, 2026
… to start (GPU-TIMELINE-REPORT-WAIT-HANDSHAKE)

`real_waits_are_recorded_with_who_ended_them` (timeline/report.rs, tests)
holds a mutex, spawns a thread that is to block on it, sleeps a fixed 20 ms
so that the thread has reached `lock.lock()` by then, lets go, and asserts
that the trace recorded a wait of 10 ms or more naming this thread as the
waker. The 20 ms is a bet on the scheduler. On a loaded machine the new
thread has not been started inside it, so it acquires the lock at once, no
wait of 10 ms or more exists, and the assert fails with `left: None` against
`right: Some(this thread)`. Measured on main 60f7dcd: 3 failures in 40
whole-suite runs of `cargo test -p leonida-gpu --lib --features diagnostics`,
and the failing line is the assert, not the recorder.

The length of the wait is now the holder's own doing: the worker takes the
lock first, says so through a mutex and a condvar, and keeps it for 30 ms;
this thread waits for that word and then blocks on the lock for as long as
the worker keeps it. The wait is as long as the holder's sleep and not as
long as the machine takes to start a thread, so there is no window left to
lose, and the test asserts that duration directly as well as the waker.

The waits are now read from this thread, because this is the thread that
waits: the wait for the lock is the latest of its waits of 10 ms or more.
The gate it waited on in the first half names the first worker, so the first
match was that one; the latest is the lock's. The chain and the rest of the
test are untouched, and `last` is still taken before the contention, so none
of this enters it.

Red and green on this machine, no game files and no GPU:

- Stock test, with 30 ms of start-up delay in the spawned thread (what the
  20 ms sleep assumes away): FAILED, `left: None`, `right: Some(27892)`.
- This test, with the same 30 ms of start-up delay: ok.
- This test without the perturbation: 10 of 10 runs ok.
- Whole diagnostics suite (`cargo test -p leonida-gpu --lib --locked
  --features diagnostics`): 3 runs, 749 passed, 0 failed, 59 ignored, each.
- Default suite (`cargo test -p leonida-gpu --lib --locked`): 667 passed, 1
  failed, 59 ignored; the failure was `texture.rs`'s
  `alias_cast_tests::..._r9g9b9e5_under_legacy_barriers`, the race ProjectLeonida#2854
  fixes, which passes alone (6 passed, 1 ignored) and is not this test.
- Teeth: with `Trace::waker` made to return `None`, this test fails at the
  assert, so the wait is still checked for who ended it.

Only the test changes, and only under the diagnostics feature; no product
code.

AI-assisted: yes - DeepSeek V4.1 Flash Free (OpenCode), with a separate
read-only review agent.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

10 participants