Skip to content

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

Open
Michael-Glaser wants to merge 1 commit into
xcl-dev:mainfrom
Michael-Glaser:fix/taskqueue-waiter-and-terminate
Open

Michael-Glaser wants to merge 1 commit into
xcl-dev:mainfrom
Michael-Glaser:fix/taskqueue-waiter-and-terminate

Conversation

@Michael-Glaser

Copy link
Copy Markdown

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).

@regiumlepidi

Copy link
Copy Markdown

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.

@Michael-Glaser
Michael-Glaser force-pushed the fix/taskqueue-waiter-and-terminate branch from 0cdf319 to 04b8042 Compare November 24, 2026 00:16
@Michael-Glaser

Copy link
Copy Markdown
Author

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.

@WebIdentity1

WebIdentity1 commented Nov 24, 2026 •

Copy link
Copy Markdown
Contributor

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 XGameRuntime: retain an async operation's queue after handle close #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 XGameRuntime: retain an async operation's queue after handle close #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.

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)
@Michael-Glaser
Michael-Glaser force-pushed the fix/taskqueue-waiter-and-terminate branch from 04b8042 to 3a684ef Compare November 24, 2026 13:50
@Michael-Glaser

Copy link
Copy Markdown
Author

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).

@Toons29

Toons29 commented Nov 25, 2026

Copy link
Copy Markdown

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.

@DavidM1911

Copy link
Copy Markdown
Contributor

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

Thanks!

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