Skip to content

d3d12: the contended-lock test waits for the holder, not for a thread to start (GPU-TIMELINE-REPORT-WAIT-HANDSHAKE) - #3050

Open
Neutron-0 wants to merge 1 commit into
ProjectLeonida:mainfrom
Neutron-0:fix/timeline-report-wait-handshake
Open

Neutron-0 wants to merge 1 commit into
ProjectLeonida:mainfrom
Neutron-0:fix/timeline-report-wait-handshake

Conversation

@Neutron-0

@Neutron-0 Neutron-0 commented Dec 2, 2026 •

Copy link
Copy Markdown

Claimed in the coordination issue as: GPU-TIMELINE-REPORT-WAIT-HANDSHAKE

What and why

real_waits_are_recorded_with_who_ended_them (d3d12/crates/leonida-gpu/src/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. The failing line is the assert, not the recorder: the recorder had nothing to name because there was no wait.

The test is under the diagnostics feature, so the default --lib run does not compile it; it is the run the layer's own diagnostics build uses.

What changed

  • 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.
  • The test asserts that blocked duration directly (10 ms or more) as well as the waker, so a machine whose clock could not measure it says so at the wait rather than at the attribution.
  • The wait for the holder's word is bounded by two seconds, so a holder that never takes the lock fails the test instead of hanging it.
  • The waits are read from this thread, because this is the thread that waits. The wait for the lock is the last of its waits of 10 ms or more before the join: the gate it waited on in the first half names the first worker, so the first match was that one. The join below is a wait of its own, so the ticks are taken before it and the waits are bounded by them - a slow worker exit makes the join the later wait, and the assert would pick a wait that names no waker.
  • The chain, the work, the stack sites, the JSON and the summary assertions are untouched, and last is still taken before the contention, so none of this enters the chain.
  • No product code changes.

Verification (CPU-only, no game files, no GPU)

On this head, based on main 0ae5a2664:

  • 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) - the flake, every time.
  • This test, with the same 30 ms of start-up delay: ok.
  • This test with 30 ms between the holder's release and its exit, so that the join is a wait of 10 ms or more: ok. Without the bound on the ticks it FAILS, left: None - the bound is what keeps that wait out.
  • This test without either perturbation: 20 of 20 runs ok.
  • Whole diagnostics suite (cargo test -p leonida-gpu --lib --locked --features diagnostics): 2 runs, 749 passed, 0 failed, 59 ignored, each. The stock test failed in this suite 3 times in 40 runs on main 60f7dcd.
  • Teeth: with Trace::waker made to return None, this test fails at the assert, so the wait is still checked for who ended it.
  • Default suite (cargo test -p leonida-gpu --lib --locked): 668 passed, 0 failed, 59 ignored.

What this does not do

It does not serialise the suite and does not touch the other parallel races in this crate (watch.rs, descriptor.rs, texture.rs, pool.rs), which are claimed elsewhere. It changes only this test, so it would also stand if the suite were serialised later: this test's wait is no longer a thing to hide.

Notes

  • No game files and no GPU. The test needs real threads and the wrappers, both of which this machine has.

Checklist

  • Based on current main (0ae5a2664).
  • No game files, no GPU, no network.
  • python -I scripts/check_no_game_data.py clean.
  • Red/green: the stock shape with a delayed start fails; this shape with the same delay passes.

AI-assisted: yes - DeepSeek V4.1 Flash Free (OpenCode); review: a separate read-only OpenCode agent.

@ecox982745

Copy link
Copy Markdown
Contributor

CPU-only review at 82e4a68b9. The handshake fixes a holder that starts late. It still permits the waiter to be descheduled after the holder signals and before the waiter attempts lock.lock() (timeline/report.rs:1463). If the holder runs its 30 ms sleep and releases the lock during that interval, the waiter acquires immediately and the new 10 ms assertion fails. The 30 ms holder-start perturbation exercises a different schedule.

I checked the schedule in a small CPU-only model of the mutex and condvar handoff. Six ordinary attempts waited about 31 ms. Six with the waiter held until the release acquired in at most 0.001 ms. This models the synchronization sequence. The Rust trace test itself was not executed. A contrived delay immediately before the wait timer would exercise the remaining window in the Rust test.

The trace lookup also runs after second.join(). If the holder is descheduled between dropping the lock and returning from the thread, max_by_key(|w| w.t1) can select the join wait. Consider timestamping the lock call with the trace clock and restricting the lookup to that interval. For the minimum-duration assertion, a bounded retry of an attempt where there was no measured lock contention would distinguish an unlucky schedule from attribution failure.

AI-assisted review. No GPU or game files used.

… 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
wait for the word is bounded by two seconds, so a holder that never takes
the lock fails the test instead of hanging it.

The waits are now read from this thread, because this is the thread that
waits: the wait for the lock is the last of its waits of 10 ms or more
before the join. The gate it waited on in the first half names the first
worker, so the first match was that one. The join below is a wait of its
own, so the ticks are taken before it and the waits are bounded by them: a
slow worker exit makes the join the later wait, and the assert would pick a
wait that names no waker. 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 with 30 ms between the holder's release and its exit, so that
  the join is a wait of 10 ms or more: ok. Without the bound on the ticks
  it FAILS, `left: None` - the bound is what keeps that wait out.
- This test without either perturbation: 20 of 20 runs ok.
- Whole diagnostics suite (`cargo test -p leonida-gpu --lib --locked
  --features diagnostics`): 2 runs, 749 passed, 0 failed, 59 ignored, each.
- Default suite (`cargo test -p leonida-gpu --lib --locked`): 668 passed, 0
  failed, 59 ignored.
- 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.
@Neutron-0
Neutron-0 force-pushed the fix/timeline-report-wait-handshake branch from 82e4a68 to 3f6545b Compare December 2, 2026 06:05
@Neutron-0

Copy link
Copy Markdown
Author

One review round found a second way for this test to fail, and it is fixed in this head (3f6545b0b).

The join below is a wait of its own: the wrappers record it like any other wait of this thread. If the worker is slow to exit, that wait is later than the wait for the lock and is at least 10 ms long, so the assertion could pick it - and a join names no waker, so it failed with left: None, the same shape as the flake this pull request removes. The ticks are now taken before the join and the waits are bounded by them, so the wait for the lock is the one that is measured.

Measured here: with 30 ms between the holder's release and its exit, this head passes, and the head without the bound fails with left: None.

The wait for the holder's word is bounded as well (two seconds), so a holder that never takes the lock fails the test instead of hanging it.

On this head: whole diagnostics suite 2 runs, 749 passed and 0 failed each; the test itself 20 of 20 runs; default suite 668 passed, 0 failed.

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.

2 participants