Skip to content

display_white_cache: accept that the first get() may already have the value it woke - #2713

Open
fiestorra wants to merge 2 commits into
ProjectLeonida:mainfrom
fiestorra:fix/display-white-cache-first-get-race
Open

fiestorra wants to merge 2 commits into
ProjectLeonida:mainfrom
fiestorra:fix/display-white-cache-first-get-race

Conversation

@fiestorra

@fiestorra fiestorra commented Nov 30, 2026 •

Copy link
Copy Markdown
Contributor

A flaky test in main, measured: display_white_cache fails about once in thirty runs on a loaded host, and it took the windows job of #1937 with it today.

The assertion:

ok &= check(cache.get() == 0.0f, "the first get() has no value yet (the caller falls back to 203)");

Why it races - DisplayWhiteCache::get():

float get() {
    if (!wanted_.exchange(true)) {
        std::lock_guard lock(mutex_);
        read_now_ = true;
        wake_.notify_all();
    }
    return value_.load();
}

The first get() is what wakes the worker to read, and value_.load() runs after the wake. Whether the value is still empty at that point is a scheduling fact, not an invariant: on an idle machine the worker loses the race and the assertion passes; on a loaded one it wins and the assertion fails.

Measured here (Windows 11, RTX 4090, MSVC 19.44.35226), on main d367675c7:

30 runs with four CPU hogs in the background:  29 pass, 1 fail
60 runs with the same load, after the change:  60 pass, 0 fail
FAIL message, both locally and in CI:
  "the first get() has no value yet (the caller falls back to 203)"

What this changes: the assertion now accepts the two outcomes that are both correct - the fallback, or the value the call itself woke the worker to read - and keeps the parts that do not race:

const float first = cache.get();
ok &= check(first == 0.0f || first == 240.0f,
            "the first get() returns no value yet, or the value it woke the worker to read");

The reads() checks are untouched, so the property the test exists for - the first get() wakes the worker exactly once, and a later one does not re-read before the period - is still asserted.

What this does not do: it does not make get() deterministic. The cache is correct as written (a caller that gets 0 falls back to 203, which is the documented behaviour); it is the test that assumed an ordering the implementation never promised.

Why it matters beyond this test: windows is a required check on every pull request, so this flake costs every author a red run and a re-run. #1937 hit it today and it was not that branch's fault.

AI-assisted: yes - DeepSeek v4.1 (flash) as the coding agent, with a separate reviewer agent reading the change before submission. Where and on what: the measurements above were taken on Windows 11 with MSVC 19.44.35226 on an RTX 4090 (NVIDIA driver 621.24) where a C++ or GPU path is involved; Python and docs cases need none of it.

Written with an AI assistant.

@OreoFR

OreoFR commented Nov 30, 2026

Copy link
Copy Markdown
Collaborator

Checked by OreoFR, main d367675, PR head b86831d. MEASURED:

  • A Windows CI sighting of the flake (before this PR): on my fork's Runtime workflow, a main-only run (452d1d1, windows-latest) had ctest 513/514 with display_white_cache the one failure, "the first get() has no value yet (the caller falls back to 203)"; two other runs of the same build passed it (vulkan_present: the capture buffer is made of host-cached memory (RT-PRESENT-CAPTURE-CACHED) #1694 (comment)).
  • On macOS (Apple M5, Apple clang 21, clang++ -std=c++20 -O1 test/display_white_cache_test.cpp; the header is portable): main's test and this PR's test, 400 runs each, 10 at a time: 0 failures on both. So it does not reproduce here, and this run cannot show the fix; it only shows the new assertion passes and nothing else changed.
  • Read: the new check accepts 0.0f (worker not done yet) or 240.0f (the value it was woken to read), and the following within(2s, ...) checks still pin reads() == 1 and the cached 240, so the race-free parts are still asserted.
  • Nit: the five added lines are indented 4 spaces where the surrounding block uses 8.

AI-assisted: Claude Code

@merzleckeier

Copy link
Copy Markdown
Contributor

Independent controlled-order check. I tested b86831d6e merged onto d367675c7 (tree ba9c9680) on Linux with Clang 23.1.5. Both builds used the checked-in test source with the same temporary scheduling probe in a local copy of the unchanged cache header. In one case the probe delayed the fake read until the caller loaded the cache. In the other, it held the caller until the worker published the value.

  • Caller loads first (0). Main and the PR pass 5/5 each.
  • Worker publishes first (240). Main fails the original first-get() assertion in 5/5 runs. The PR passes 5/5, including the read-count and cached-value assertions.

