D3D12-TIMELINE-REPORT-WAKER-ISOLATION: synchronize thread startup in timeline::report wait test #2933

Closed
bobcatchris wants to merge 1 commit from upstream-pr/2933 into main
bobcatchris commented 2026-12-01 19:55:52 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by bobcatchris on 2026-12-01T19:55:52Z.


Claimed in the coordination issue as: D3D12-TIMELINE-REPORT-WAKER-ISOLATION (https://github.com/ProjectLeonida/mandy/issues/2570#issuecomment-7323232862; ref finding https://github.com/ProjectLeonida/mandy/issues/2570#issuecomment-7320804801)

What and why

timeline::report::tests::real_waits_are_recorded_with_who_ended_them (d3d12/crates/leonida-gpu/src/timeline/report.rs) intermittently failed under parallel whole-suite runs (reported 3/40 on main) because it assumed newly spawned threads reached their synchronization points within fixed sleeps:

  1. second was spawned while the test thread held lock, slept a fixed 20 ms, and dropped lock. Under parallel test contention, thread creation/startup could consume >10 ms so second either waited less than the 10 ms filter or never contended at all (contended == None).
  2. worker started its timed work span immediately on spawn without waiting for the test thread to acquire gate.0 and enter gate.1.wait.

Fix:

  • Synchronize worker with an unhooked AtomicBool (waiting) set while holding gate.0, then sleep 10 ms before starting the 40 ms timed span so gate.1.wait overlaps the full span.
  • Synchronize second with an unhooked AtomicBool (ready) set immediately before lock.lock(), and hold lock for 50 ms after ready is observed so slow thread startup cannot eat the 10 ms wait filter.
  • Use trace.waits.get(&second).into_iter().flatten() instead of direct map indexing.

Tested

  • Windows 11, Rust 1.98.1, cargo test --locked -p leonida-gpu --lib --features diagnostics timeline::report::tests: 6 passed, 0 failed.
  • Full parallel leonida-gpu lib suite (--features diagnostics -- --skip depth_bounds_pipeline_loads_and_filters_draws), 15 consecutive runs: 15/15 passed, 0 failures in real_waits_are_recorded_with_who_ended_them (724 passed, 0 failed, 59 ignored per run).
  • python -I scripts/check_no_game_data.py d3d12/crates/leonida-gpu/src/timeline/report.rs: clean.

GPUs tested: n/a (CPU-only timeline hook test; Windows 11).

AI-assisted: yes — Gemini 4 Neon (implementation and an independent read-only reviewer pass).

Checklist

  • Based on current main and merges cleanly (a conflicting pull request is labeled needs rebase).
  • One topic; follow-up work goes in a new pull request.
  • No game data, keys or decryption (CONTRIBUTING.md), including in logs or reports quoted here.
<!-- greyfinch-sync:ubiquitarianism/leonida:issue:5593395257 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/2933). Originally posted by [bobcatchris](https://github.com/bobcatchris) on 2026-12-01T19:55:52Z. --- Claimed in the coordination issue as: D3D12-TIMELINE-REPORT-WAKER-ISOLATION (https://github.com/ProjectLeonida/mandy/issues/2570#issuecomment-7323232862; ref finding https://github.com/ProjectLeonida/mandy/issues/2570#issuecomment-7320804801) ### What and why `timeline::report::tests::real_waits_are_recorded_with_who_ended_them` (`d3d12/crates/leonida-gpu/src/timeline/report.rs`) intermittently failed under parallel whole-suite runs (reported 3/40 on `main`) because it assumed newly spawned threads reached their synchronization points within fixed sleeps: 1. `second` was spawned while the test thread held `lock`, slept a fixed 20 ms, and dropped `lock`. Under parallel test contention, thread creation/startup could consume >10 ms so `second` either waited less than the 10 ms filter or never contended at all (`contended == None`). 2. `worker` started its timed work span immediately on spawn without waiting for the test thread to acquire `gate.0` and enter `gate.1.wait`. Fix: - Synchronize `worker` with an unhooked `AtomicBool` (`waiting`) set while holding `gate.0`, then sleep 10 ms before starting the 40 ms timed span so `gate.1.wait` overlaps the full span. - Synchronize `second` with an unhooked `AtomicBool` (`ready`) set immediately before `lock.lock()`, and hold `lock` for 50 ms after `ready` is observed so slow thread startup cannot eat the 10 ms wait filter. - Use `trace.waits.get(&second).into_iter().flatten()` instead of direct map indexing. ### Tested - Windows 11, Rust 1.98.1, `cargo test --locked -p leonida-gpu --lib --features diagnostics timeline::report::tests`: **6 passed, 0 failed**. - Full parallel `leonida-gpu` lib suite (`--features diagnostics -- --skip depth_bounds_pipeline_loads_and_filters_draws`), 15 consecutive runs: **15/15 passed, 0 failures** in `real_waits_are_recorded_with_who_ended_them` (724 passed, 0 failed, 59 ignored per run). - `python -I scripts/check_no_game_data.py d3d12/crates/leonida-gpu/src/timeline/report.rs`: clean. GPUs tested: n/a (CPU-only timeline hook test; Windows 11). AI-assisted: yes — Gemini 4 Neon (implementation and an independent read-only reviewer pass). ### Checklist - [x] Based on current `main` and merges cleanly (a conflicting pull request is labeled `needs rebase`). - [x] One topic; follow-up work goes in a new pull request. - [x] No game data, keys or decryption (CONTRIBUTING.md), including in logs or reports quoted here.
merzleckeier commented 2026-12-01 20:13:09 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by merzleckeier on 2026-12-01T20:13:09Z.


Controlled second-worker scheduling check at 45e0aae5 against 99a29d10 under Win64 Wine 11.22. I inserted the same optional 150 ms delay in the test on both trees. The compared fixture builds carry matching PDBs and mark the test function inline(never) to keep the separate stack-name assertion stable.

Delay placement Main PR
None 3/3 3/3
At second worker entry, before publishing ready 0/3 3/3
Immediately before the mutex, after the PR publishes ready 0/3 0/3

The failures had no matching waker. Delayed startup no longer consumes the holder’s wait window. The deliberately injected stall after ready.store still can. The six-test timeline selection also passed on both fixture builds without an injected delay. An earlier optimized probe of the PR reached the expected waker with delayed startup, then separately failed the symbol-name assertion under Wine. These are isolated scheduling tests. AI-assisted (Codex).

<!-- greyfinch-sync:ubiquitarianism/leonida:comment:7324275252 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/2933#issuecomment-7324275252). Originally posted by [merzleckeier](https://github.com/merzleckeier) on 2026-12-01T20:13:09Z. --- Controlled second-worker scheduling check at `45e0aae5` against `99a29d10` under Win64 Wine 11.22. I inserted the same optional 150 ms delay in the test on both trees. The compared fixture builds carry matching PDBs and mark the test function `inline(never)` to keep the separate stack-name assertion stable. | Delay placement | Main | PR | | --- | ---: | ---: | | None | 3/3 | 3/3 | | At second worker entry, before publishing ready | 0/3 | 3/3 | | Immediately before the mutex, after the PR publishes ready | 0/3 | 0/3 | The failures had no matching waker. Delayed startup no longer consumes the holder’s wait window. The deliberately injected stall after `ready.store` still can. The six-test timeline selection also passed on both fixture builds without an injected delay. An earlier optimized probe of the PR reached the expected waker with delayed startup, then separately failed the symbol-name assertion under Wine. These are isolated scheduling tests. AI-assisted (Codex).
KelboLive commented 2026-12-01 22:00:36 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by KelboLive on 2026-12-01T22:00:36Z.

Review: COMMENTED


Diagnosis and fix both hold up. At 4× oversubscription the parent commit failed this test in 10/24 suite runs (report.rs:1455 "no entry found for key"; :1459 None vs Some(tid)); this branch is 0/24 at 4× and 0/16 at 8×, and 20/20 on the exact timeline::report filter. The flags are sound: one store, acquire load, no lost wakeup, no lock-order issue, and yield_now prevents starvation.

It narrows rather than closes, though: ready is still published before lock.lock() (:1453-1454), so a stall after publication is covered only by the fixed 50 ms hold (40 ms above the 10 ms filter). I injected 60 ms there and it fails 3/3; 60 ms after waiting.store fails the work assertion 2/2. The fix removes the startup-latency charge against the window — which is the real runner behaviour it was fighting — but a scheduling stall inside the window still flakes.

Two suggestions:

  • Drop the >= 10 ms duration filter (:1475) and assert on the wait that ends at the release (the last wait of second) and its waker. That removes one avoidable failure mode and tests the property rather than a duration.
  • Prefer a std::sync::Barrier over the yield_now spin (:1416): if the test thread panics before waiting.store (:1437), the worker spins forever (libtest still exits, but it burns CPU). The pre-existing gate.1.wait already has that hazard; a barrier would remove the class.

The .get(&second).into_iter().flatten() change is a strict improvement over the index panic either way. Merge-tree against current main is clean, and #2884 is complementary (different test, same thread-startup class, mutually exclusive via TEST_GLOBAL_LOCK).

<!-- greyfinch-sync:ubiquitarianism/leonida:review:6140919363 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/2933#pullrequestreview-6140919363). Originally posted by [KelboLive](https://github.com/KelboLive) on 2026-12-01T22:00:36Z. **Review:** COMMENTED --- Diagnosis and fix both hold up. At 4× oversubscription the parent commit failed this test in **10/24** suite runs (`report.rs:1455` "no entry found for key"; `:1459` `None` vs `Some(tid)`); this branch is **0/24** at 4× and **0/16** at 8×, and 20/20 on the exact `timeline::report` filter. The flags are sound: one store, acquire load, no lost wakeup, no lock-order issue, and `yield_now` prevents starvation. It narrows rather than closes, though: `ready` is still published **before** `lock.lock()` (`:1453-1454`), so a stall after publication is covered only by the fixed 50 ms hold (40 ms above the 10 ms filter). I injected 60 ms there and it fails 3/3; 60 ms after `waiting.store` fails the work assertion 2/2. The fix removes the *startup-latency* charge against the window — which is the real runner behaviour it was fighting — but a scheduling stall inside the window still flakes. Two suggestions: - **Drop the `>= 10 ms` duration filter** (`:1475`) and assert on the wait that ends at the release (the last wait of `second`) and its waker. That removes one avoidable failure mode and tests the property rather than a duration. - **Prefer a `std::sync::Barrier` over the `yield_now` spin** (`:1416`): if the test thread panics before `waiting.store` (`:1437`), the worker spins forever (libtest still exits, but it burns CPU). The pre-existing `gate.1.wait` already has that hazard; a barrier would remove the class. The `.get(&second).into_iter().flatten()` change is a strict improvement over the index panic either way. Merge-tree against current main is clean, and #2884 is complementary (different test, same thread-startup class, mutually exclusive via `TEST_GLOBAL_LOCK`).
MoonEyes commented 2026-12-02 12:31:33 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by MoonEyes on 2026-12-02T12:31:33Z.


This pull request touches the render path, so it needs an in-game run before it can be merged: unit tests and CI do not catch rendering regressions (a device loss after an earlier merge is why this gate exists).

Could you, or anyone with a working setup, post a run report on the current head merged onto current main:

  • the head SHA and the main SHA it was merged onto;
  • GPU, driver and OS;
  • at least 300 s in the world after loading;
  • device lost / crashes (count), and rendered fps (median) if you have them;
  • the same run on main alone for comparison, if you can.

Please do not attach logs or screenshots that show game content. If a run on this exact head is already posted, a link to it in a reply is enough.

AI-assisted (Claude Code).

<!-- greyfinch-sync:ubiquitarianism/leonida:comment:7345469546 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/2933#issuecomment-7345469546). Originally posted by [MoonEyes](https://github.com/MoonEyes) on 2026-12-02T12:31:33Z. --- This pull request touches the render path, so it needs an in-game run before it can be merged: unit tests and CI do not catch rendering regressions (a device loss after an earlier merge is why this gate exists). Could you, or anyone with a working setup, post a run report on the **current head** merged onto current `main`: - the head SHA and the `main` SHA it was merged onto; - GPU, driver and OS; - at least 300 s in the world after loading; - device lost / crashes (count), and rendered fps (median) if you have them; - the same run on `main` alone for comparison, if you can. Please do not attach logs or screenshots that show game content. If a run on this exact head is already posted, a link to it in a reply is enough. AI-assisted (Claude Code).
MoonEyes commented 2026-12-02 15:53:24 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by MoonEyes on 2026-12-02T15:53:24Z.


RUN (in game, batch of six render-path pull requests), each head merged with git merge --no-ff onto main 83046a713, in one build: #2933 45e0aae53, #2831 4375de941, #2840 4db84a195, #2887 3be77fdeb, #2855 5e4cd9bcb, #2995 9e1e95930. RTX 4060 8 GB, driver 610.88, Windows 11. Series S, -Resolution 1080, frame generation 2x, the same walking route as the other runs on this machine, 330 s after reaching the world.

What Result
Device lost / device removed 0
Exceptions, panics, hang watchdog, GPU stall, title suspended 0
Process alive at the end yes, stopped by the time limit
Rendered fps, last 240 s (48 samples) median 16.7, mean 17.0
Rendered fps, last 120 s median 16.9

For reference, the same machine and route rendered 13.3 to 15.2 fps today on main with single pull requests and 14.9 with the first batch; no compile ran beside the game this time. These fps are a check that nothing fell over, not a measurement of a gain.

Verdict: PASS for the in-game gate (300 s or more in the world, no device loss) for this batch. If one of these pull requests changes its head, this run no longer covers it.

Limits: one run; free system memory was low (median 1.3 GB); arrival in the world was driven by keys and not checked on pictures.

AI-assisted (Claude Code).

<!-- greyfinch-sync:ubiquitarianism/leonida:comment:7351096598 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/2933#issuecomment-7351096598). Originally posted by [MoonEyes](https://github.com/MoonEyes) on 2026-12-02T15:53:24Z. --- RUN (in game, batch of six render-path pull requests), each head merged with `git merge --no-ff` onto main `83046a713`, in one build: #2933 `45e0aae53`, #2831 `4375de941`, #2840 `4db84a195`, #2887 `3be77fdeb`, #2855 `5e4cd9bcb`, #2995 `9e1e95930`. RTX 4060 8 GB, driver 610.88, Windows 11. Series S, `-Resolution 1080`, frame generation 2x, the same walking route as the other runs on this machine, 330 s after reaching the world. | What | Result | |---|---| | Device lost / device removed | 0 | | Exceptions, panics, hang watchdog, GPU stall, title suspended | 0 | | Process alive at the end | yes, stopped by the time limit | | Rendered fps, last 240 s (48 samples) | median 16.7, mean 17.0 | | Rendered fps, last 120 s | median 16.9 | For reference, the same machine and route rendered 13.3 to 15.2 fps today on main with single pull requests and 14.9 with the first batch; no compile ran beside the game this time. These fps are a check that nothing fell over, not a measurement of a gain. Verdict: PASS for the in-game gate (300 s or more in the world, no device loss) for this batch. If one of these pull requests changes its head, this run no longer covers it. Limits: one run; free system memory was low (median 1.3 GB); arrival in the world was driven by keys and not checked on pictures. AI-assisted (Claude Code).
MoonEyes commented 2026-12-02 18:00:56 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by MoonEyes on 2026-12-02T18:00:56Z.


Code review of head 45e0aae53 (merge base ab374c7d5, merges cleanly into current main 45dc41f95). Not built: Windows-only test. Superseded in substance by #3050.

1 file: d3d12/crates/leonida-gpu/src/timeline/report.rs  +26/-8 (test only)

Both this PR and #3050 change the same test, timeline::report::tests::real_waits_are_recorded_with_who_ended_them, under different claim IDs (D3D12-TIMELINE-REPORT-WAKER-ISOLATION here, GPU-TIMELINE-REPORT-WAIT-HANDSHAKE there). They cannot both land as written.

Remaining race in this head (report.rs, contended-lock block): the second thread stores ready = true and only then calls lock.lock(). The test thread sees ready, sleeps 50 ms and drops the lock. If second is descheduled between the store and the lock call for more than ~40 ms (the loaded-runner case this PR is about), it takes the lock uncontended and the >= 10 ms assertion fails with None, the same shape as the original flake. The change narrows the window (thread start no longer counts) but still bets on the scheduler, now with 50 ms instead of 20. The first block adds a 10 ms sleep and a waiting flag with the same pattern.

#3050's current head (1c1f92f80) closes this window by having the holder wait on the recorder's own stood_at() counter, which is set inside the hooked wait, before it starts the timed hold. @merzleckeier's Wine run in this thread with a forced delay is consistent with this head being better than main, not with it being race-free.

The .get(&second).into_iter().flatten() change is good (a missing entry now fails the assertion with a message rather than panicking on the index) and could be folded into #3050 if not already covered there.

In-game batch run by @MoonEyes on this head showed no regressions, as expected for a test-only change.

Verdict: superseded by #3050 (same test, stronger fix); suggest closing this one or merging its unique part into #3050.

AI-assisted verification: Claude Opus (Claude Code), separate agent per batch.


Generated by Claude Code

<!-- greyfinch-sync:ubiquitarianism/leonida:comment:7353862345 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/2933#issuecomment-7353862345). Originally posted by [MoonEyes](https://github.com/MoonEyes) on 2026-12-02T18:00:56Z. --- **Code review of head `45e0aae53` (merge base `ab374c7d5`, merges cleanly into current `main` `45dc41f95`). Not built: Windows-only test. Superseded in substance by #3050.** ``` 1 file: d3d12/crates/leonida-gpu/src/timeline/report.rs +26/-8 (test only) ``` Both this PR and #3050 change the same test, `timeline::report::tests::real_waits_are_recorded_with_who_ended_them`, under different claim IDs (`D3D12-TIMELINE-REPORT-WAKER-ISOLATION` here, `GPU-TIMELINE-REPORT-WAIT-HANDSHAKE` there). They cannot both land as written. **Remaining race in this head** (`report.rs`, contended-lock block): the `second` thread stores `ready = true` and only then calls `lock.lock()`. The test thread sees `ready`, sleeps 50 ms and drops the lock. If `second` is descheduled between the store and the lock call for more than ~40 ms (the loaded-runner case this PR is about), it takes the lock uncontended and the `>= 10 ms` assertion fails with `None`, the same shape as the original flake. The change narrows the window (thread start no longer counts) but still bets on the scheduler, now with 50 ms instead of 20. The first block adds a 10 ms sleep and a `waiting` flag with the same pattern. #3050's current head (`1c1f92f80`) closes this window by having the holder wait on the recorder's own `stood_at()` counter, which is set inside the hooked wait, before it starts the timed hold. @merzleckeier's Wine run in this thread with a forced delay is consistent with this head being better than `main`, not with it being race-free. The `.get(&second).into_iter().flatten()` change is good (a missing entry now fails the assertion with a message rather than panicking on the index) and could be folded into #3050 if not already covered there. In-game batch run by @MoonEyes on this head showed no regressions, as expected for a test-only change. **Verdict: superseded** by #3050 (same test, stronger fix); suggest closing this one or merging its unique part into #3050. AI-assisted verification: Claude Opus (Claude Code), separate agent per batch. --- _Generated by [Claude Code](https://claude.ai/code)_
bobcatchris commented 2026-12-02 21:02:37 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by bobcatchris on 2026-12-02T21:02:37Z.


Closing as superseded by #3050 (GPU-TIMELINE-REPORT-WAIT-HANDSHAKE).

Thank you @MoonEyes, @KelboLive, and @merzleckeier for identifying and reproducing the remaining post-ready.store scheduling window; #3050's stood_at() handshake closes that race properly and already guards trace.waits.get(...) without indexing panic.

AI-assisted: Gemini 4 Neon

<!-- greyfinch-sync:ubiquitarianism/leonida:comment:7358705860 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/2933#issuecomment-7358705860). Originally posted by [bobcatchris](https://github.com/bobcatchris) on 2026-12-02T21:02:37Z. --- Closing as superseded by `#3050` (`GPU-TIMELINE-REPORT-WAIT-HANDSHAKE`). Thank you `@MoonEyes`, `@KelboLive`, and `@merzleckeier` for identifying and reproducing the remaining post-`ready.store` scheduling window; `#3050`'s `stood_at()` handshake closes that race properly and already guards `trace.waits.get(...)` without indexing panic. AI-assisted: Gemini 4 Neon

Pull request closed

Sign in to join this conversation.
No reviewers
No labels
No milestone
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Reference
ProjectLeonida/mandy!2933
No description provided.