HLE/gameinput: write callback token before registration and order blocking enumeration #485

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

@⁠AscilCH on GitHub · 2026-11-25 01:23 UTC · original
AscilCH:fix/hle-gameinput-callback-registration-race → main · +156 −14 · 1 files · 1 commits · open

Problem

Two concurrency races in register_device (crates/victory-hle/src/modules/gameinput.rs):

  1. Callback invoked before *out receives the registration token:
    • register_device pushed registration into state.callbacks and released hub.state() before writing *out = registration.token. A concurrent poll() thread could invoke the callback before *out was written, racing with the title's token slot.
  2. Blocking enumeration overtaken by poll():
    • For blocking enumeration (enumeration != 0 && enumeration != ASYNC_ENUMERATION), register_device added registration to state.callbacks, released hub.state(), and then delivered the initial CONNECTED snapshot. A concurrent poll() noticing a disconnect in that window could deliver DISCONNECTED concurrently with or ahead of the initial CONNECTED callback, inverting the device state seen by the title.

Fix

  • Write *out = registration.token before locking hub.state() and inserting registration into state.callbacks.
  • Track tokens performing blocking enumeration in State::enumerating and queue any concurrent poll() events for those tokens into State::deferred (discarded on forget()).
  • Drain State::deferred for the token on the registering thread after initial enumeration finishes, clearing enumerating only once no deferred events remain (or the callback was unregistered), without ever holding hub.state() across user callback calls.
  • Add synthetic concurrency test blocking_enumeration_writes_token_first_and_orders_concurrent_poll.

Verification

  • cargo test -p victory-hle --locked gameinput::tests: 7 passed, 0 failed
  • cargo xtask policy: 169 files ok
  • cargo clippy -p victory-hle --all-targets --locked -- -D warnings: clean
  • cargo clippy -p victory-hle --all-targets --all-features --locked -- -D warnings: clean

(AI-assisted; synthetic unit tests only, no game data.)

**@⁠AscilCH** on GitHub · 2026-11-25 01:23 UTC · [original](https://github.com/fxceless/victory-fork/pull/485) `AscilCH:fix/hle-gameinput-callback-registration-race` → `main` · +156 −14 · 1 files · 1 commits · open ## Problem Two concurrency races in `register_device` (`crates/victory-hle/src/modules/gameinput.rs`): 1. **Callback invoked before `*out` receives the registration token**: - `register_device` pushed `registration` into `state.callbacks` and released `hub.state()` before writing `*out = registration.token`. A concurrent `poll()` thread could invoke the callback before `*out` was written, racing with the title's token slot. 2. **Blocking enumeration overtaken by `poll()`**: - For blocking enumeration (`enumeration != 0 && enumeration != ASYNC_ENUMERATION`), `register_device` added `registration` to `state.callbacks`, released `hub.state()`, and then delivered the initial `CONNECTED` snapshot. A concurrent `poll()` noticing a disconnect in that window could deliver `DISCONNECTED` concurrently with or ahead of the initial `CONNECTED` callback, inverting the device state seen by the title. ## Fix - Write `*out = registration.token` before locking `hub.state()` and inserting `registration` into `state.callbacks`. - Track tokens performing blocking enumeration in `State::enumerating` and queue any concurrent `poll()` events for those tokens into `State::deferred` (discarded on `forget()`). - Drain `State::deferred` for the token on the registering thread after initial enumeration finishes, clearing `enumerating` only once no deferred events remain (or the callback was unregistered), without ever holding `hub.state()` across user callback calls. - Add synthetic concurrency test `blocking_enumeration_writes_token_first_and_orders_concurrent_poll`. ## Verification - `cargo test -p victory-hle --locked gameinput::tests`: 7 passed, 0 failed - `cargo xtask policy`: 169 files ok - `cargo clippy -p victory-hle --all-targets --locked -- -D warnings`: clean - `cargo clippy -p victory-hle --all-targets --all-features --locked -- -D warnings`: clean *(AI-assisted; synthetic unit tests only, no game data.)*
- Write `*out = registration.token` before inserting the callback into
  `state.callbacks` in `register_device` so no callback invocation (blocking,
  async, or poll-driven) can observe an uninitialized token or race with the
  write to `*out`.
- Track tokens currently running blocking enumeration in `State::enumerating`
  and buffer any concurrent `poll()` transitions for those tokens in
  `State::deferred`, draining them on the registering thread before clearing
  `enumerating` (and dropping them on `forget()`). This prevents `poll()` from
  delivering `DISCONNECTED` ahead of or concurrently with initial `CONNECTED`
  enumeration without holding `hub.state()` across user callbacks.
- Add synthetic concurrency test
  `blocking_enumeration_writes_token_first_and_orders_concurrent_poll`.
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 03:44:26 +00:00
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/485/head:github-bot/gh-485'
git switch 'github-bot/gh-485'

Merge

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