Skip to content

XGameRuntime: prevent timer() from dropping due delayed callbacks during Terminate - #1276

Open
b3IKK wants to merge 1 commit into
xcl-dev:mainfrom
b3IKK:xgameruntime/timer-terminate-no-drop
Open

b3IKK wants to merge 1 commit into
xcl-dev:mainfrom
b3IKK:xgameruntime/timer-terminate-no-drop

Conversation

@b3IKK

@b3IKK b3IKK commented Nov 24, 2026 •

Copy link
Copy Markdown
Contributor

Between popping a due Timed entry from the timer heap and calling submit_now(), the delayed item was neither in the timer heap nor counted in QueueState::inflight. A concurrent XTaskQueueTerminate could observe inflight == [0, 0], invoke its termination callback, and cause timer()'s subsequent let _ = submit_now(...) to silently drop the callback without running it canceled (leaving XAsyncSchedule blocks permanently E_PENDING and leaking their payloads).

This reserves an inflight slot for the due item while still holding the timer lock (preserving the timer -> queue.state lock order used by submit_delayed), then dispatches via dispatch_due_timed: on success done_one() releases the temporary slot, and on termination q.execute() runs the callback directly with canceled = true so XTaskQueueTerminate waits until it completes.

What is in it

  • crates/xcl/src/hle/xgameruntime/taskqueue.rs: reserve inflight under the timer lock before popping/dispatching a due item via dispatch_due_timed; add unit test due_delayed_callback_is_canceled_before_terminate_finishes (covering both the deterministic pop-then-terminate window and a 40-iteration live timer-thread race).

Tested

  • hardware (GPU + driver, CPU), Windows build, Rust version, debug / release: NVIDIA GeForce RTX 5080 (driver 32.0.16.1088), AMD Ryzen 9 9950X3D, Windows 11 10.0.26300, Rust 1.100.0 (stable-x86_64-pc-windows-msvc), DXC 1.9.2609.5, debug (scripts/test.ps1) and release (scripts/smoke.ps1)
  • .\scripts\test.ps1 (cargo build --workspace + cargo test --workspace -- --test-threads=4): 752 passed, 0 failed, 41 ignored (SKIPPED GPU tests: 2 no wave64 — gpu::recomp::tests::gpu_geometry_wave32_prefix_count_over_a_mask, gpu::recomp::tests::gpu_wave32_program_in_wave64_hardware)
  • cargo test -p xcl --lib due_delayed_callback_is_canceled_before_terminate_finishes: 1 passed, 0 failed

Regression run

main:

xcl-smoke v1
commit: 47efa2ecea0a12934719ae69b81930cf02a4b1c3
gpu: NVIDIA GeForce RTX 5080
title: 9cc6a39e
seconds: 60
exit: 0x0
frames shown: 1226 (PresentX 1226)
presents per 2 s (last 3): 60 60 60
device removals: 0
crashes: 0
peak commit: 15341 MB

PR:

xcl-smoke v1
commit: 30c30410290e1ddaad7fc3121764976639fa2a1e
gpu: NVIDIA GeForce RTX 5080
title: 9cc6a39e
seconds: 60
exit: 0x0
frames shown: 1643 (PresentX 1643)
presents per 2 s (last 3): 60 60 60
device removals: 0
crashes: 0
peak commit: 14472 MB

Not tested

  • AMD GPUs and native wave64 hardware
  • other titles (Minecraft Preview, Unicorn Overlord, Fortnite, XDefiant) and Linux / Wine

AI use

  • OpenCode, Gemini 4 Neon: audited taskqueue.rs, wrote the fix and regression test, ran the test suite and scripts/smoke.ps1 regression runs, and drafted this PR description.

…ing Terminate

Between popping a due Timed entry from the timer heap and calling
submit_now(), the item was neither in the timer heap nor counted in
QueueState::inflight. A concurrent XTaskQueueTerminate could see
inflight == [0, 0], fire its termination callback, and cause timer()'s
subsequent `let _ = submit_now(...)` to silently discard the callback
without running it canceled (leaving XAsyncSchedule blocks permanently
E_PENDING).

Increment `inflight` for the due item while still holding the timer
lock (preserving the timer -> queue.state lock order used by
submit_delayed), then dispatch via dispatch_due_timed: on success
release the temporary inflight count with done_one(), and on
termination run q.execute() directly with canceled = true so Terminate
waits until the canceled callback completes.

Assisted-by: OpenCode (Gemini 4 Neon)
@DavidM1911

Copy link
Copy Markdown
Contributor

Verification on RTX 3060 Ti (Windows 11), branch merged with current main:

  • cargo test -p xcl --lib xgameruntime: 74 passed, 0 failed.

Thanks!

@dcastell1

Copy link
Copy Markdown

Verified on Windows 11 10.0.26300, NVIDIA GeForce RTX 5090 (driver 621.24), Rust 1.100.0 MSVC 14.44.35207, debug build, on main@930e8b4 (baseline 751 passed, 0 failed, 41 ignored on this box):

patch -p1 < this PR            # clean, no fuzz
cargo test --workspace -- --test-threads=4
752 passed; 0 failed; 41 ignored   (exit 0)

One more than the baseline; timer() no longer being dropped is exercised and green.

Tree restored afterwards (full diff against a clean checkout), hardware adapter, no Wine, no game data. AI-assisted (opencode): applied the patch to a built tree, ran the suite, reverted it.

IvyNotFound added a commit to IvyNotFound/xcl that referenced this pull request Nov 25, 2026
# Conflicts:
#	crates/xcl/src/hle/xgameruntime/taskqueue.rs
rootw0rm pushed a commit to rootw0rm/xcl that referenced this pull request Nov 26, 2026
…#1681 + xcl-dev#1276)

