GPU: a frame wait takes its tick before it sleeps #174

Merged
AndyLatimer merged 2 commits from upstream-pr/174 into main 2026-11-25 00:18:54 +00:00
AndyLatimer commented 2026-11-24 01:06:04 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by AndyLatimer on 2026-11-24T01:06:04Z.


The frame.rs module doc says each event kind (origin, vblank, flip) happens once per tick, so two waits for the same kind never end on the same tick. wait picked the next tick in next_tick under the clock's lock, but marked it as taken (LAST[kind]) only after std::thread::sleep, with the lock released. Two threads waiting for one kind at the same time therefore both read the same LAST, both got tick T, and both returned S_OK on it. For the origin, that begins two frames (FRAME += 2) in one interval.

next_tick now takes a limit (the wait's time-out). When the tick comes within it, next_tick records the tick under the clock's lock, before anyone sleeps. A wait that times out takes no tick, as before. Sequential waits behave as before.

Testing

  • cargo test --workspace: 265 passed, 4 ignored (need external files). Includes 1 new test, waits_begun_together_take_different_ticks: two next_tick calls before any sleep get different ticks, and a timed-out call leaves LAST alone. It fails on the old behaviour ("1 after 1"), checked by putting it back.
  • cargo clippy --workspace --all-targets: no new warnings. The two that remain (xmem.rs and host.rs:267) are on main.
  • cargo xtask policy: pass. rustfmt --check on frame.rs: clean.
  • Tested with a synthetic unit test only (not run against a title).

🤖 Generated with Claude Code

<!-- greyfinch-sync:Marvisak/VIctory:issue:6572241060 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/174). Originally posted by [AndyLatimer](https://github.com/AndyLatimer) on 2026-11-24T01:06:04Z. --- The `frame.rs` module doc says each event kind (origin, vblank, flip) happens once per tick, so two waits for the same kind never end on the same tick. `wait` picked the next tick in `next_tick` under the clock's lock, but marked it as taken (`LAST[kind]`) only after `std::thread::sleep`, with the lock released. Two threads waiting for one kind at the same time therefore both read the same `LAST`, both got tick T, and both returned `S_OK` on it. For the origin, that begins two frames (`FRAME` += 2) in one interval. `next_tick` now takes a `limit` (the wait's time-out). When the tick comes within it, `next_tick` records the tick under the clock's lock, before anyone sleeps. A wait that times out takes no tick, as before. Sequential waits behave as before. ## Testing - `cargo test --workspace`: 265 passed, 4 ignored (need external files). Includes 1 new test, `waits_begun_together_take_different_ticks`: two `next_tick` calls before any sleep get different ticks, and a timed-out call leaves `LAST` alone. It fails on the old behaviour ("1 after 1"), checked by putting it back. - `cargo clippy --workspace --all-targets`: no new warnings. The two that remain (`xmem.rs` and `host.rs:267`) are on `main`. - `cargo xtask policy`: pass. `rustfmt --check` on `frame.rs`: clean. - Tested with a synthetic unit test only (not run against a title). 🤖 Generated with [Claude Code](https://claude.com/claude-code)
DaviTostes commented 2026-11-24 06:54:55 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by DaviTostes on 2026-11-24T01:21:42Z.


Tested 810f442 under Wine 11.22 + vkd3d-proton 3.1 on Arch Linux x86_64 (AMD RX 9060 XT, RADV 26.3.1), Windows build with cargo xwin ... --target x86_64-pc-windows-msvc and a native Microsoft d3dcompiler_47: pass, no regression.

  • victory_gpu: 42 passed, 2 failed, 1 ignored, including the 1 test this PR adds.
  • The two failures are the environment-only a_rejected_blob_gives_an_empty_library (vkd3d needs a non-empty blob) and #128.
  • The ignored test is host::batch::texture_checks_per_submission (benchmark).

No game data used.

<!-- greyfinch-sync:Marvisak/VIctory:comment:7119023211 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/174#issuecomment-7119023211). Originally posted by [DaviTostes](https://github.com/DaviTostes) on 2026-11-24T01:21:42Z. --- Tested `810f442` under **Wine 11.22 + vkd3d-proton 3.1** on Arch Linux x86_64 (AMD RX 9060 XT, RADV 26.3.1), Windows build with `cargo xwin ... --target x86_64-pc-windows-msvc` and a native Microsoft `d3dcompiler_47`: **pass, no regression**. - `victory_gpu`: **42 passed, 2 failed, 1 ignored**, including the 1 test this PR adds. - The two failures are the environment-only `a_rejected_blob_gives_an_empty_library` (vkd3d needs a non-empty blob) and #128. - The ignored test is `host::batch::texture_checks_per_submission` (benchmark). No game data used.
Sunkiller-qc commented 2026-11-24 06:54:57 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by Sunkiller-qc on 2026-11-24T01:29:25Z.


The diagnostics build no longer compiles with this change. wait now drops the tick (let (_, wait) = next_tick(kind, limit); at crates/victory-gpu/src/frame.rs:133), but the #[cfg(feature = "diagnostics")] tracing::debug! further down still logs tick (frame.rs:156), so the diag build fails with E0425. Keeping the binding (let (tick, wait) = ...) fixes it. The logic change itself looks right to me.

Static review, not built or run.

<!-- greyfinch-sync:Marvisak/VIctory:comment:7119149839 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/174#issuecomment-7119149839). Originally posted by [Sunkiller-qc](https://github.com/Sunkiller-qc) on 2026-11-24T01:29:25Z. --- The `diagnostics` build no longer compiles with this change. `wait` now drops the tick (`let (_, wait) = next_tick(kind, limit);` at `crates/victory-gpu/src/frame.rs:133`), but the `#[cfg(feature = "diagnostics")]` `tracing::debug!` further down still logs `tick` (`frame.rs:156`), so the `diag` build fails with E0425. Keeping the binding (`let (tick, wait) = ...`) fixes it. The logic change itself looks right to me. Static review, not built or run.
r4tedx commented 2026-11-24 06:55:25 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by r4tedx on 2026-11-24T03:29:46Z.


Review note: merged onto current main (d2a4bae), the default build is fine but the diagnostics build does not compile:

error[E0425]: cannot find value `tick` in this scope
   --> crates\victory-gpu\src\frame.rs:156:9

(cargo check -p victory-gpu --features diagnostics; main alone builds with that feature.) wait no longer binds tick, and the tracing call under diagnostics still names it. Testers use the diagnostics build for device-removal reports, so it is worth fixing before this goes in.

From building only; I have no copy of the title.

<!-- greyfinch-sync:Marvisak/VIctory:comment:7121151889 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/174#issuecomment-7121151889). Originally posted by [r4tedx](https://github.com/r4tedx) on 2026-11-24T03:29:46Z. --- Review note: merged onto current `main` (`d2a4bae`), the default build is fine but the diagnostics build does not compile: ``` error[E0425]: cannot find value `tick` in this scope --> crates\victory-gpu\src\frame.rs:156:9 ``` (`cargo check -p victory-gpu --features diagnostics`; `main` alone builds with that feature.) `wait` no longer binds `tick`, and the tracing call under `diagnostics` still names it. Testers use the diagnostics build for device-removal reports, so it is worth fixing before this goes in. From building only; I have no copy of the title.
AndyLatimer commented 2026-11-24 08:02:55 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by AndyLatimer on 2026-11-24T07:46:09Z.


Thanks @Sunkiller-qc and @r4tedx, you are right. Fixed on the new head 882664e: wait binds the tick again for the diagnostics log (with unused_variables allowed when the feature is off). cargo check -p victory-gpu now passes with and without --features diagnostics, no warnings, and the frame tests pass.

<!-- greyfinch-sync:Marvisak/VIctory:comment:7125350518 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/174#issuecomment-7125350518). Originally posted by [AndyLatimer](https://github.com/AndyLatimer) on 2026-11-24T07:46:09Z. --- Thanks @Sunkiller-qc and @r4tedx, you are right. Fixed on the new head `882664e`: `wait` binds the tick again for the `diagnostics` log (with `unused_variables` allowed when the feature is off). `cargo check -p victory-gpu` now passes with and without `--features diagnostics`, no warnings, and the frame tests pass.
mercuryVM commented 2026-11-24 08:26:53 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by mercuryVM on 2026-11-24T08:26:53Z.

Review: APPROVED


Tested 882664e on Windows 11, RTX 5070, MSVC: cargo test --workspace --no-fail-fast passes (265 passed, 0 failed), including waits_begun_together_take_different_ticks.

The race is real. next_tick chose the tick under the clock lock, but LAST was only bumped after the sleep, with the lock released, so two concurrent waits for one kind could both end on tick T (two FRAME increments for the origin). Taking the tick inside next_tick when wait <= limit, and taking none on a time-out, fixes it.

Interaction with #319, which touches the same function. #319 adds an early return to next_tick for a late non-origin wait (return (passed, Duration::ZERO)) and relies on wait to mark that tick afterwards. This PR removes the mark in wait and adds its own mark only at the end of next_tick, so with both applied the early-return path never takes its tick. Merging this onto a branch with #319 confirms it: frame::tests::waits_end_on_ticks_and_count_frames fails at frame.rs:254 (the LAST == flipped + 1 check). Git reports no textual conflict, so this would slip through.

Fix for whichever lands second: in #319's early-return branch, take the tick before returning, i.e. last(kind).fetch_max(passed, Ordering::Relaxed); return (passed, Duration::ZERO);. Its limit check is trivially true, since the wait is zero.

Approving this PR's own change. I have not merged it into our fork, which already has #319.

AI-assisted (Claude Code).

<!-- greyfinch-sync:Marvisak/VIctory:review:6035899844 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/174#pullrequestreview-6035899844). Originally posted by [mercuryVM](https://github.com/mercuryVM) on 2026-11-24T08:26:53Z. **Review:** APPROVED --- Tested 882664e on Windows 11, RTX 5070, MSVC: `cargo test --workspace --no-fail-fast` passes (265 passed, 0 failed), including `waits_begun_together_take_different_ticks`. The race is real. `next_tick` chose the tick under the clock lock, but `LAST` was only bumped after the sleep, with the lock released, so two concurrent waits for one kind could both end on tick T (two `FRAME` increments for the origin). Taking the tick inside `next_tick` when `wait <= limit`, and taking none on a time-out, fixes it. **Interaction with #319, which touches the same function.** #319 adds an early return to `next_tick` for a late non-origin wait (`return (passed, Duration::ZERO)`) and relies on `wait` to mark that tick afterwards. This PR removes the mark in `wait` and adds its own mark only at the end of `next_tick`, so with both applied the early-return path never takes its tick. Merging this onto a branch with #319 confirms it: `frame::tests::waits_end_on_ticks_and_count_frames` fails at `frame.rs:254` (the `LAST == flipped + 1` check). Git reports no textual conflict, so this would slip through. Fix for whichever lands second: in #319's early-return branch, take the tick before returning, i.e. `last(kind).fetch_max(passed, Ordering::Relaxed); return (passed, Duration::ZERO);`. Its `limit` check is trivially true, since the wait is zero. Approving this PR's own change. I have not merged it into our fork, which already has #319. AI-assisted (Claude Code).
alexdeliu commented 2026-11-24 19:04:25 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by alexdeliu on 2026-11-24T18:45:49Z.


Heads-up: #319 (b36bd4c1) and #174 (882664e1) each pass on main, but merged together frame::tests::waits_end_on_ticks_and_count_frames fails on every run (left: 10, right: 11 at frame.rs:254).

#174 moves taking the tick (last(kind).fetch_max) into next_tick. #319 adds an early return (passed, Duration::ZERO) for a late flip wait, before that point, so the late wait's tick is never taken. Taking it in the early return too fixes it. The tests pass (710 in the merge train):

if kind != ORIGIN && passed > last_tick && passed > c.first {
    last(kind).fetch_max(passed, Ordering::Relaxed);
    return (passed, Duration::ZERO);
}

Whichever of the two lands second needs this. It is in e3f72c57 on the train branch.

<!-- greyfinch-sync:Marvisak/VIctory:comment:7141117307 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/174#issuecomment-7141117307). Originally posted by [alexdeliu](https://github.com/alexdeliu) on 2026-11-24T18:45:49Z. --- Heads-up: #319 (`b36bd4c1`) and #174 (`882664e1`) each pass on `main`, but merged together `frame::tests::waits_end_on_ticks_and_count_frames` fails on every run (`left: 10, right: 11` at `frame.rs:254`). #174 moves taking the tick (`last(kind).fetch_max`) into `next_tick`. #319 adds an early `return (passed, Duration::ZERO)` for a late flip wait, before that point, so the late wait's tick is never taken. Taking it in the early return too fixes it. The tests pass (710 in the merge train): ```rust if kind != ORIGIN && passed > last_tick && passed > c.first { last(kind).fetch_max(passed, Ordering::Relaxed); return (passed, Duration::ZERO); } ``` Whichever of the two lands second needs this. It is in [`e3f72c57`](https://github.com/alexdeliu/VIctory/commit/e3f72c5768173be7a1cf44ceaecffc3a685d6b56) on the train branch.
upstream-sync closed this pull request 2026-11-25 01:02:54 +00:00
Marvisak commented 2026-11-25 01:07:41 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by Marvisak on 2026-11-25T00:21:01Z.


Automated triage for the maintainers.

Merge candidate. In crates/victory-gpu/src/frame.rs, wait chose its tick in next_tick under the clock lock but recorded it in LAST only after sleeping, so two threads waiting for the same kind could both end on one tick (for the origin wait, two frames in one interval). next_tick now takes the time-out and records the tick under the lock when the wait will not time out; timed-out waits still take no tick, and single-thread behaviour is unchanged. The diagnostics build issue raised in review is fixed on this head, and the new test fails on the old order.

Note for whoever merges: #319 adds an early return in next_tick that would also need to record its tick, so whichever lands second needs that line.

CI: no check runs on the head commit.


Generated by Claude Code

<!-- greyfinch-sync:Marvisak/VIctory:comment:7148416699 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/174#issuecomment-7148416699). Originally posted by [Marvisak](https://github.com/Marvisak) on 2026-11-25T00:21:01Z. --- <!-- victory-triage v1 head=882664e13cfa557fb46d41e182c3267b01097485 verdict=merge-candidate at=2026-11-25T00:18:15Z --> Automated triage for the maintainers. **Merge candidate.** In `crates/victory-gpu/src/frame.rs`, `wait` chose its tick in `next_tick` under the clock lock but recorded it in `LAST` only after sleeping, so two threads waiting for the same kind could both end on one tick (for the origin wait, two frames in one interval). `next_tick` now takes the time-out and records the tick under the lock when the wait will not time out; timed-out waits still take no tick, and single-thread behaviour is unchanged. The `diagnostics` build issue raised in review is fixed on this head, and the new test fails on the old order. Note for whoever merges: #319 adds an early return in `next_tick` that would also need to record its tick, so whichever lands second needs that line. CI: no check runs on the head commit. --- _Generated by [Claude Code](https://claude.ai/code)_
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
Marvisak/VIctory!174
No description provided.