Frame events: queue an origin before the wait it wakes can take it #205
No reviewers
Labels
No labels
accessibility
bug
documentation
duplicate
enhancement
goal:world-load
good first issue
help wanted
invalid
keep-open
off-goal
question
stale
test-only
wontfix
No milestone
No assignees
1 participant
Notifications
Due date
No due date set.
Dependencies
No dependencies set
Reference
github/xcl-dev--xcl!205
Loading…
Reference in a new issue
No description provided.
Delete branch "gh-pr/205/head"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
@Kalmarv on GitHub · 2026-11-23 20:49 UTC · original
Kalmarv:fix/frame-origin-order→main· +51 −9 · 1 files · 1 commits · closed without mergingBased on main
956a476. Commit488e5fe.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). AWaitFrameEventXwoken by that release could take theORIGINSlock before the clock did. It found the queue empty and fell back to a freshqpc(). 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_tickhits 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 theORIGINSlock acrossReleaseSemaphoreand the push. A woken wait blocks on the lock until its origin is queued.take_originis the wait's side, unchanged in behaviour.ORIGIN_SEMAPHORE_MAXpending) still drops the origin, as before.after_releaseis 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.hle::d3d12x::frame), 50 runs each,--test-threads=2: this branch 50 of 50 pass (4 tests); main956a47650 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=2on 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_threadand keeps the old order, so the two will conflict textually. Whichever lands second needs a one-line merge: callpublish_origininside #165's loop.Not covered
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 #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) <[email protected]>@Kalmarv on GitHub · 2026-11-23 21:04 UTC · original
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.@mastrr9 on GitHub · 2026-11-23 21:05 UTC · original
Independent static concurrency audit of
5107d8a54(code inspection only —cargo testnot run here; no game files):Summary: Fixes the publish/take race cleanly and deadlock-free
clock_thread()callspublish_origin(&ORIGINS, ...)without holdingCLOCK, andwait_frame_event_x()callstake_origin(&ORIGINS)afterWaitForMultipleObjectsreturns and releasesORIGINSbefore lockingISSUED. No lock-order inversion or deadlock risk.ORIGINSacrossReleaseSemaphoreando.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)semis full (count == 4) withORIGINS = [t1, t2, t3, t4]. Waiter W1 returns fromWaitForMultipleObjects(semdrops4 → 3), then is preempted before callingtake_origin(&ORIGINS).clock_thread()then runspublish_origin(t5): locksORIGINS(still len 4),ReleaseSemaphoresucceeds (3 → 4), pushest5(len 5), andwhile o.len() > ORIGIN_SEMAPHORE_MAX as usize { o.pop_front(); }dropst1.[t2, t3, t4, t5], so the 5th consumer still getsNoneand falls back toqpc().ReleaseSemaphorealready bounds queued tokens toORIGIN_SEMAPHORE_MAX, the queue only legitimately exceedsORIGIN_SEMAPHORE_MAXwhen woken waiters are still in flight totake_origin()(unless the semaphore was drained/recreated without clearingORIGINS). Raising the safety cap (or clearingORIGINSon unschedule/reset instead of trimming atORIGIN_SEMAPHORE_MAX) would prevent dropping an in-flight waiter's origin under backpressure.(AI-assisted)
@ivall on GitHub · 2026-11-23 21:10 UTC · original
Merged into main (
6da33fa) as488e5fec, 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