Windows hle/xgameruntime/taskqueue.rs hunks verbatim (cfg(windows)-only,
0 tests run on Linux): xcl-dev#1681 timer-heap leg in XTaskQueueIsEmpty (lock
order timer -> items -> state) + test; xcl-dev#1276 inflight reserve under the
timer lock + dispatch_due_timed with canceled-execute fallback + test.
Fidelity: diff vs pr1681head/pr1276head shows only the other PR's hunks.

Linux live twin hle/xtaskqueue_linux.rs gets both semantics (win64 ABI)
+ 2 Linux-shaped tests. Failing-first shown in an isolated pre-fix
worktree (since removed): IsEmpty test FAILED at runtime, due-delayed
test failed to compile on missing dispatch_due_timed; both green post.

Gates: both cargo checks 0 errors; lib 650/0/1 t=4 (+2); diff --check
clean; title fresh-root FAIL set byte-identical pre/post (13: 12
D3D12.X GPU-only + files:delete os-error-2, the latter also failing on
pristine HEAD - pre-existing drift, no regress); retail fresh-root
SIGSEGV +0x2237320 fault 0x0 exit 139 stubs 20 xgr 3 apis - HOLDS.
rootw0rm pushed a commit to rootw0rm/xcl that referenced this pull request Nov 26, 2026
, verbatim Windows + Linux twin)

Live pr805head=e88ee4d1 still OPEN (merge-base false); Windows
hle/xgameruntime/taskqueue.rs hunks hand-applied byte-identical
(cherry-pick -n conflicted at the shared test-site tail: steps-49/50
tests moved the append anchor; production + test hunks verified
identical to the head by direct diff, only line numbers drift).
Fidelity: git diff pr805head shows only steps-49/50 drift
(xcl-dev#1276/xcl-dev#1681 timer, xcl-dev#1154 terminate); pool block + mpsc test import +
new test byte-identical.

Production: fixed clamp(2,8) mpsc pool -> Pool{Mutex<PoolState>,Condvar,
base} + MAX_WORKERS=256 + IDLE_EXIT 30s + worker loop + spawn
wake-idle-else-grow with rollback + notify on spawn failure.
Linux live twin hle/xtaskqueue_linux.rs mirrors the production hunks
(ABI-neutral pool; win64 ABI untouched) + 1 Linux-shaped twin test
(16 blockers + releaser through wait=true Terminate, extern win64).

Failing-first: twin test FAILED pre-fix (7 vs 16, releaser starved on
the fixed 8-worker pool), green post-fix. Gates: both cargo checks 0
errors; lib 652/0/1 t=4 (+1); diff --check clean; lock audit clean
(named g/ng guards, explicit drop before job). Title fresh-root 12
unique FAILs all D3D12.X GPU-only 0 non-GPU (harness FAILs by design,
M2 baseline). Retail fresh-root SIGSEGV game_scarlett_master.exe
+0x2237320 fault 0x0 unmapped exit 139 stubs 20 xgr exactly
XAsync/XNetworking/XUser unimplemented [] - HOLDS, M1-RE TERMINAL.
rootw0rm pushed a commit to rootw0rm/xcl that referenced this pull request Nov 26, 2026
…l-dev#1239 + xcl-dev#315, verbatim Windows + Linux twin)

Windows hle/xgameruntime/taskqueue.rs: xcl-dev#315 waiter form (superset of xcl-dev#1239):
immutable Arc<Waiter> (wait Mutex + stopped + callback_arc), monotonic-u64
token map, IN_WAITER thread-local + guard, non-blocking self-unregister
offload to pool, post-insert terminated race, Terminate waiter cleanup.
xcl-dev#1239 + xcl-dev#315 waiter tests verbatim. xcl-dev#315 timer hunk skipped: landed xcl-dev#1276
already covers the due-timer race (different dispatch_due_timed shape);
its timer test superseded by the landed deterministic + 40-iter tests.
Linux hle/xtaskqueue_linux.rs twin: helper-thread-per-waiter on
kernelx WaitForSingleObject (5ms poll), same token map / guard / cleanup,
win64 ABI, 4 twin tests (fires-once, dup/bogus, self-unregister, terminate).
Failing-first: grafted twin probe onto HEAD worktree E0425 (no waiters());
twin 11/11 green.
@CryingLaughing

Copy link
Copy Markdown
Contributor

Independent integration check on current main 8b957bec6f82495aec940944c6bf47faa02db590, Windows 11 25H2 build 26200.9974, Core i5-13600K and Rust 1.100.0 x64 MSVC, debug. Both integrations are automatic Git merges with no source overlay.

Both selections had zero failures, ignored tests, or skips. The runs were CPU-only and did not include retail smoke or scheduling stress beyond the existing fixtures.

@Foniass

Foniass commented Dec 1, 2026

Copy link
Copy Markdown
Contributor

Triage (#1607) – head 30c3041

#315 contains the same timer fix: inflight is reserved under the timer lock, then done_one runs after a successful submit_now, else execute runs canceled. #315 also rewrites the waiter registration, as #1239 does. This PR is the focused, green version of the timer part. The race is still on main: timer() in taskqueue.rs pops the entry, drops the lock, and submit_now silently drops the item if XTaskQueueTerminate ran in between.
One difference from #315: here the canceled callback runs inline on the shared xcl-taskqueue-timer thread, so a slow title callback delays every other queue's timers; #315 hands it to spawn.
Recommendation: keep open.

AI review: Claude Code (Claude Opus 5.5), diffs read only, not built or run; not checked by a human.

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.

5 participants