XGameRuntime: fix waiter data race, token double-free, and self-unregister deadlock #1229

Closed
b3IKK wants to merge 1 commit from upstream-pr/1239 into main
b3IKK commented 2026-11-24 13:35:31 +00:00 (Migrated from github.com)

Imported from ivall/xcl. Originally posted by b3IKK on 2026-11-24T13:35:31Z.


XTaskQueueRegisterWaiter / XTaskQueueUnregisterWaiter had three safety/liveness bugs:

  1. (*w).wait = wh as usize mutated a non-atomic field on Waiter after RegisterWaitForSingleObject activated the wait, racing with waiter_fired on a pre-signaled handle.
  2. token.token was a raw Box<Waiter> pointer with no live-token registry, so calling XTaskQueueUnregisterWaiter twice with the same token or with an invalid token double-freed or dereferenced bogus memory.
  3. Calling XTaskQueueUnregisterWaiter from inside a MODE_IMMEDIATE callback deadlocked deterministically in UnregisterWaitEx(wh, INVALID_HANDLE_VALUE).

This tracks live waiters in a WAITERS map keyed by monotonic u64 tokens, keeps Waiter immutable after RegisterWaitForSingleObject, and offloads blocking UnregisterWaitEx + the final Arc drop to a helper thread when unregistering from inside that waiter's own callback.

