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

Closed
b3IKK wants to merge 1 commit from upstream-pr/1276 into main
b3IKK commented 2026-11-24 14:08:32 +00:00 (Migrated from github.com)

Imported from ivall/xcl. Originally posted by b3IKK on 2026-11-24T14:08:32Z.


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.
<!-- greyfinch-sync:ivall/xcl:issue:5473733024 --> Imported from [ivall/xcl](https://github.com/xcl-dev/xcl/pull/1276). Originally posted by [b3IKK](https://github.com/b3IKK) on 2026-11-24T14:08:32Z. --- 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.
DavidM1911 commented 2026-11-24 15:05:43 +00:00 (Migrated from github.com)

Imported from ivall/xcl. Originally posted by DavidM1911 on 2026-11-24T14:10:00Z.


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

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

Thanks!

<!-- greyfinch-sync:ivall/xcl:comment:7133942821 --> Imported from [ivall/xcl](https://github.com/ivall/xcl/pull/1276#issuecomment-7133942821). Originally posted by [DavidM1911](https://github.com/DavidM1911) on 2026-11-24T14:10:00Z. --- Verification on RTX 3060 Ti (Windows 11), branch merged with current `main`: - `cargo test -p xcl --lib xgameruntime`: 74 passed, 0 failed. Thanks!
dcastell1 commented 2026-11-24 15:05:45 +00:00 (Migrated from github.com)

Imported from ivall/xcl. Originally posted by dcastell1 on 2026-11-24T14:14:04Z.


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.

<!-- greyfinch-sync:ivall/xcl:comment:7134047432 --> Imported from [ivall/xcl](https://github.com/ivall/xcl/pull/1276#issuecomment-7134047432). Originally posted by [dcastell1](https://github.com/dcastell1) on 2026-11-24T14:14:04Z. --- 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.
CryingLaughing commented 2026-11-26 16:50:37 +00:00 (Migrated from github.com)

Imported from ivall/xcl. Originally posted by CryingLaughing on 2026-11-26T16:50:37Z.


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.

  • #1276 head 30c30410290e1ddaad7fc3121764976639fa2a1e on current main (tree dcd3c64595b7342181b65f682ba445c4c195c415) passed 11 task queue tests and 1 XAsync test.
  • With #1681 c53eed33ef1625568c739ca854e4eb34db8d1775 and #2806 10dbfac3facf4a1bb93784bcb3eda6203438b134 added (tree f636fa26df09a112a0aa0a255c42f66c8f3c3b5c), a fresh isolated target passed 13 task queue tests and 1 XAsync test. This included due-callback cancellation before termination, pending delayed callbacks counted by IsEmpty, and explicit dispatch on the caller while the serialized worker was active.

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.

<!-- greyfinch-sync:ivall/xcl:comment:7198166046 --> Imported from [ivall/xcl](https://github.com/xcl-dev/xcl/pull/1276#issuecomment-7198166046). Originally posted by [CryingLaughing](https://github.com/CryingLaughing) on 2026-11-26T16:50:37Z. --- 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. - #1276 head `30c30410290e1ddaad7fc3121764976639fa2a1e` on current main (tree `dcd3c64595b7342181b65f682ba445c4c195c415`) passed **11 task queue tests and 1 XAsync test**. - With #1681 `c53eed33ef1625568c739ca854e4eb34db8d1775` and #2806 `10dbfac3facf4a1bb93784bcb3eda6203438b134` added (tree `f636fa26df09a112a0aa0a255c42f66c8f3c3b5c`), a fresh isolated target passed **13 task queue tests and 1 XAsync test**. This included due-callback cancellation before termination, pending delayed callbacks counted by `IsEmpty`, and explicit dispatch on the caller while the serialized worker was active. 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 commented 2026-12-01 05:18:36 +00:00 (Migrated from github.com)

Imported from ivall/xcl. Originally posted by Foniass on 2026-12-01T05:18:36Z.


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.

<!-- greyfinch-sync:ivall/xcl:comment:7301635646 --> Imported from [ivall/xcl](https://github.com/xcl-dev/xcl/pull/1276#issuecomment-7301635646). Originally posted by [Foniass](https://github.com/Foniass) on 2026-12-01T05:18:36Z. --- **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.
throny commented 2026-12-02 10:17:54 +00:00 (Migrated from github.com)

Imported from ivall/xcl. Originally posted by throny on 2026-12-02T10:17:54Z.


Closing as a duplicate of #315: #1607 group D170, checked by Foniass and Decodere, who both read the diffs and suggest keeping #315. If something here is not in #315, say so and reopen it, or ask and I will.

AI use: Claude Code (Claude Opus 5.5) ran the closing script.

<!-- greyfinch-sync:ivall/xcl:comment:7342261494 --> Imported from [ivall/xcl](https://github.com/xcl-dev/xcl/pull/1276#issuecomment-7342261494). Originally posted by [throny](https://github.com/throny) on 2026-12-02T10:17:54Z. --- Closing as a duplicate of #315: #1607 group D170, checked by Foniass and Decodere, who both read the diffs and suggest keeping #315. If something here is not in #315, say so and reopen it, or ask and I will. AI use: Claude Code (Claude Opus 5.5) ran the closing script.

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!1266
No description provided.