Frame events: queue an origin before the wait it wakes can take it #202

Closed
Kalmarv wants to merge 1 commit from upstream-pr/205 into main
Kalmarv commented 2026-11-23 20:49:27 +00:00 (Migrated from github.com)

Imported from ivall/xcl. Originally posted by Kalmarv on 2026-11-23T20:49:27Z.


Based on main 956a476. Commit 488e5fe.

Problem

On every origin (type 0), the clock thread released the origin semaphore and then queued the origin's QPC in ORIGINS (frame.rs, clock_thread). A WaitFrameEventX woken by that release could take the ORIGINS lock before the clock did. It found the queue empty and fell back to a fresh qpc(). The next wait then took the origin left in the queue, which was older. So origins could go backwards, and frame statistics could name the wrong frame's origin. The ordering was pointed out in a review comment on #165 (https://github.com/ivall/xcl/pull/165#issuecomment-7113420164).

The existing test statistics_name_each_frames_origin_and_rescheduled_handles_outlive_the_clock_tick hits this now and then. It failed once ("each frame its own origin", the third origin earlier than the second) in a full workspace run for #199, and passed on the rerun.

Change

  • publish_origin (the clock's side) holds the ORIGINS lock across ReleaseSemaphore and the push. A woken wait blocks on the lock until its origin is queued.
  • take_origin is the wait's side, unchanged in behaviour.
  • A semaphore that is already full (ORIGIN_SEMAPHORE_MAX pending) still drops the origin, as before.
  • after_release is a closure that runs between the release and the push. Production passes || {}; the test uses it to pause there.

Tests

  • a_woken_wait_takes_the_origin_that_woke_it (CPU, its own semaphore and queue, not the process clock). It pauses 100 ms between the release and the push, and the woken wait must get that origin. Two more origins must then come back in order.
    • With the old order (release, then lock and push), it fails in 20 of 20 runs: the woken wait gets no origin.
    • With this change it passes in 20 of 20.
  • Frame tests (hle::d3d12x::frame), 50 runs each, --test-threads=2: this branch 50 of 50 pass (4 tests); main 956a476 50 of 50 pass (3 tests). The original flake didn't show up in isolation on either. The forced interleaving above is the reproduction.
  • cargo test -p xcl --lib -- --test-threads=2 on an RTX 5070 Ti (NVIDIA 610.74, Windows 11): 537 passed, 0 failed, 19 ignored. That is main's 536 plus the new test; two wave64 tests skip on this GPU.

Overlap

#165 rewrites the same block of clock_thread and keeps the old order, so the two will conflict textually. Whichever lands second needs a one-line merge: call publish_origin inside #165's loop.

Not covered

  • The full workspace suite on this branch.
  • Whether a title ever saw backward origins in a real run; this was found in review and in a test.

AI-assisted (Claude Code). No game data.

🤖 Generated with Claude Code

<!-- greyfinch-sync:ivall/xcl:issue:6568545705 --> Imported from [ivall/xcl](https://github.com/xcl-dev/xcl/pull/205). Originally posted by [Kalmarv](https://github.com/Kalmarv) on 2026-11-23T20:49:27Z. --- Based on main 956a476. Commit 488e5fe. ## Problem On every origin (type 0), the clock thread released the origin semaphore and then queued the origin's QPC in `ORIGINS` (`frame.rs`, `clock_thread`). A `WaitFrameEventX` woken by that release could take the `ORIGINS` lock before the clock did. It found the queue empty and fell back to a fresh `qpc()`. The next wait then took the origin left in the queue, which was older. So origins could go backwards, and frame statistics could name the wrong frame's origin. The ordering was pointed out in a review comment on #165 (https://github.com/ivall/xcl/pull/165#issuecomment-7113420164). The existing test `statistics_name_each_frames_origin_and_rescheduled_handles_outlive_the_clock_tick` hits this now and then. It failed once ("each frame its own origin", the third origin earlier than the second) in a full workspace run for #199, and passed on the rerun. ## Change - `publish_origin` (the clock's side) holds the `ORIGINS` lock across `ReleaseSemaphore` and the push. A woken wait blocks on the lock until its origin is queued. - `take_origin` is the wait's side, unchanged in behaviour. - A semaphore that is already full (`ORIGIN_SEMAPHORE_MAX` pending) still drops the origin, as before. - `after_release` is a closure that runs between the release and the push. Production passes `|| {}`; the test uses it to pause there. ## Tests - `a_woken_wait_takes_the_origin_that_woke_it` (CPU, its own semaphore and queue, not the process clock). It pauses 100 ms between the release and the push, and the woken wait must get that origin. Two more origins must then come back in order. - With the old order (release, then lock and push), it fails in 20 of 20 runs: the woken wait gets no origin. - With this change it passes in 20 of 20. - Frame tests (`hle::d3d12x::frame`), 50 runs each, `--test-threads=2`: this branch 50 of 50 pass (4 tests); main 956a476 50 of 50 pass (3 tests). The original flake didn't show up in isolation on either. The forced interleaving above is the reproduction. - `cargo test -p xcl --lib -- --test-threads=2` on an RTX 5070 Ti (NVIDIA 610.74, Windows 11): 537 passed, 0 failed, 19 ignored. That is main's 536 plus the new test; two wave64 tests skip on this GPU. ## Overlap #165 rewrites the same block of `clock_thread` and keeps the old order, so the two will conflict textually. Whichever lands second needs a one-line merge: call `publish_origin` inside #165's loop. ## Not covered - The full workspace suite on this branch. - Whether a title ever saw backward origins in a real run; this was found in review and in a test. AI-assisted (Claude Code). No game data. 🤖 Generated with [Claude Code](https://claude.com/claude-code)
upstream-sync closed this pull request 2026-11-24 06:57:38 +00:00
Kalmarv commented 2026-11-24 07:04:19 +00:00 (Migrated from github.com)

Imported from ivall/xcl. Originally posted by Kalmarv on 2026-11-23T21:04:08Z.


Rebased on main eaa36b6 (5107d8a, no conflicts). cargo test --release -p xcl --lib frame: 7 passed, including the forced-interleaving test.

Overlaps: main's 956a476 (every event type due at one deadline fires) and #165, closed as covered by it. This PR is the separate ordering race between queueing an origin and the wait it wakes, which neither changed.

<!-- greyfinch-sync:ivall/xcl:comment:7113952549 --> Imported from [ivall/xcl](https://github.com/ivall/xcl/pull/205#issuecomment-7113952549). Originally posted by [Kalmarv](https://github.com/Kalmarv) on 2026-11-23T21:04:08Z. --- Rebased on main eaa36b6 (5107d8a, no conflicts). `cargo test --release -p xcl --lib frame`: 7 passed, including the forced-interleaving test. Overlaps: main's 956a476 (every event type due at one deadline fires) and #165, closed as covered by it. This PR is the separate ordering race between queueing an origin and the wait it wakes, which neither changed.
ethandepstein-del commented 2026-11-24 07:04:19 +00:00 (Migrated from github.com)

Imported from ivall/xcl. Originally posted by ethandepstein-del on 2026-11-23T21:05:39Z.


Independent static concurrency audit of 5107d8a54 (code inspection only — cargo test not run here; no game files):

Summary: Fixes the publish/take race cleanly and deadlock-free

  • Lock ordering verified: clock_thread() calls publish_origin(&ORIGINS, ...) without holding CLOCK, and wait_frame_event_x() calls take_origin(&ORIGINS) after WaitForMultipleObjects returns and releases ORIGINS before locking ISSUED. No lock-order inversion or deadlock risk.
  • Holding ORIGINS across ReleaseSemaphore and o.push_back(now) guarantees a woken waiter cannot observe an empty queue for the origin that just woke it.

One subtle edge case under full-semaphore backpressure (frame.rs:122-133)

  • Suppose sem is full (count == 4) with ORIGINS = [t1, t2, t3, t4]. Waiter W1 returns from WaitForMultipleObjects (sem drops 4 → 3), then is preempted before calling take_origin(&ORIGINS).
  • clock_thread() then runs publish_origin(t5): locks ORIGINS (still len 4), ReleaseSemaphore succeeds (3 → 4), pushes t5 (len 5), and while o.len() > ORIGIN_SEMAPHORE_MAX as usize { o.pop_front(); } drops t1.
  • Now 5 consumers (in-flight W1 + 4 semaphore tokens) share only 4 queued timestamps [t2, t3, t4, t5], so the 5th consumer still gets None and falls back to qpc().
  • Since ReleaseSemaphore already bounds queued tokens to ORIGIN_SEMAPHORE_MAX, the queue only legitimately exceeds ORIGIN_SEMAPHORE_MAX when woken waiters are still in flight to take_origin() (unless the semaphore was drained/recreated without clearing ORIGINS). Raising the safety cap (or clearing ORIGINS on unschedule/reset instead of trimming at ORIGIN_SEMAPHORE_MAX) would prevent dropping an in-flight waiter's origin under backpressure.

(AI-assisted)

<!-- greyfinch-sync:ivall/xcl:comment:7113989111 --> Imported from [ivall/xcl](https://github.com/ivall/xcl/pull/205#issuecomment-7113989111). Originally posted by [ethandepstein-del](https://github.com/ethandepstein-del) on 2026-11-23T21:05:39Z. --- Independent static concurrency audit of `5107d8a54` (code inspection only — `cargo test` not run here; no game files): ### Summary: Fixes the publish/take race cleanly and deadlock-free - **Lock ordering verified:** `clock_thread()` calls `publish_origin(&ORIGINS, ...)` without holding `CLOCK`, and `wait_frame_event_x()` calls `take_origin(&ORIGINS)` after `WaitForMultipleObjects` returns and releases `ORIGINS` before locking `ISSUED`. No lock-order inversion or deadlock risk. - Holding `ORIGINS` across `ReleaseSemaphore` and `o.push_back(now)` guarantees a woken waiter cannot observe an empty queue for the origin that just woke it. ### One subtle edge case under full-semaphore backpressure (`frame.rs:122-133`) - Suppose `sem` is full (`count == 4`) with `ORIGINS = [t1, t2, t3, t4]`. Waiter W1 returns from `WaitForMultipleObjects` (`sem` drops `4 → 3`), then is preempted before calling `take_origin(&ORIGINS)`. - `clock_thread()` then runs `publish_origin(t5)`: locks `ORIGINS` (still len 4), `ReleaseSemaphore` **succeeds** (`3 → 4`), pushes `t5` (len 5), and `while o.len() > ORIGIN_SEMAPHORE_MAX as usize { o.pop_front(); }` drops `t1`. - Now 5 consumers (in-flight W1 + 4 semaphore tokens) share only 4 queued timestamps `[t2, t3, t4, t5]`, so the 5th consumer still gets `None` and falls back to `qpc()`. - Since `ReleaseSemaphore` already bounds queued tokens to `ORIGIN_SEMAPHORE_MAX`, the queue only legitimately exceeds `ORIGIN_SEMAPHORE_MAX` when woken waiters are still in flight to `take_origin()` (unless the semaphore was drained/recreated without clearing `ORIGINS`). Raising the safety cap (or clearing `ORIGINS` on unschedule/reset instead of trimming at `ORIGIN_SEMAPHORE_MAX`) would prevent dropping an in-flight waiter's origin under backpressure. *(AI-assisted)*
ivall commented 2026-11-24 07:04:22 +00:00 (Migrated from github.com)

Imported from ivall/xcl. Originally posted by ivall on 2026-11-23T21:10:13Z.


Merged into main (6da33fa) as 488e5fec, the head before your last rebase - the same patch (identical patch-id), so GitHub does not show it as merged; closing it by hand. Checked on an RX 9070 (RDNA4) with the 22 PRs merged together: 582 unit tests passed, 0 failed, the integration and e2e tests passed, and the retail game still reaches its first menu (646 frames in 30 s, no device removal). Thanks!

<!-- greyfinch-sync:ivall/xcl:comment:7114099277 --> Imported from [ivall/xcl](https://github.com/ivall/xcl/pull/205#issuecomment-7114099277). Originally posted by [ivall](https://github.com/ivall) on 2026-11-23T21:10:13Z. --- Merged into main (6da33fa) as 488e5fec, the head before your last rebase - the same patch (identical patch-id), so GitHub does not show it as merged; closing it by hand. Checked on an RX 9070 (RDNA4) with the 22 PRs merged together: 582 unit tests passed, 0 failed, the integration and e2e tests passed, and the retail game still reaches its first menu (646 frames in 30 s, no device removal). Thanks!

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
xcl-dev/xcl!202
No description provided.