GameInput: keep self-unregistered callbacks tracked until return and reap stopped entries #1439

Open
b3IKK wants to merge 1 commit from upstream-pr/1449 into main
b3IKK commented 2026-11-24 16:14:59 +00:00 (Migrated from github.com)

Imported from ivall/xcl. Originally posted by b3IKK on 2026-11-24T16:14:59Z.


When a GameInput callback called UnregisterCallback on itself, unregister_callback immediately removed the CallbackEntry from CALLBACKS even though that callback invocation was still running (in_flight == 1). If another thread then called UnregisterCallback(token, timeout) to wait for the callback to finish before freeing its context, it saw no entry in CALLBACKS and returned true immediately—allowing a use-after-free while the callback was still executing.

Similarly, calling StopCallback while an invocation was in flight left the stopped entry in CALLBACKS permanently if UnregisterCallback was never called later.

This changes CallbackEntry::unregister to return (unregistered, is_idle) so unregister_callback removes the entry only when in_flight == 0, and updates CallbackGuard::drop to remove stopped entries from CALLBACKS once in_flight drops to 0.

What is in it

  • crates/xcl/src/hle/gameinput.rs: track (unregistered, is_idle) in CallbackEntry::unregister / unregister_callback; reap stopped entries in CallbackGuard::drop when in_flight == 0; add unit tests unregister_after_self_unregister_waits_until_callback_returns and stop_callback_while_in_flight_cleans_up_entry_on_return (both verified failing 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): 772 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 gameinput: 11 passed, 0 failed (both new tests failed 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: 61187c0d12ddca7a63d3dc3a61e9d79e114918e4
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: 14595 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 gameinput.rs, wrote the fix and unit tests, ran the test suite and scripts/smoke.ps1 regression runs, and drafted this PR description.
<!-- greyfinch-sync:ivall/xcl:issue:5475953172 --> Imported from [ivall/xcl](https://github.com/xcl-dev/xcl/pull/1449). Originally posted by [b3IKK](https://github.com/b3IKK) on 2026-11-24T16:14:59Z. --- When a GameInput callback called `UnregisterCallback` on itself, `unregister_callback` immediately removed the `CallbackEntry` from `CALLBACKS` even though that callback invocation was still running (`in_flight == 1`). If another thread then called `UnregisterCallback(token, timeout)` to wait for the callback to finish before freeing its `context`, it saw no entry in `CALLBACKS` and returned `true` immediately—allowing a use-after-free while the callback was still executing. Similarly, calling `StopCallback` while an invocation was in flight left the stopped entry in `CALLBACKS` permanently if `UnregisterCallback` was never called later. This changes `CallbackEntry::unregister` to return `(unregistered, is_idle)` so `unregister_callback` removes the entry only when `in_flight == 0`, and updates `CallbackGuard::drop` to remove stopped entries from `CALLBACKS` once `in_flight` drops to `0`. **What is in it** - `crates/xcl/src/hle/gameinput.rs`: track `(unregistered, is_idle)` in `CallbackEntry::unregister` / `unregister_callback`; reap stopped entries in `CallbackGuard::drop` when `in_flight == 0`; add unit tests `unregister_after_self_unregister_waits_until_callback_returns` and `stop_callback_while_in_flight_cleans_up_entry_on_return` (both verified failing 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`): 772 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 gameinput`: 11 passed, 0 failed (both new tests failed 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: 61187c0d12ddca7a63d3dc3a61e9d79e114918e4 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: 14595 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 `gameinput.rs`, wrote the fix and unit tests, ran the test suite and `scripts/smoke.ps1` regression runs, and drafted this PR description.
futurepro62 commented 2026-11-24 17:06:40 +00:00 (Migrated from github.com)

Imported from ivall/xcl. Originally posted by futurepro62 on 2026-11-24T16:55:07Z.


Full-suite run, since this PR has no test report yet. Tested exact head 61187c0 (on its base b7aa9f3).

Tested

  • NVIDIA GeForce RTX 5070 Ti 16 GB (Blackwell, 32-lane waves only, mesh shaders), driver 616.64, Ryzen 7 9800X3D, Windows 11 Home 26300, Rust 1.100.0 MSVC, debug, D3D12 debug layer installed.
  • cargo build --workspace, then XCL_REQUIRE_GPU=mesh cargo test --workspace -- --test-threads=4: 772 passed, 0 failed, 41 ignored. SKIPPED (no wave64): gpu_geometry_wave32_prefix_count_over_a_mask, gpu_wave32_program_in_wave64_hardware.
  • The new tests ran and passed: unregister_after_self_unregister_waits_until_callback_returns, stop_callback_while_in_flight_cleans_up_entry_on_return.
  • Same head on the AMD Radeon iGPU of the 9800X3D (RDNA2, XCL_ADAPTER=radeon), cargo test --workspace -- --test-threads=4: 772 passed, 0 failed, 41 ignored, no skips.

Not tested

  • a release build, a game (test run only).

AI use

  • Claude Code (Claude Opus 5.5) ran the tests and wrote this report. I checked it before posting.
<!-- greyfinch-sync:ivall/xcl:comment:7138407751 --> Imported from [ivall/xcl](https://github.com/xcl-dev/xcl/pull/1449#issuecomment-7138407751). Originally posted by [futurepro62](https://github.com/futurepro62) on 2026-11-24T16:55:07Z. --- Full-suite run, since this PR has no test report yet. Tested exact head `61187c0` (on its base `b7aa9f3`). **Tested** - NVIDIA GeForce RTX 5070 Ti 16 GB (Blackwell, 32-lane waves only, mesh shaders), driver 616.64, Ryzen 7 9800X3D, Windows 11 Home 26300, Rust 1.100.0 MSVC, debug, D3D12 debug layer installed. - `cargo build --workspace`, then `XCL_REQUIRE_GPU=mesh cargo test --workspace -- --test-threads=4`: **772 passed, 0 failed, 41 ignored**. SKIPPED (no wave64): `gpu_geometry_wave32_prefix_count_over_a_mask`, `gpu_wave32_program_in_wave64_hardware`. - The new tests ran and passed: `unregister_after_self_unregister_waits_until_callback_returns`, `stop_callback_while_in_flight_cleans_up_entry_on_return`. - Same head on the AMD Radeon iGPU of the 9800X3D (RDNA2, `XCL_ADAPTER=radeon`), `cargo test --workspace -- --test-threads=4`: **772 passed, 0 failed, 41 ignored**, no skips. **Not tested** - a release build, a game (test run only). **AI use** - Claude Code (Claude Opus 5.5) ran the tests and wrote this report. I checked it before posting.
DavidM1911 commented 2026-11-24 19:07:42 +00:00 (Migrated from github.com)

Imported from ivall/xcl. Originally posted by DavidM1911 on 2026-11-24T18:52:44Z.


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

  • cargo test -p xcl --lib: all passed, 0 failed, ~21 ignored. (Intermittent single-test flake / 0xc0000005 abort under parallel scheduling; clean on single-threaded rerun — same flake seen on unmodified main.)

Thanks!

<!-- greyfinch-sync:ivall/xcl:comment:7141278946 --> Imported from [ivall/xcl](https://github.com/xcl-dev/xcl/pull/1449#issuecomment-7141278946). Originally posted by [DavidM1911](https://github.com/DavidM1911) on 2026-11-24T18:52:44Z. --- Verification on RTX 3060 Ti (Windows 11), branch merged with current `main`: - `cargo test -p xcl --lib`: all passed, 0 failed, ~21 ignored. (Intermittent single-test flake / 0xc0000005 abort under parallel scheduling; clean on single-threaded rerun — same flake seen on unmodified `main`.) Thanks!
S1m0ne1997 commented 2026-11-25 16:12:03 +00:00 (Migrated from github.com)

Imported from ivall/xcl. Originally posted by S1m0ne1997 on 2026-11-25T15:14:21Z.


Test report for 61187c0d12ddca7a63d3dc3a61e9d79e114918e4 (this PR's head), for #1607.

Tested

  • NVIDIA GeForce RTX 4090 24 GB (driver 621.40), AMD Ryzen 9 9950X3D, 47 GB RAM, Windows 11 Home 10.0.26300, rustc 1.100.0 (0ef1bc386 2026-11-09) x86_64-pc-windows-msvc, debug.
  • cargo build --workspace, then cargo test --workspace -- --test-threads=4 at the head: 772 passed, 0 failed, 41 ignored.
  • SKIPPED (2): gpu_geometry_wave32_prefix_count_over_a_mask (no wave64), gpu_wave32_program_in_wave64_hardware (no wave64).

Regression run (retail title, scripts/smoke.ps1 -Seconds 60, release; main built from the commit in its block, this PR from its head with no changes to crates/, both run with the scripts/smoke.ps1 of main, which hashes the name of the game folder)

main:

xcl-smoke v1
commit: 06c9f5505f63f10c5ec49ef932d75b2c28177324
gpu: NVIDIA GeForce RTX 4090
title: 9cc6a39e
seconds: 60
exit: 0x0
frames shown: 1599 (PresentX 1599)
presents per 2 s (last 3): 60 60 60
device removals: 0
crashes: 0
peak commit: 16384 MB

this PR:

xcl-smoke v1
commit: 61187c0d12ddca7a63d3dc3a61e9d79e114918e4
gpu: NVIDIA GeForce RTX 4090
title: 9cc6a39e
seconds: 60
exit: 0x0
frames shown: 1663 (PresentX 1663)
presents per 2 s (last 3): 60 60 60
device removals: 0
crashes: 0
peak commit: 17579 MB

Not tested

  • AMD, other NVIDIA cards and drivers, Linux. I did not review the diff; this report is the build, the suite and the regression run only.

AI use: Claude Code with Claude Opus 5.5 ran the build, the suite and the regression run and wrote this report at my request.

<!-- greyfinch-sync:ivall/xcl:comment:7166522067 --> Imported from [ivall/xcl](https://github.com/xcl-dev/xcl/pull/1449#issuecomment-7166522067). Originally posted by [S1m0ne1997](https://github.com/S1m0ne1997) on 2026-11-25T15:14:21Z. --- Test report for `61187c0d12ddca7a63d3dc3a61e9d79e114918e4` (this PR's head), for #1607. **Tested** - NVIDIA GeForce RTX 4090 24 GB (driver 621.40), AMD Ryzen 9 9950X3D, 47 GB RAM, Windows 11 Home 10.0.26300, `rustc 1.100.0 (0ef1bc386 2026-11-09)` `x86_64-pc-windows-msvc`, debug. - `cargo build --workspace`, then `cargo test --workspace -- --test-threads=4` at the head: **772 passed, 0 failed, 41 ignored**. - SKIPPED (2): `gpu_geometry_wave32_prefix_count_over_a_mask` (no wave64), `gpu_wave32_program_in_wave64_hardware` (no wave64). **Regression run** (retail title, `scripts/smoke.ps1 -Seconds 60`, release; `main` built from the commit in its block, this PR from its head with no changes to `crates/`, both run with the `scripts/smoke.ps1` of `main`, which hashes the name of the game folder) main: ``` xcl-smoke v1 commit: 06c9f5505f63f10c5ec49ef932d75b2c28177324 gpu: NVIDIA GeForce RTX 4090 title: 9cc6a39e seconds: 60 exit: 0x0 frames shown: 1599 (PresentX 1599) presents per 2 s (last 3): 60 60 60 device removals: 0 crashes: 0 peak commit: 16384 MB ``` this PR: ``` xcl-smoke v1 commit: 61187c0d12ddca7a63d3dc3a61e9d79e114918e4 gpu: NVIDIA GeForce RTX 4090 title: 9cc6a39e seconds: 60 exit: 0x0 frames shown: 1663 (PresentX 1663) presents per 2 s (last 3): 60 60 60 device removals: 0 crashes: 0 peak commit: 17579 MB ``` **Not tested** - AMD, other NVIDIA cards and drivers, Linux. I did not review the diff; this report is the build, the suite and the regression run only. *AI use: Claude Code with Claude Opus 5.5 ran the build, the suite and the regression run and wrote this report at my request.*
github-actions[bot] commented 2026-11-29 20:42:48 +00:00 (Migrated from github.com)

Imported from ivall/xcl. Originally posted by github-actions[bot] on 2026-11-29T20:42:48Z.


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:7262298279 --> Imported from [ivall/xcl](https://github.com/xcl-dev/xcl/pull/1449#issuecomment-7262298279). Originally posted by [github-actions[bot]](https://github.com/apps/github-actions) on 2026-11-29T20:42:48Z. --- 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:47:43 +00:00 (Migrated from github.com)

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


Triage (#1607) – head 61187c0

No open or merged PR makes the same change. On main, unregister_callback drops an entry from CALLBACKS while a self-unregistered callback is still running, and a StopCallback during an invocation leaves the entry forever. The new tests cover both.
The PR merges cleanly, but the merged tree does not build its tests. main changed dispatch_reading_callbacks to (reading: &OwnedReading, previous: u64), and both new tests still call dispatch_reading_callbacks(GamepadState { .. }). A rebase is needed.
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:7302145399 --> Imported from [ivall/xcl](https://github.com/xcl-dev/xcl/pull/1449#issuecomment-7302145399). Originally posted by [Foniass](https://github.com/Foniass) on 2026-12-01T05:47:43Z. --- **Triage (#1607)** – head 61187c0 No open or merged PR makes the same change. On `main`, `unregister_callback` drops an entry from `CALLBACKS` while a self-unregistered callback is still running, and a `StopCallback` during an invocation leaves the entry forever. The new tests cover both. The PR merges cleanly, but the merged tree does not build its tests. `main` changed `dispatch_reading_callbacks` to `(reading: &OwnedReading, previous: u64)`, and both new tests still call `dispatch_reading_callbacks(GamepadState { .. })`. A rebase is needed. Recommendation: keep open. AI review: Claude Code (Claude Opus 5.5), diffs read only, not built or run; not checked by a human.
This pull request cannot be merged automatically due to conflicts.
Merge manually to resolve the conflicts.
View command line instructions

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u origin 'upstream-pr/1449:upstream-pr/1449'
git switch 'upstream-pr/1449'

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.

git switch 'main'
git merge --no-ff 'upstream-pr/1449'
git switch 'upstream-pr/1449'
git rebase 'main'
git switch 'main'
git merge --ff-only 'upstream-pr/1449'
git switch 'upstream-pr/1449'
git rebase 'main'
git switch 'main'
git merge --no-ff 'upstream-pr/1449'
git switch 'main'
git merge --squash 'upstream-pr/1449'
git switch 'main'
git merge --ff-only 'upstream-pr/1449'
git switch 'main'
git merge 'upstream-pr/1449'
git push origin 'main'
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!1439
No description provided.