Skip to content

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

Closed
Kalmarv wants to merge 1 commit into
ivall:mainfrom
Kalmarv:fix/frame-origin-order
Closed

Kalmarv wants to merge 1 commit into
ivall:mainfrom
Kalmarv:fix/frame-origin-order

Conversation

@Kalmarv

@Kalmarv Kalmarv commented Nov 23, 2026

Copy link
Copy Markdown
Contributor

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 (#165 (comment)).

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

The clock released the origin semaphore and only then queued the origin's
QPC. A WaitFrameEventX woken by that release could take the ORIGINS lock
first, find the queue empty and use a fresh QPC. The next wait then got the
older origin, so origins went backwards. The frame-statistics test caught
this now and then ("each frame its own origin").

publish_origin now holds the queue lock across the release and the push,
and take_origin is the wait's side. Reported in a review comment on ivall#165.

Test: a_woken_wait_takes_the_origin_that_woke_it pauses between the
release and the push. With the old order it fails every time (20 of 20
runs: the wait gets no origin); with the lock held it passes (0 of 20).

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
@Kalmarv
Kalmarv force-pushed the fix/frame-origin-order branch from 488e5fe to 5107d8a Compare November 23, 2026 21:03
@Kalmarv

Kalmarv commented Nov 23, 2026

Copy link
Copy Markdown
Contributor Author

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

Copy link
Copy Markdown

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 added a commit that referenced this pull request Nov 23, 2026
@ivall

ivall commented Nov 23, 2026

Copy link
Copy Markdown
Owner

Merged into main (6da33fa) as 488e5fe, 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!

@ivall ivall closed this Nov 23, 2026
josilo added a commit to josilo/xcl that referenced this pull request Nov 23, 2026
Apply XCL ivall#204, ivall#205, ivall#147, ivall#167, ivall#191, ivall#201 and ivall#210: sparse range tracking, frame-origin ordering, crash reporting, refused interface and BCPack diagnostics, shader fixes, and console format queries. Keep menu glyph syncing and slot-57 tracking.
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.

3 participants