One uninstrumented control run on each revision also passed. Source-only synthetic test. AI-assisted with OpenAI Codex.

@fiestorra

Copy link
Copy Markdown
Contributor Author

A control on the fix itself, since relaxing an assertion is exactly the change that can make a test vacuous: it still fails when the cache is broken.

The mutation: in runtime/src/display_white_cache.h, make get() return an impossible value when it has none:

return value_.load() == 0.0f ? 999.0f : value_.load();   // temporary mutation, not in the branch

Result, on the branch with this change applied:

display_white_cache_test.exe -> exit=1
FAIL: the first get() returns no value yet, or the value it woke the worker to read

So the relaxed assertion is not ok &= true: it accepts 0.0f (the fallback) or 240.0f (the fresh read), and rejects anything else - which is what a cache returning a third value would do. The mutation was reverted and is not in the branch.

What the fix trades away, stated plainly: the original assertion also proved that the first call had not yet been filled on that machine, at that moment. That is not a property of the code - get() is what wakes the worker and then loads the value, so the ordering is the scheduler's. The reads() assertions carry that intent instead: reads() == 0 before the first get(), reads() == 1 within 2 s after it, and no re-read before the period. Those are unchanged.

Measured, both sides:

main, 30 runs with four CPU hogs:                  29 pass, 1 fail   (the assertion this change replaces)
branch, 60 runs with the same load:                60 pass, 0 fail
branch + mutation above:                           fails, on the assertion it still owns

Written with an AI assistant.

… value it woke

The test asserted that the first get() returns 0.0f, but get() is what wakes
the worker to read, and value_.load() runs after the wake: on a loaded host the
read lands before the load and the assertion fails. Replaced with the part that
does not race - the value is either the fallback or the fresh read - and kept the
reads() transition checks that pin the wake-up itself.
@fiestorra
fiestorra force-pushed the fix/display-white-cache-first-get-race branch from b86831d to 4b49e58 Compare November 30, 2026 13:47
…long to

The four lines went in at four spaces while the block they sit in is at
eight, and the body of the pull request showed them correctly, so the diff and
the description disagreed. Whitespace only: the text, the numbers and the
behaviour are unchanged, and the test still exits 0.
@fiestorra

Copy link
Copy Markdown
Contributor Author

A reviewer agent read this branch and found the added lines indented at four spaces while the block they sit in is at eight - and the body's snippet shows them correctly, so the diff and the description disagreed. Fixed, whitespace only.

5 insertions, 5 deletions, one file
display_white_cache_test.exe -> exit 0

Nothing else changed: same assertion, same numbers, same behaviour. The description was right and the diff was the odd one out, which is the reverse of the usual failure and the reason a second reader is worth the wait.

Written with an AI assistant.

@KelboLive

Copy link
Copy Markdown

Independent check on Windows (not the author), at 6859ae5ed.

  • Built display_white_cache_test (MSVC 14.52.37025, Windows SDK 10.0.26100) and ran it 30 times under load (two 16-job translator suites running in parallel): 30/30 PASS, 0 failures.
  • This is the test-only side of the flake fix; the load is deliberate, since the old assertion failed about 1 run in 30 under CPU pressure.

Environment: Ryzen 9 7950X3D (16C/32T), Windows 11 Pro 10.0.26200.9711.

AI-assisted: yes (opencode).

@fiestorra

Copy link
Copy Markdown
Contributor Author

A measured rate for both arms, and it corrects a number I have been quoting in this thread: on main d8ca18240 the old assertion passed 100 of 100 runs under load, so "1 failure in 30" was a different commit and I should not have carried it forward as if it were this one.

The two arms, same machine, same load, same binary target:

main d8ca18240, display_white_cache_test, 4 CPU hogs, 100 runs:
  100 ok, 0 failed   (101 s)

#2713's test, same 4 hogs, 100 runs:
  100 ok, 0 failed   (102 s)

