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

Open
github-bot wants to merge 1 commit from github-bot/gh-1276 into main AGit
Owner

@⁠b3IKK on GitHub · 2026-11-24 14:08 UTC · original
b3IKK:xgameruntime/timer-terminate-no-drop → main · +100 −1 · 1 files · 1 commits · open

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.
**@⁠b3IKK** on GitHub · 2026-11-24 14:08 UTC · [original](https://github.com/xcl-dev/xcl/pull/1276) `b3IKK:xgameruntime/timer-terminate-no-drop` → `main` · +100 −1 · 1 files · 1 commits · open 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.
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)
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 04:49:10 +00:00
Author
Owner

@⁠DavidM1911 on GitHub · 2026-11-24 14:10 UTC · original

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

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

Thanks!

**@⁠DavidM1911** on GitHub · 2026-11-24 14:10 UTC · [original](https://github.com/xcl-dev/xcl/pull/1276#issuecomment-7133942821) Verification on RTX 3060 Ti (Windows 11), branch merged with current `main`: - `cargo test -p xcl --lib xgameruntime`: 74 passed, 0 failed. Thanks!
Author
Owner

@⁠dcastell1 on GitHub · 2026-11-24 14:14 UTC · original

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.

**@⁠dcastell1** on GitHub · 2026-11-24 14:14 UTC · [original](https://github.com/xcl-dev/xcl/pull/1276#issuecomment-7134047432) 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.
This pull request can be merged automatically.
This branch is out-of-date with the base branch
You are not authorized to merge this pull request.
View command line instructions

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u origin '+refs/pull/1276/head:github-bot/gh-1276'
git switch 'github-bot/gh-1276'

Merge

Merge the changes and update on Forgejo.
git switch 'main'
git merge --no-ff 'github-bot/gh-1276'
git switch 'github-bot/gh-1276'
git rebase 'main'
git switch 'main'
git merge --ff-only 'github-bot/gh-1276'
git switch 'github-bot/gh-1276'
git rebase 'main'
git switch 'main'
git merge --no-ff 'github-bot/gh-1276'
git switch 'main'
git merge --squash 'github-bot/gh-1276'
git switch 'main'
git merge --ff-only 'github-bot/gh-1276'
git switch 'main'
git merge 'github-bot/gh-1276'
git push origin 'main'
This discussion has been locked. Commenting is limited to contributors.
No description provided.