XGameRuntime: track task queue waiters safely and avoid terminate races #312
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/315"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Imported from ivall/xcl. Originally posted by Michael-Glaser on 2026-11-23T23:08:08Z.
Fixes raw-pointer waiter double-free,
MODE_IMMEDIATEself-unregister deadlock, leaked waiters onXTaskQueueTerminate, and atimer()pop-vs-terminate race that dropped canceled delayed callbacks.What is in it
crates/xcl/src/hle/xgameruntime/taskqueue.rs: token-keyedArc<Waiter>map, lockedRegisterWaitForSingleObject, drop thewaiters()mutex before blocking inUnregisterWaitEx(fixing the review deadlock caught by@luckyteagirl), deferred worker-pool unregister on self-unregister, waiter cleanup on terminate,inflighthandoff guard intimer(), and unit tests (waiter_concurrent_unregister_releases_map_lock_before_blocking_os_wait,dispatch_due_timeddeterministic pop-vs-terminate race).Tested
c3063fdon macOS (aarch64-apple-darwin, Rust 1.100.0):cargo check --offline --target x86_64-pc-windows-msvc --workspace --tests(0 errors, 0 new warnings).04b8042/3a684ef) were validated on Windows by@WebIdentity1(12/12taskqueuetests + negative control),@Toons29(60 s RX 9070 retail smoke run), and@DavidM1911(RTX 3060 Ti).Not tested
c3063fd(rebased onto623d02ewith@luckyteagirl's lock-drop fix and new concurrent-unregister test) has not been executed on Windows or re-smoked withscripts/smoke.ps1yet.AI use
Imported from ivall/xcl. Originally posted by regiumlepidi on 2026-11-23T23:57:25Z.
A Windows run for this draft, since Not tested says it has not been built or run on Windows yet:
0cdf319merged onto currentmaindb1ddb2(clean merge): builds and passes. One finding: the delayed-callback test does not fail onmainhere.Tested
XCL_ADAPTER=radeon(the Ryzen 7 9800X3D's RDNA2 iGPU, driver 32.0.21030.2001).\scripts\test.ps1(cargo test --workspace -- --test-threads=4), noXCL_SAMPLE_*setmaindb1ddb20cdf319ondb1ddb2delayed_callback_racing_terminate_always_runs_before_termination_callbackcopied unchanged intomain'staskqueue.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 andXTaskQueueTerminate(for example a delay long enough that the item is popped while terminate is running, rather thaniter % 2ms) might make it fail onmain.waiter_unregister_handles_duplicates_bogus_tokens_and_self_unregistercannot be run againstmain: it uses the newwaiters()map, andmaintakes the token as aBox<Waiter>pointer, so the bogus-token call would dereference0xDEADBEEFCAFEBABE.Not tested
scripts/smoke.ps1on 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.
Imported from ivall/xcl. Originally posted by Michael-Glaser on 2026-11-24T00:17:47Z.
Thanks @regiumlepidi! Rebased onto main (
a6f5f7a, commit04b8042) 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.Imported from ivall/xcl. Originally posted by WebIdentity1 on 2026-11-24T02:53:21Z.
Updated-head Windows validation:
04b8042fbbe99f771e2a9b5f24ba9bb286f3f80aapplied to maindd2ae9a70af5eb5aad60f02d86e386b72abe1e95, clean integration tree85a5bd5a89b75b4f77b6058d28f35b10fe0ba7dd. The deterministic timer test now catches the old behavior.Tested
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.dispatch_due_timedwith 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 --workspacesucceeded.cargo test --locked --workspace --no-fail-fast -- --test-threads=4 --show-output: 691 passed, 1 failed, 41 ignored. Onlygpu_clamp_and_omod_per_instructionfailed, previously reproduced on main. Presentation fixtures ran in desktop session1;XCL_ADAPTER=3090, while some fixtures select AMD directly.gpu_geometry_wave32_prefix_count_over_a_maskandgpu_wave32_program_in_wave64_hardware(no native wave64 on NVIDIA),test_title_draws_with_a_root_signature_under_the_debug_layeranduploads_start_from_each_subresources_state(debug layer unavailable).XCL_SAMPLE_*variables unset.8d0bbc8, retained async queue):cargo test --locked -p xcl --lib hle::xgameruntime:: -- --test-threads=1 --show-output78/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.
Imported from ivall/xcl. Originally posted by Michael-Glaser on 2026-11-24T13:53:26Z.
Thanks @WebIdentity1 for validating
04b8042and the deterministic negative control! Rebased cleanly ontomain(47efa2e, commit3a684ef); cross-checked againstx86_64-pc-windows-msvc(--workspace --tests).Imported from ivall/xcl. Originally posted by Toons29 on 2026-11-25T02:54:51Z.
Regression run of
3a684ef2on the retail title, for this PR's Regression run section. Both blocks are fromscripts/smoke.ps1as printed, not edited:mainon4178e888, this PR on its head commit with no uncommitted changes, same title and time.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.Imported from ivall/xcl. Originally posted by DavidM1911 on 2026-11-25T14:29:09Z.
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!
Imported from ivall/xcl. Originally posted by luckyteagirl on 2026-11-26T10:29:11Z.
Review: COMMENTED
At
3a684ef, bothXTaskQueueUnregisterWaiter(taskqueue.rs:808–809) and the post-registration terminated cleanup (:795–796) callunregister_waiterinside a successfulif let Some(...) = waiters().lock().unwrap().remove(...)body. The temporary mutex guard remains held through that body.unregister_waiterthen callsUnregisterWaitEx(..., INVALID_HANDLE_VALUE), which waits for any running callback to finish.One reachable deadlock: waiter A's
MODE_IMMEDIATEcallback has entered title code; another thread removes A and waits inUnregisterWaitExwhile still holding the waiter map; A's callback tries to unregister its own (now absent) token and blocks trying to inspect the map. Neither thread can finish. The self-unregister branch cannot help because the callback never reachesunregister_waiter. The legacy base has no registry mutex around its blocking OS unregister, so this particular lock cycle is introduced here.Could each site first bind the removal result in a separate statement, letting the map guard drop before the blocking unregister? A gated immediate callback that looks up its already-removed token while another thread unregisters A would exercise this. A Rust 2024 mutex-scope probe confirmed the guard is held in the current
if letbody and released by the split statement; I did not deliberately hang XCL's native waiter test. Codex (GPT-6.2 Sol and GPT-6.2 Astra) verified this exact-head source path and interleaving.Imported from ivall/xcl. Originally posted by Michael-Glaser on 2026-11-27T12:05:18Z.
Great catch @luckyteagirl — thank you! Fixed both
waiters().lock().unwrap().remove(...)sites (XTaskQueueUnregisterWaiterand the post-registration terminate cleanup) to bind and drop the map guard before callingunregister_waiter, addedwaiter_concurrent_unregister_releases_map_lock_before_blocking_os_waitto cover that exact interleaving, and rebased ontomain(623d02e, commitc3063fd). Cross-checked on macOS againstx86_64-pc-windows-msvc(--workspace --tests); thanks also to @Toons29 and @DavidM1911 for the earlier Windows/smoke runs —c3063fdwill need a fresh Windows test +smoke.ps1pass.Imported from ivall/xcl. Originally posted by Foniass on 2026-12-01T03:07:18Z.
Triage (#1607) – head
c3063fd#1239 makes the same waiter change (Arc'd
Waiterbehind a token map, a deferredUnregisterWaitExon self-unregister, safe double or bogus unregister). #1276 makes the same timer change (an inflight slot reserved under the timer lock beforesubmit_now, run canceled if the queue terminated). This PR does both, opened first, and also unregisters a queue's remaining waiters inXTaskQueueTerminate. Neither change is on main.These are two independent fixes in one PR; CONTRIBUTING.md asks for one per PR, and #1239 / #1276 are already that split. Only the PR description check has run on this head; it failed.
Recommendation: keep open.
AI review: Claude Code (Claude Opus 5.5), diffs read only, not built or run; not checked by a human.
View command line instructions
Checkout
From your project repository, check out a new branch and test the changes.Merge
Merge the changes and update on Forgejo.Warning: The "Autodetect manual merge" setting is not enabled for this repository, you will have to mark this pull request as manually merged afterwards.