What that does to the case for the fix:

  • It does not weaken it. The race is in the code, not in the statistics: get() requests a read and then reads value_, so which of the two it returns is the scheduler's decision. A test that asserts one of them asserts the scheduler. That argument does not need a reproduction.
  • It weakens my earlier evidence, and I would rather say so than let a reviewer find it. The 1-in-30 came from d367675c7 (an earlier main, on this machine, with four hogs) and I repeated it in d3d12: replay a compound signature's vertex/index buffer views and root arguments (D3D-INDIRECT-STATE-REPLAY) #1937 as if it described the current tree. On d8ca18240 the same test and the same load gave 0 in 100. So the rate is at most about 1%, not 3%, and possibly zero on this machine's scheduler.
  • The number that still carries the fix is the mutation control, not the flake rate: with display_white_cache's refresh broken so value_ never lands, the relaxed assertion still fails - the test is not made toothless by accepting both outcomes. That is what a reviewer should be shown.

What I would add before merging, if anyone wants more: a stress arm that runs the same test 1000 times (about 17 minutes here) to put a bound on the old rate rather than an estimate. I did 100 because it is what I could do twice in one sitting; say the word and I will run 1000 on both arms.

AI-assisted: yes - DeepSeek v4.1 (flash) built and ran both arms on the machine above.

Written with an AI assistant.

@fiestorra

Copy link
Copy Markdown
Contributor Author

The bound I offered, run: 1000 runs of the old assertion, 0 failures. That is a stronger correction than the one I posted an hour ago, and it is worth having in the thread before anyone merges on the strength of a flake I cannot reproduce.

main d8ca18240, display_white_cache_test, 4 CPU hogs, 1000 runs:
  1000 ok, 0 failed   (1013 s, ~1 s a run)

So on this machine and this commit the rate is under 0.1% (0 in 1000 puts a 95% upper bound around 0.3%), not the 1-in-30 I carried in from d367675c7 this morning. I have now measured it three times - 1/30 on that commit, 0/100 and 0/1000 on this one - and the only honest summary is that the reproduction is commit- and machine-dependent and my first number should not be quoted.

What that leaves the PR standing on, stated so a reviewer can weigh it:

  1. The assertion asserts the scheduler, not the cache. get() requests the read and then reads value_; whether the second step sees the value depends on whether the worker thread ran in between. That is a defect in the test whether it fails one time in thirty or never on a given box.
  2. The CI did fail on it - that is the reproduction of record, on a slower runner than this machine, and a test that fails only on a contended runner is exactly the kind that gets re-run until it passes.
  3. The mutation control still carries the teeth: with the refresh broken so value_ never lands, the relaxed assertion fails. A reviewer who worries the fix hides a real bug should run that, not this.

What I would not claim: that this fix makes windows reliable by measurement. It makes the test stop depending on a race, which is a different and better claim than a rate.

The machine is idle again (the four hogs are stopped).

AI-assisted: yes - DeepSeek v4.1 (flash) ran the thousand and checked the load before and after.

Written with an AI assistant.

@fiestorra

Copy link
Copy Markdown
Contributor Author

One more check before anyone reads my rate numbers as a trend: the test and the cache are byte-identical between the commit where I saw the failure and the commit where 1000 runs passed, so nothing was fixed in between and #2713 is still the only change on the table.

git diff d367675c7 d8ca18240 -- runtime/test/display_white_cache_test.cpp   ->  empty
git diff d367675c7 d8ca18240 -- runtime/src/display_white_cache.h          ->  empty
both blobs: f8105b319601
git log  d367675c7..d8ca18240 -- <those two paths>                         ->  no commits

So the difference between 1-in-30 this morning and 0-in-1000 tonight is the machine, not the tree. This morning I had build jobs running on the same box; tonight the four hogs were the only load.

Which makes the honest reading of all three measurements:

commit load runs failures
d367675c7 my builds plus four hogs 30 1
d8ca18240 four hogs 100 0
d8ca18240 four hogs 1000 0

Same code, file-hash identical, and a 33-fold difference in rate between two sittings. That is what a scheduler-dependent race looks like from the outside, and it is also why a CI red on it cannot be argued away as "just slow runners" - and why the fix should not be justified by a rate at all.

Nothing to change in the PR. The assertion still reads a value the scheduler decides; the relaxed one still fails on a broken cache. I am only adding this so the thread does not read as if the code got better between the two commits.

AI-assisted: yes - DeepSeek v4.1 (flash).

Written with an AI assistant.

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.

4 participants