Skip to content

d3d12-gpu: stabilize global stall routing test under parallel diagnostics runs - #2884

Merged
OreoFR merged 1 commit into
ProjectLeonida:mainfrom
lixonik:fix/timeline-stall-test-parallel-flake
Dec 1, 2026
Merged

OreoFR merged 1 commit into
ProjectLeonida:mainfrom
lixonik:fix/timeline-stall-test-parallel-flake

Conversation

@lixonik

@lixonik lixonik commented Dec 1, 2026 •

Copy link
Copy Markdown
Contributor

Claim: D3D12-TIMELINE-STALL-TEST-PARALLEL (#2570 (comment))

Fixes the --features diagnostics parallel test flake in d3d12/crates/leonida-gpu/src/timeline/record.rs (global_stall_routing_retains_spans_waits_wakes_and_sent_fences_without_stacks, reported by @enlight3d in #2854).

Root cause

Under parallel cargo test -p leonida-gpu --lib --features diagnostics, global_stall_routing_retains_spans_waits_wakes_and_sent_fences_without_stacks shares the process-wide 65,536-entry ROLLING ring buffer with all other test threads. Previously, it emitted "synthetic stall span" first and then spawned a worker thread and synchronized a Condvar + Barrier wait/wake before calling finalize_stall. While the thread-spawn + condvar step ran, concurrent tests flooded ROLLING and evicted the earlier "synthetic stall span" from the ring before freeze (as @KelboLive confirmed by instrumenting the base: the span landed in ROLLING with present=true, then vanished by freeze with overwritten=339,176).

Separately, emit_global_without_contention polled the process-wide ROLLING.dropped_contention counter and silently gave up after 64 retries instead of checking that the test's own event was actually retained in ROLLING.

Change

  • Reordered global_stall_routing_retains_spans_waits_wakes_and_sent_fences_without_stacks so the thread-spawn + condvar wait/wake runs before emitting "synthetic stall span", Sent, and the fence-linked Wait, shrinking the window during which concurrent tests can evict those events before freeze.
  • Added #[cfg(test)] RollingBuffer::has_event(&self, pred) in d3d12/crates/leonida-gpu/src/timeline/record.rs.
  • Replaced emit_global_without_contention with emit_global_until(pred, emit), which verifies the specific synthetic event is present in ROLLING (panicking loudly after 64 attempts rather than silently giving up). Applied to both global_stall_routing_retains_spans_waits_wakes_and_sent_fences_without_stacks and step_stall_present_writes_on_background_thread_and_preserves_reentrancy_safety.

Note: this does not eliminate concurrent test traffic into the shared ROLLING ring itself; it removes the thread-spawn eviction gap and turns any emit-time drop into an explicit failure.

Verification

  • Before (on main b3910e5e3, 10 parallel runs of cargo test --locked -p leonida-gpu --lib --features diagnostics -- --skip depth_bounds_pipeline_loads_and_filters_draws): global_stall_routing_retains_spans_waits_wakes_and_sent_fences_without_stacks panicked at record.rs:2325:9 (1/10 runs here; 7/10--8/10 on @enlight3d's machine; 18/20 on @KelboLive's machine).
  • After (20 consecutive parallel runs of the same command): 0 / 20 failures in timeline:: (timeline::record::tests::* passed 20/20; 0/20 on @KelboLive's machine as well).

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

@merzleckeier

Copy link
Copy Markdown
Contributor

Independent parallel diagnostics runs at 4b52062d merged onto b3910e5e, with fresh Win64 MSVC-target builds and Wine 11.22. I ran the 11 timeline::record::tests with eight test threads four times per tree on each of RTX 4070 SUPER (620.73.04) and Radeon Raphael (RADV 26.3.1). All eight targeted runs passed per tree.

I also ran the full GPU crate selection on RTX 4070 SUPER three times per tree and three more with #2854 applied. No timeline::record test failed in those nine runs. The wider runs stayed red for failures also observed on main (including a separate timeline::report test). The original stall-routing flake did not reproduce on this Wine setup, so this is integration coverage for the changed tests and #2854 together.

MEASURED with synthetic tests, no game files. AI-assisted (Codex).

@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.

Verified empirically, and the fix works: the target test fails 18/20 full-suite diagnostics runs on base b3910e5e3 and 0/20 with this branch, and the merged tree passes. CI green, merge clean, and record.rs has not moved between the branch base and current main.

But the stated root cause does not match the failure. Instrumenting base shows the span is in ROLLING right after emit_global_without_contention returns (present=true, overwritten=0), and it only vanishes by freeze with overwritten=339,176 and the ring full at 65,536 — so it is eviction by other tests' events, not a push that never landed. The new has_event check (record.rs:674-676) is a real improvement (silent give-up becomes a panic), but it proves presence only at emit time; the same eviction can still wipe the event between the last check and freeze(). The underlying shared-ring flooding is not addressed.

Two suggestions that would keep this class visible instead of latent:

  • assert on data.meta.overwritten == 0 (or gate the foreign producers) so a future flood fails loudly rather than relying on the timing window;
  • make the wait predicate object-specific — Kind::Wait && e.b == 0 (record.rs:2282) matches any concurrent non-card wait, so the test can be satisfied by someone else's wait.

Mutation note: replacing the retention guard with if true passes the stock test 8/8, so the new guard is only covered when contention actually hits — the temporary 2 ms ring hold I used is the only way I got it red. That is acceptable for a flake fix, but it means the guard is not a regression test on its own.

Nit: the over-long assert! at record.rs:2283 adds a 15th rustfmt diff to a file that already has 14; fmt is report-only here, so cosmetic.

Verdict: approve with the root-cause correction — no runtime risk, and the flake is gone empirically — but the narrative should say "evicted by other tests", not "the push was lost".

@Neutron-0

Copy link
Copy Markdown

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

The change is right in shape, and the flake it fixes did not reproduce on this machine. The whole leonida-gpu test binary was run in parallel, ten times on each side, 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:

runs the test this pull request fixes
the base b3910e5e3 10 0 failures
the head 4b52062d5 10 0 failures

global_stall_routing_retains_spans_waits_wakes_and_sent_fences_without_stacks did not fail on either side in twenty runs, so on this machine the change is neither needed nor harmful - it is simply not the thing that reproduces here. The report's own measurement (1 of 10 runs on main ad7c62edb) is a different commit and a different machine, and a race that fires in 1 run out of 10 is not going to fire in every set of ten.

One failure was seen, in a different test, and it belongs to #2854. On run 8 of the head:

texture::alias_cast_tests::committed_and_placed_r32_uint_textures_cast_to_r9g9b9e5_under_legacy_barriers
thread ... panicked at crates\leonida-gpu\src\texture.rs:5664:17

That is the pool race #2854 fixes (Reclaim::release leaving the released resource's raw pointer in RECENT/UNSEEN, so a reused pointer is reported as fresh), and #2854 is still open. It is worth knowing that the parallel run of this crate is not yet clean even with this pull request: #2884 and #2854 fix different races in the same test binary, and both are needed for a green parallel run.

What the change does, and it matches the report. emit_global_without_contention polled the process-wide ROLLING.dropped_contention counter to decide whether its own synthetic event had landed; another test thread advancing that counter made the helper give up before the event was in the buffer, and the test then failed on data.events.iter().any(...). The replacement asks the buffer itself (RollingBuffer::has_event) whether the event is there, and retries until it is. That is the right fix: the counter was a proxy for a question the buffer can answer directly, and the proxy was shared with every other thread in the process.

One note on the helper's shape. emit_global_until retries until the event is present, so a genuine failure inside the emitter - an event that is never retained because the buffer is full - would now be retried until the test times out rather than failing on the first attempt. The loop is bounded by the test harness's timeout, so it fails, but with a timeout panic instead of the assertion that says what went wrong. Checking the buffer for the event and the buffer's capacity (or a retry limit) would keep the failure readable. Not blocking.

Not checked: the GPU crates' behaviour under a real adapter (no GPU device here); the depth_bounds_pipeline_loads_and_filters_draws test, which the repository's own commands skip on a software adapter.

@lixonik

lixonik commented Dec 1, 2026

Copy link
Copy Markdown
Contributor Author

@KelboLive Thanks for instrumenting the base and catching the actual failure mechanism -- eviction from ROLLING during the thread-spawn + condvar step rather than a lost push. I've updated the PR description to state the root cause accurately (eviction by other tests' events into the shared 65,536-entry ring, plus the has_event panic guard) and explicitly note that shared-ring flooding itself is not eliminated here. Keeping the code untouched so CI and your / @Neutron-0's verification runs stay valid on 4b52062d5.

@Neutron-0 Thanks for running the 10+10 parallel check as well (and confirming the separate alias_cast_tests hit belongs to #2854).

@OreoFR

OreoFR commented Dec 1, 2026

Copy link
Copy Markdown
Collaborator

Merging (added to the merge queue). Head 4b52062 merges cleanly with main c4ca576. Claim: D3D12-TIMELINE-STALL-TEST-PARALLEL (#2570 (comment)). Red/green: KelboLive (the test fails 18/20 diagnostics runs on base, 0/20 with this), Neutron-0 (Windows 11, 10 runs each side). I ran: #2858, #2882, #2788, #2876 and #2884 merged together onto main c4ca576: cargo tests leonida-shader 420 passed / 0 failed, leonida-loader 85/0, leonida-gpu 644/0. In-game run: RTX 4070 Laptop 8 GB, NVIDIA 621.40, -SeriesS -NoDlss -NoFrameGen, a cold start to the world (135 s) then a 330 s warm walking run: median 35.4 ms (28.2 fps), private memory max 24.4 GB, 0 device lost. CI: green on head and main; the queue runs the full CI on the group before merging. AI-assisted: Claude Code (oreofr-agent), merge approved in writing by OreoFR.

@OreoFR
OreoFR added this pull request to the merge queue Dec 1, 2026
Merged via the queue into ProjectLeonida:main with commit 913b88b Dec 1, 2026
6 checks passed
@imShangLi

Copy link
Copy Markdown
Contributor

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

Verdict: PASS on what I could check. The change makes a test that was flaky under parallel runs deterministic, and nothing that passed before fails now. The flakiness itself does not reproduce on this machine, so I could not watch the test go from red to green.

Head 4b52062d, base the current main, merge base b3910e5e, 1 file, +70/−42: d3d12/crates/leonida-gpu/src/timeline/record.rs.

1. What it changes

ROLLING is a global ring the timeline writes into; try_push drops rather than blocks when another thread holds the ring's lock. The global-stall routing test writes a synthetic span through that global producer, and under cargo test's parallel threads an unrelated test thread can hold ROLLING.inner at that moment — so the producer call is dropped and the test fails even though the code is right. The test's own comment said so:

Under parallel cargo test, unrelated test threads may momentarily hold ROLLING.inner, causing an intentional non-blocking contention drop on a single global producer call. Retry our synthetic producer call when the contention counter advances so global-routing tests stay deterministic without weakening the non-blocking producer path.

The change replaces "retry when the contention counter advances" with "retry until the producer call actually took":

fn emit_global_until(pred: impl Fn(&Event) -> bool, mut emit: impl FnMut()) { ... }

and the assertion it guards becomes "the synthetic wake/wait events were not retained in ROLLING" — the test now waits for the events to be there rather than assuming one call was enough.

2. What the tests do here

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 merge base with only this pull request's tests 690 tests
the pull request's head 690 tests
tests that pass on the base and fail on the head 0
failing on both sides 3 — 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

The test count is the same on both sides because the change modifies an existing test (timeline::record::tests::global_stall_routing_retains_spans_waits_wakes_and_sent_fences_without_stacks) rather than adding one.

3. Why there is no red→green to see

The modified test passes on the merge base as well: the contention the retry answers to needs another test thread to hold ROLLING.inner at that exact moment, and on this machine (8 cores, Wine's own timing) it does not happen. So the base's copy of the test passes, the head's copy passes, and the difference the pull request makes — a test that fails sometimes becoming one that never does — is not observable here.

That is the opposite of a defect: the change removes a flake that the upstream's 16-core Windows CI sees. I could not reproduce the flake to measure how often it fires.

4. What I did not check

  • The flake's rate on the machine it was seen on. The change's purpose is to stop it; I can neither reproduce nor measure it here.
  • That the retry cannot hang. emit_global_until waits for the events to appear; if the producer keeps losing the race the test would spin. I did not read its bound.
  • Windows and MSVC. Everything above is Linux with Wine 11.21 (staging) and no GPU device.
  • The rest of the file's changes — the diff also touches the non-test code in timeline/record.rs; I read the test-related part and the has_event/emit_global_until helpers, not every line of the production change.

AI-assisted: MiniMax M3.1 Flash.

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.

6 participants