What is in it

  • crates/xcl/src/hle/xgameruntime/taskqueue.rs: track waiters in WAITERS with monotonic tokens and Arc<Waiter>, track the active callback token in CURRENT_WAITER to avoid self-unregister deadlocks, and add unregister_waiter_is_idempotent_and_self_unregister_does_not_deadlock (verified deadlocking before the fix).

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): 742 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 unregister_waiter_is_idempotent_and_self_unregister_does_not_deadlock: 1 passed, 0 failed (deadlocked/timed out before the fix)

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: f6e94fbf2dc3a9686dee743c402aa4727792cd9b
gpu: NVIDIA GeForce RTX 5080
title: 9cc6a39e
seconds: 60
exit: 0x0
frames shown: 1646 (PresentX 1646)
presents per 2 s (last 3): 60 60 60
device removals: 0
crashes: 0
peak commit: 14608 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:5473174483 --> Imported from [ivall/xcl](https://github.com/xcl-dev/xcl/pull/1239). Originally posted by [b3IKK](https://github.com/b3IKK) on 2026-11-24T13:35:31Z. --- `XTaskQueueRegisterWaiter` / `XTaskQueueUnregisterWaiter` had three safety/liveness bugs: 1. `(*w).wait = wh as usize` mutated a non-atomic field on `Waiter` *after* `RegisterWaitForSingleObject` activated the wait, racing with `waiter_fired` on a pre-signaled handle. 2. `token.token` was a raw `Box<Waiter>` pointer with no live-token registry, so calling `XTaskQueueUnregisterWaiter` twice with the same token or with an invalid token double-freed or dereferenced bogus memory. 3. Calling `XTaskQueueUnregisterWaiter` from inside a `MODE_IMMEDIATE` callback deadlocked deterministically in `UnregisterWaitEx(wh, INVALID_HANDLE_VALUE)`. This tracks live waiters in a `WAITERS` map keyed by monotonic `u64` tokens, keeps `Waiter` immutable after `RegisterWaitForSingleObject`, and offloads blocking `UnregisterWaitEx` + the final `Arc` drop to a helper thread when unregistering from inside that waiter's own callback. **What is in it** - `crates/xcl/src/hle/xgameruntime/taskqueue.rs`: track waiters in `WAITERS` with monotonic tokens and `Arc<Waiter>`, track the active callback token in `CURRENT_WAITER` to avoid self-unregister deadlocks, and add `unregister_waiter_is_idempotent_and_self_unregister_does_not_deadlock` (verified deadlocking before the fix). **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`): 742 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 unregister_waiter_is_idempotent_and_self_unregister_does_not_deadlock`: 1 passed, 0 failed (deadlocked/timed out before the fix) **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: f6e94fbf2dc3a9686dee743c402aa4727792cd9b gpu: NVIDIA GeForce RTX 5080 title: 9cc6a39e seconds: 60 exit: 0x0 frames shown: 1646 (PresentX 1646) presents per 2 s (last 3): 60 60 60 device removals: 0 crashes: 0 peak commit: 14608 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 14:06:51 +00:00 (Migrated from github.com)

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


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

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

Thanks!

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

Imported from ivall/xcl. Originally posted by DavidM1911 on 2026-11-26T04:32:23Z.


Re-verification on RTX 3060 Ti (Windows 11), new head merged with current main:

  • cargo test -p xcl --lib: all passed, 0 failed, 27 ignored. The earlier merge conflict is resolved.

Thanks!

— Devin

<!-- greyfinch-sync:ivall/xcl:comment:7182683021 --> Imported from [ivall/xcl](https://github.com/xcl-dev/xcl/pull/1239#issuecomment-7182683021). Originally posted by [DavidM1911](https://github.com/DavidM1911) on 2026-11-26T04:32:23Z. --- Re-verification on RTX 3060 Ti (Windows 11), new head merged with current `main`: - `cargo test -p xcl --lib`: all passed, 0 failed, 27 ignored. The earlier merge conflict is resolved. Thanks! — Devin
github-actions[bot] commented 2026-12-01 02:31:29 +00:00 (Migrated from github.com)

Imported from ivall/xcl. Originally posted by github-actions[bot] on 2026-12-01T02:31:29Z.


Nothing has happened on this PR for 3 days, so it is marked stale and will be closed in 4 days. A push or a comment takes the mark off: rebase it onto main if it conflicts, and say in one line what it still fixes. If it only waits for a maintainer, a comment saying so is enough.

<!-- greyfinch-sync:ivall/xcl:comment:7298708864 --> Imported from [ivall/xcl](https://github.com/xcl-dev/xcl/pull/1239#issuecomment-7298708864). Originally posted by [github-actions[bot]](https://github.com/apps/github-actions) on 2026-12-01T02:31:29Z. --- Nothing has happened on this PR for 3 days, so it is marked `stale` and will be closed in 4 days. A push or a comment takes the mark off: rebase it onto `main` if it conflicts, and say in one line what it still fixes. If it only waits for a maintainer, a comment saying so is enough.
Foniass commented 2026-12-01 05:12:27 +00:00 (Migrated from github.com)

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


Triage (#1607) – head f6e94fb

#315 makes the same change to XTaskQueueRegisterWaiter / XTaskQueueUnregisterWaiter in taskqueue.rs: the waiter becomes an Arc behind a token map, waiter_fired stops once the waiter is unregistered, and a self-unregister from inside the waiter callback hands UnregisterWaitEx(INVALID_HANDLE_VALUE) to another thread. #315 was opened first, has a test of the same self-unregister/duplicate-token case, and also unregisters a queue's remaining waiters in XTaskQueueTerminate. Neither is on main.
No problems found in this diff itself.
Recommendation: close (duplicate of #315).

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:7301529018 --> Imported from [ivall/xcl](https://github.com/xcl-dev/xcl/pull/1239#issuecomment-7301529018). Originally posted by [Foniass](https://github.com/Foniass) on 2026-12-01T05:12:27Z. --- **Triage (#1607)** – head f6e94fb #315 makes the same change to `XTaskQueueRegisterWaiter` / `XTaskQueueUnregisterWaiter` in taskqueue.rs: the waiter becomes an `Arc` behind a token map, `waiter_fired` stops once the waiter is unregistered, and a self-unregister from inside the waiter callback hands `UnregisterWaitEx(INVALID_HANDLE_VALUE)` to another thread. #315 was opened first, has a test of the same self-unregister/duplicate-token case, and also unregisters a queue's remaining waiters in `XTaskQueueTerminate`. Neither is on main. No problems found in this diff itself. Recommendation: close (duplicate of #315). 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 09:08:42 +00:00 (Migrated from github.com)

Imported from ivall/xcl. Originally posted by throny on 2026-12-02T09:08:42Z.


Two contributors checked independently and found that this PR makes the same change as #315 #1276: #1607, group D170. They suggest keeping #315. If you agree, closing this PR or joining the other one would save the maintainers a review. If the two differ in a way the checks missed, a note in #1607 helps.

This is a hint from the community triage in #1607, not a maintainer decision.

AI use: Claude Code (Claude Opus 5.5), posted by a script from the #1607 table.

<!-- greyfinch-sync:ivall/xcl:comment:7340443124 --> Imported from [ivall/xcl](https://github.com/xcl-dev/xcl/pull/1239#issuecomment-7340443124). Originally posted by [throny](https://github.com/throny) on 2026-12-02T09:08:42Z. --- <!-- xcl-1607-hint:D170 --> Two contributors checked independently and found that this PR makes the same change as #315 #1276: #1607, group D170. They suggest keeping #315. If you agree, closing this PR or joining the other one would save the maintainers a review. If the two differ in a way the checks missed, a note in #1607 helps. This is a hint from the community triage in #1607, not a maintainer decision. AI use: Claude Code (Claude Opus 5.5), posted by a script from the #1607 table.
throny commented 2026-12-02 10:17:45 +00:00 (Migrated from github.com)

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


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:7342257437 --> Imported from [ivall/xcl](https://github.com/xcl-dev/xcl/pull/1239#issuecomment-7342257437). Originally posted by [throny](https://github.com/throny) on 2026-12-02T10:17:45Z. --- 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!1229
No description provided.