XGameRuntime: track task queue waiters safely and avoid terminate races #315

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

@⁠Michael-Glaser on GitHub · 2026-11-23 23:08 UTC · original
Michael-Glaser:fix/taskqueue-waiter-and-terminate → main · +269 −18 · 1 files · 1 commits · open

Fixes raw-pointer waiter double-free, MODE_IMMEDIATE self-unregister deadlock, leaked waiters on XTaskQueueTerminate, and a timer() pop-vs-terminate race that dropped canceled delayed callbacks.

What is in it

  • crates/xcl/src/hle/xgameruntime/taskqueue.rs: token-keyed Arc<Waiter> map, locked RegisterWaitForSingleObject, deferred worker-pool unregister on self-unregister, waiter cleanup on terminate, inflight handoff guard in timer(), and unit tests.

Tested

  • Cross-checked on macOS (aarch64-apple-darwin, Rust 1.100.0): cargo check --offline --target x86_64-pc-windows-msvc --workspace --tests (0 errors, 0 new warnings).

Not tested

  • Not built or executed on Windows; cargo test and scripts/smoke.ps1 not run yet.

AI use

  • Antigravity with Gemini 4 Neon (audit, fix, unit tests, description).
**@⁠Michael-Glaser** on GitHub · 2026-11-23 23:08 UTC · [original](https://github.com/xcl-dev/xcl/pull/315) `Michael-Glaser:fix/taskqueue-waiter-and-terminate` → `main` · +269 −18 · 1 files · 1 commits · open Fixes raw-pointer waiter double-free, `MODE_IMMEDIATE` self-unregister deadlock, leaked waiters on `XTaskQueueTerminate`, and a `timer()` pop-vs-terminate race that dropped canceled delayed callbacks. **What is in it** - `crates/xcl/src/hle/xgameruntime/taskqueue.rs`: token-keyed `Arc<Waiter>` map, locked `RegisterWaitForSingleObject`, deferred worker-pool unregister on self-unregister, waiter cleanup on terminate, `inflight` handoff guard in `timer()`, and unit tests. **Tested** - Cross-checked on macOS (`aarch64-apple-darwin`, Rust 1.100.0): `cargo check --offline --target x86_64-pc-windows-msvc --workspace --tests` (0 errors, 0 new warnings). **Not tested** - Not built or executed on Windows; `cargo test` and `scripts/smoke.ps1` not run yet. **AI use** - Antigravity with Gemini 4 Neon (audit, fix, unit tests, description).
XTaskQueueRegisterWaiter handed the raw Box<Waiter> pointer out as the registration token and wrote (*w).wait after RegisterWaitForSingleObject had already armed waiter_fired. XTaskQueueUnregisterWaiter blindly cast any token back to Box<Waiter> (double-free on duplicate or bogus tokens) and called UnregisterWaitEx(..., INVALID_HANDLE_VALUE) synchronously (deadlocking when called from a MODE_IMMEDIATE waiter callback). XTaskQueueTerminate also left waiters registered and could race with timer() between popping a due item and submit_now, silently dropping the canceled callback.

Track waiters in a token-keyed map with Arc<Waiter>, hold the wait-handle lock across RegisterWaitForSingleObject, detect self-unregister via thread-local IN_WAITER and defer blocking UnregisterWaitEx to the worker pool, unregister a queue's remaining waiters during XTaskQueueTerminate, and hold an inflight handoff guard across timer()'s pop-to-submit_now transition under the existing timer -> q.state lock order.

Assisted-by: Antigravity (Gemini 4 Neon)
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 04:19:51 +00:00
Author
Owner

@⁠regiumlepidi on GitHub · 2026-11-23 23:57 UTC · original

A Windows run for this draft, since Not tested says it has not been built or run on Windows yet: 0cdf319 merged onto current main db1ddb2 (clean merge): builds and passes. One finding: the delayed-callback test does not fail on main here.

Tested

  • NVIDIA GeForce RTX 5070 Ti 16 GB, driver 617.42 (32.0.16.1742); also with XCL_ADAPTER=radeon (the Ryzen 7 9800X3D's RDNA2 iGPU, driver 32.0.21030.2001)
  • Ryzen 7 9800X3D (8 cores / 16 threads), 48 GB, Windows 11 Pro 25H2, rustc 1.100.0 MSVC, DXC from Windows SDK 10.0.26100, debug
  • .\scripts\test.ps1 (cargo test --workspace -- --test-threads=4), no XCL_SAMPLE_* set
Build RTX 5070 Ti RDNA2 iGPU
main db1ddb2 666 passed, 0 failed, 41 ignored 666 passed, 0 failed, 41 ignored
0cdf319 on db1ddb2 668 passed, 0 failed, 41 ignored 668 passed, 0 failed, 41 ignored
  • Both new tests run alone 10 times on the merge: 10 of 10 passed, no hang.
  • delayed_callback_racing_terminate_always_runs_before_termination_callback copied unchanged into main's taskqueue.rs (it uses only existing exports): passed 60 of 60 runs (40 iterations each). So on this machine it does not reproduce the drop / run-after-termination race the timer change fixes, and would not catch it coming back. A wider window between the timer popping the item and XTaskQueueTerminate (for example a delay long enough that the item is popped while terminate is running, rather than iter % 2 ms) might make it fail on main.
  • waiter_unregister_handles_duplicates_bogus_tokens_and_self_unregister cannot be run against main: it uses the new waiters() map, and main takes the token as a Box<Waiter> pointer, so the bogus-token call would dereference 0xDEADBEEFCAFEBABE.

Not tested

  • scripts/smoke.ps1 on a real title (I have none).

AI use: Claude Code with Claude Opus 5.5 ran the build and tests and wrote this comment; the numbers come from those runs.

**@⁠regiumlepidi** on GitHub · 2026-11-23 23:57 UTC · [original](https://github.com/xcl-dev/xcl/pull/315#issuecomment-7117544751) A Windows run for this draft, since **Not tested** says it has not been built or run on Windows yet: `0cdf319` merged onto current `main` `db1ddb2` (clean merge): **builds and passes**. One finding: the delayed-callback test does not fail on `main` here. **Tested** - NVIDIA GeForce RTX 5070 Ti 16 GB, driver 617.42 (32.0.16.1742); also with `XCL_ADAPTER=radeon` (the Ryzen 7 9800X3D's RDNA2 iGPU, driver 32.0.21030.2001) - Ryzen 7 9800X3D (8 cores / 16 threads), 48 GB, Windows 11 Pro 25H2, rustc 1.100.0 MSVC, DXC from Windows SDK 10.0.26100, debug - `.\scripts\test.ps1` (`cargo test --workspace -- --test-threads=4`), no `XCL_SAMPLE_*` set | Build | RTX 5070 Ti | RDNA2 iGPU | |---|---|---| | `main` `db1ddb2` | 666 passed, 0 failed, 41 ignored | 666 passed, 0 failed, 41 ignored | | `0cdf319` on `db1ddb2` | 668 passed, 0 failed, 41 ignored | 668 passed, 0 failed, 41 ignored | - Both new tests run alone 10 times on the merge: 10 of 10 passed, no hang. - `delayed_callback_racing_terminate_always_runs_before_termination_callback` copied unchanged into `main`'s `taskqueue.rs` (it uses only existing exports): **passed 60 of 60 runs** (40 iterations each). So on this machine it does not reproduce the drop / run-after-termination race the timer change fixes, and would not catch it coming back. A wider window between the timer popping the item and `XTaskQueueTerminate` (for example a delay long enough that the item is popped while terminate is running, rather than `iter % 2` ms) might make it fail on `main`. - `waiter_unregister_handles_duplicates_bogus_tokens_and_self_unregister` cannot be run against `main`: it uses the new `waiters()` map, and `main` takes the token as a `Box<Waiter>` pointer, so the bogus-token call would dereference `0xDEADBEEFCAFEBABE`. **Not tested** - `scripts/smoke.ps1` on a real title (I have none). AI use: Claude Code with Claude Opus 5.5 ran the build and tests and wrote this comment; the numbers come from those runs.
Author
Owner

@⁠Michael-Glaser on GitHub · 2026-11-24 00:17 UTC · original

Thanks @⁠regiumlepidi! Rebased onto main (a6f5f7a, commit 04b8042) and replaced the timing loop with a deterministic dispatch_due_timed pop-vs-terminate test that holds Terminate right between pop() and submit_now() so it reliably reproduces the dropped callback without the fix.

**@⁠Michael-Glaser** on GitHub · 2026-11-24 00:17 UTC · [original](https://github.com/xcl-dev/xcl/pull/315#issuecomment-7117917659) Thanks @⁠regiumlepidi! Rebased onto main (a6f5f7a, commit 04b8042) and replaced the timing loop with a deterministic dispatch_due_timed pop-vs-terminate test that holds Terminate right between pop() and submit_now() so it reliably reproduces the dropped callback without the fix.
Author
Owner

@⁠WebIdentity1 on GitHub · 2026-11-24 02:53 UTC · original · edited 2026-11-24 03:01 UTC

Updated-head Windows validation: 04b8042fbbe99f771e2a9b5f24ba9bb286f3f80a applied to main dd2ae9a70af5eb5aad60f02d86e386b72abe1e95, clean integration tree 85a5bd5a89b75b4f77b6058d28f35b10fe0ba7dd. The deterministic timer test now catches the old behavior.

Tested

  • Windows 10 build 19045, Ryzen 7 7800X3D, RTX 3090 driver 591.74 plus AMD integrated graphics driver 32.0.21030.2001, Rust 1.98.1 MSVC, SDK DXC 1.8.2502.11, Agility 1.620.2, debug.
  • cargo test --locked -p xcl --lib hle::xgameruntime::taskqueue:: -- --test-threads=1 --show-output: 12 passed, 0 failed, 0 ignored, no skips. This includes both updated waiter and timer regressions.
  • Negative control: replaced only dispatch_due_timed with pop → release timer lock → after_pop() → submit_now, preserving the deterministic fixture. cargo test --locked -p xcl --lib delayed_callback_racing_terminate_always_runs_before_termination_callback -- --test-threads=1 --show-output: 0 passed, 1 failed, 0 ignored. Observed zero callbacks instead of one. No timeout. Restored the PR helper before the full run.
  • cargo build --locked --workspace succeeded. cargo test --locked --workspace --no-fail-fast -- --test-threads=4 --show-output: 691 passed, 1 failed, 41 ignored. Only gpu_clamp_and_omod_per_instruction failed, previously reproduced on main. Presentation fixtures ran in desktop session1; XCL_ADAPTER=3090, while some fixtures select AMD directly.
  • Four explicit skips: gpu_geometry_wave32_prefix_count_over_a_mask and gpu_wave32_program_in_wave64_hardware (no native wave64 on NVIDIA), test_title_draws_with_a_root_signature_under_the_debug_layer and uploads_start_from_each_subresources_state (debug layer unavailable). XCL_SAMPLE_* variables unset.
  • Separate local integration with #641 (8d0bbc8, retained async queue): cargo test --locked -p xcl --lib hle::xgameruntime:: -- --test-threads=1 --show-output 78/0/0, no skips. The full workspace result above does not include #641.

Not tested
Retail smoke, release builds, Wine, installed PC XGameRuntime, or full-workspace execution with #641. The restored-old-behavior control covers the timer handoff; it does not test the old unsafe waiter-token implementation.

AI use
Codex (GPT-6) inspected the code, prepared the negative control, ran the native tests and wrote this report. No competing implementation.

**@⁠WebIdentity1** on GitHub · 2026-11-24 02:53 UTC · [original](https://github.com/xcl-dev/xcl/pull/315#issuecomment-7120572729) · edited 2026-11-24 03:01 UTC Updated-head Windows validation: `04b8042fbbe99f771e2a9b5f24ba9bb286f3f80a` applied to main `dd2ae9a70af5eb5aad60f02d86e386b72abe1e95`, clean integration tree `85a5bd5a89b75b4f77b6058d28f35b10fe0ba7dd`. The deterministic timer test now catches the old behavior. **Tested** - Windows 10 build 19045, Ryzen 7 7800X3D, RTX 3090 driver 591.74 plus AMD integrated graphics driver 32.0.21030.2001, Rust 1.98.1 MSVC, SDK DXC 1.8.2502.11, Agility 1.620.2, debug. - `cargo test --locked -p xcl --lib hle::xgameruntime::taskqueue:: -- --test-threads=1 --show-output`: **12 passed, 0 failed, 0 ignored**, no skips. This includes both updated waiter and timer regressions. - Negative control: replaced only `dispatch_due_timed` with pop → release timer lock → `after_pop()` → `submit_now`, preserving the deterministic fixture. `cargo test --locked -p xcl --lib delayed_callback_racing_terminate_always_runs_before_termination_callback -- --test-threads=1 --show-output`: **0 passed, 1 failed, 0 ignored**. Observed zero callbacks instead of one. No timeout. Restored the PR helper before the full run. - `cargo build --locked --workspace` succeeded. `cargo test --locked --workspace --no-fail-fast -- --test-threads=4 --show-output`: **691 passed, 1 failed, 41 ignored**. Only `gpu_clamp_and_omod_per_instruction` failed, previously reproduced on main. Presentation fixtures ran in desktop session1; `XCL_ADAPTER=3090`, while some fixtures select AMD directly. - Four explicit skips: `gpu_geometry_wave32_prefix_count_over_a_mask` and `gpu_wave32_program_in_wave64_hardware` (no native wave64 on NVIDIA), `test_title_draws_with_a_root_signature_under_the_debug_layer` and `uploads_start_from_each_subresources_state` (debug layer unavailable). `XCL_SAMPLE_*` variables unset. - Separate local integration with #641 (`8d0bbc8`, retained async queue): `cargo test --locked -p xcl --lib hle::xgameruntime:: -- --test-threads=1 --show-output` **78/0/0**, no skips. The full workspace result above does not include #641. **Not tested** Retail smoke, release builds, Wine, installed PC XGameRuntime, or full-workspace execution with #641. The restored-old-behavior control covers the timer handoff; it does not test the old unsafe waiter-token implementation. **AI use** Codex (GPT-6) inspected the code, prepared the negative control, ran the native tests and wrote this report. No competing implementation.
Author
Owner

@⁠Michael-Glaser on GitHub · 2026-11-24 13:53 UTC · original

Thanks @⁠WebIdentity1 for validating 04b8042 and the deterministic negative control! Rebased cleanly onto main (47efa2e, commit 3a684ef); cross-checked against x86_64-pc-windows-msvc (--workspace --tests).

**@⁠Michael-Glaser** on GitHub · 2026-11-24 13:53 UTC · [original](https://github.com/xcl-dev/xcl/pull/315#issuecomment-7133489106) Thanks @⁠WebIdentity1 for validating `04b8042` and the deterministic negative control! Rebased cleanly onto `main` (`47efa2e`, commit `3a684ef`); cross-checked against `x86_64-pc-windows-msvc` (`--workspace --tests`).
Author
Owner

@⁠Toons29 on GitHub · 2026-11-25 02:54 UTC · original

Regression run of 3a684ef2 on the retail title, for this PR's Regression run section. Both blocks are from scripts/smoke.ps1 as printed, not edited: main on 4178e888, this PR on its head commit with no uncommitted changes, same title and time.

xcl-smoke v1
commit: 4178e888c0b5be50a2887eda45cdb832763b68a4
gpu: AMD Radeon RX 9070
title: game.exe
seconds: 60
exit: 0x0
frames shown: 1618 (PresentX 1618)
presents per 2 s (last 3): 61 60 60
device removals: 0
crashes: 0
peak commit: 14831 MB
xcl-smoke v1
commit: 3a684ef26a0cf7e0e436a81b15467b8acc56d8c6
gpu: AMD Radeon RX 9070
title: game.exe
seconds: 60
exit: 0x0
frames shown: 1633 (PresentX 1633)
presents per 2 s (last 3): 60 60 60
device removals: 0
crashes: 0
peak commit: 14818 MB

Against the limits in CONTRIBUTING.md: 101 % of main's frames, 100 % of its peak commit, no crash, no device removal.

AMD Radeon RX 9070 (RDNA4, driver 32.0.32029.2010), Ryzen 7 7800X3D, 32 GB, Windows 11 build 26300, Rust 1.100.0, cargo build --release -p xcl, 60 s, XCL_NETWORK=0. This is a regression check only: it does not show whether the run reaches the code this PR changes.

AI use: Claude Code (Claude Opus 5.5) built both trees and ran scripts/smoke.ps1; I checked the blocks before posting.

**@⁠Toons29** on GitHub · 2026-11-25 02:54 UTC · [original](https://github.com/xcl-dev/xcl/pull/315#issuecomment-7151351269) Regression run of `3a684ef2` on the retail title, for this PR's **Regression run** section. Both blocks are from `scripts/smoke.ps1` as printed, not edited: `main` on `4178e888`, this PR on its head commit with no uncommitted changes, same title and time. ``` xcl-smoke v1 commit: 4178e888c0b5be50a2887eda45cdb832763b68a4 gpu: AMD Radeon RX 9070 title: game.exe seconds: 60 exit: 0x0 frames shown: 1618 (PresentX 1618) presents per 2 s (last 3): 61 60 60 device removals: 0 crashes: 0 peak commit: 14831 MB ``` ``` xcl-smoke v1 commit: 3a684ef26a0cf7e0e436a81b15467b8acc56d8c6 gpu: AMD Radeon RX 9070 title: game.exe seconds: 60 exit: 0x0 frames shown: 1633 (PresentX 1633) presents per 2 s (last 3): 60 60 60 device removals: 0 crashes: 0 peak commit: 14818 MB ``` Against the limits in CONTRIBUTING.md: 101 % of `main`'s frames, 100 % of its peak commit, no crash, no device removal. AMD Radeon RX 9070 (RDNA4, driver 32.0.32029.2010), Ryzen 7 7800X3D, 32 GB, Windows 11 build 26300, Rust 1.100.0, `cargo build --release -p xcl`, 60 s, `XCL_NETWORK=0`. This is a regression check only: it does not show whether the run reaches the code this PR changes. AI use: Claude Code (Claude Opus 5.5) built both trees and ran `scripts/smoke.ps1`; I checked the blocks before posting.
Author
Owner

@⁠DavidM1911 on GitHub · 2026-11-25 14:29 UTC · original

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

  • cargo test -p xcl --lib: all passed, 0 failed, ~26 ignored. (Intermittent single-test flake / 0xc0000005 abort under parallel scheduling — tracked in #1978; clean on single-threaded rerun.)

Thanks!

**@⁠DavidM1911** on GitHub · 2026-11-25 14:29 UTC · [original](https://github.com/xcl-dev/xcl/pull/315#issuecomment-7165351128) Verification on RTX 3060 Ti (Windows 11), branch merged with current `main`: - `cargo test -p xcl --lib`: all passed, 0 failed, ~26 ignored. (Intermittent single-test flake / 0xc0000005 abort under parallel scheduling — tracked in #1978; clean on single-threaded rerun.) Thanks!
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/315/head:github-bot/gh-315'
git switch 'github-bot/gh-315'

Merge

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