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

Open
AscilCH wants to merge 1 commit from upstream-pr/485 into main
AscilCH commented 2026-11-25 01:23:11 +00:00 (Migrated from github.com)

Imported from fxceless/victory-fork. Originally posted by AscilCH on 2026-11-25T01:23:11Z.


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

<!-- greyfinch-sync:fxceless/victory-fork:issue:6594945033 --> Imported from [fxceless/victory-fork](https://github.com/fxceless/victory-fork/pull/485). Originally posted by [AscilCH](https://github.com/AscilCH) on 2026-11-25T01:23:11Z. --- ## 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.)*
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/485:upstream-pr/485'
git switch 'upstream-pr/485'

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/485'
git switch 'upstream-pr/485'
git rebase 'main'
git switch 'main'
git merge --ff-only 'upstream-pr/485'
git switch 'upstream-pr/485'
git rebase 'main'
git switch 'main'
git merge --no-ff 'upstream-pr/485'
git switch 'main'
git merge --squash 'upstream-pr/485'
git switch 'main'
git merge --ff-only 'upstream-pr/485'
git switch 'main'
git merge 'upstream-pr/485'
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
fxceless/victory-fork!485
No description provided.