HLE GameInput: serialize blocking enumeration with polling and publish callback token first #3302

Open
fuzeofficlal wants to merge 1 commit from upstream-pr/3302 into main
fuzeofficlal commented 2026-11-26 16:30:33 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by fuzeofficlal on 2026-11-26T16:30:33Z.


Summary

Fixes two concurrency bugs in IGameInput::RegisterDeviceCallback in crates/victory-hle/src/modules/gameinput.rs under concurrent background polling (Hub::poll):

  1. Blocking enumeration vs. poll thread race and inverted event ordering:
    register_device inserted the new Registration into state.callbacks and released hub.state() before running hub.deliver(found) on the caller's thread. While blocking enumeration was in progress, the background poll thread could invoke the same callback concurrently and deliver a later disconnect transition before blocking enumeration had even delivered the initial connect snapshot (for example, delivering (device 1, DISCONNECTED) followed by the stale (device 1, CONNECTED)).
  2. Callback token *out publication race:
    register_device wrote *out only after publishing the registration and async-enumeration events under hub.state() and releasing the lock, allowing a concurrent poll callback to run before *out was written.

Changes

  • Write *out = registration.token before acquiring and releasing hub.state() so the mutex release-acquire edge publishes the token before any poll callback can observe the registration.
  • Track Registration::enumerating while blocking enumeration delivers its initial snapshot on the caller's thread (cleared by an Enumerating RAII guard). While enumerating is set for a registration, State::poll and State::update defer transitions for that registration into state.pending in FIFO order without stalling unrelated registrations; once blocking enumeration completes, the next poll delivers the deferred transitions in chronological order (or drops them if StopCallback/UnregisterCallback removed the registration).
  • Manage state.running in Hub::deliver with a Running RAII guard using rposition + swap_remove and releasing hub.state() before calling returned.notify_all(), and consolidate forget_locked into unregister_callback's lock acquisition.
  • Add deterministic barrier-synchronized regression tests covering blocking enumeration vs. concurrent multi-poll transitions, self-unregistration during blocking enumeration, concurrent UnregisterCallback waiting on blocking enumeration, and *out token visibility during async enumeration.

Verification

  • Confirmed deterministic baseline failures before the fix on blocking_enumeration_serializes_with_concurrent_poll_and_preserves_order (observed concurrent callback depth 2) and self_unregister_during_blocking_enumeration_cancels_remaining_and_deferred_events (observed concurrent disconnect delivery before self-unregister).
  • cargo test -p victory-hle --locked --lib modules::gameinput::tests:: (20 consecutive runs: 20/20 passed, 13/13 tests each)
  • cargo test -p victory-hle --locked and cargo test -p victory-hle --all-features --locked (all passed)
  • cargo clippy --workspace --all-targets --locked -- -D warnings and --all-features (0 warnings)
  • cargo test -p xtask --locked policy::tests::repository_passes_policy (passed)
<!-- greyfinch-sync:Marvisak/VIctory:issue:6630992455 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/3302). Originally posted by [fuzeofficlal](https://github.com/fuzeofficlal) on 2026-11-26T16:30:33Z. --- ## Summary Fixes two concurrency bugs in `IGameInput::RegisterDeviceCallback` in `crates/victory-hle/src/modules/gameinput.rs` under concurrent background polling (`Hub::poll`): 1. **Blocking enumeration vs. poll thread race and inverted event ordering**: `register_device` inserted the new `Registration` into `state.callbacks` and released `hub.state()` before running `hub.deliver(found)` on the caller's thread. While blocking enumeration was in progress, the background poll thread could invoke the same callback concurrently and deliver a later disconnect transition before blocking enumeration had even delivered the initial connect snapshot (for example, delivering `(device 1, DISCONNECTED)` followed by the stale `(device 1, CONNECTED)`). 2. **Callback token `*out` publication race**: `register_device` wrote `*out` only after publishing the registration and async-enumeration events under `hub.state()` and releasing the lock, allowing a concurrent poll callback to run before `*out` was written. ## Changes - Write `*out = registration.token` before acquiring and releasing `hub.state()` so the mutex release-acquire edge publishes the token before any poll callback can observe the registration. - Track `Registration::enumerating` while blocking enumeration delivers its initial snapshot on the caller's thread (cleared by an `Enumerating` RAII guard). While `enumerating` is set for a registration, `State::poll` and `State::update` defer transitions for that registration into `state.pending` in FIFO order without stalling unrelated registrations; once blocking enumeration completes, the next poll delivers the deferred transitions in chronological order (or drops them if `StopCallback`/`UnregisterCallback` removed the registration). - Manage `state.running` in `Hub::deliver` with a `Running` RAII guard using `rposition` + `swap_remove` and releasing `hub.state()` before calling `returned.notify_all()`, and consolidate `forget_locked` into `unregister_callback`'s lock acquisition. - Add deterministic barrier-synchronized regression tests covering blocking enumeration vs. concurrent multi-poll transitions, self-unregistration during blocking enumeration, concurrent `UnregisterCallback` waiting on blocking enumeration, and `*out` token visibility during async enumeration. ## Verification - Confirmed deterministic baseline failures before the fix on `blocking_enumeration_serializes_with_concurrent_poll_and_preserves_order` (observed concurrent callback depth 2) and `self_unregister_during_blocking_enumeration_cancels_remaining_and_deferred_events` (observed concurrent disconnect delivery before self-unregister). - `cargo test -p victory-hle --locked --lib modules::gameinput::tests::` (20 consecutive runs: 20/20 passed, 13/13 tests each) - `cargo test -p victory-hle --locked` and `cargo test -p victory-hle --all-features --locked` (all passed) - `cargo clippy --workspace --all-targets --locked -- -D warnings` and `--all-features` (0 warnings) - `cargo test -p xtask --locked policy::tests::repository_passes_policy` (passed)
Dukestar69 commented 2026-11-26 16:48:59 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by Dukestar69 on 2026-11-26T16:48:59Z.

Review: APPROVED


Reviewed the diff and verified locally on Windows 11 x64:

  • Serializing blocking device enumeration with concurrent polling via Registration::enumerating + RAII Running / Enumerating guards (and publishing the out-token before async callbacks run) cleanly preserves event ordering and unregister synchronization.
  • cargo test -p victory-hle --lib modules::gameinput:: -- --test-threads=1: 14 passed, 0 failed (including blocking_enumeration_serializes_with_concurrent_poll_and_preserves_order, concurrent_unregister_waits_for_blocking_enumeration_and_skips_remaining, out_token_is_visible_before_async_enumeration_callback_runs, and self_unregister_during_blocking_enumeration_cancels_remaining_and_deferred_events).
  • cargo clippy -p victory-hle --all-targets -- -D warnings: clean.
  • cargo xtask policy: 191 files ok.

Synthetic tests only (no game files used). AI-assisted (Gemini 4 Neon).

<!-- greyfinch-sync:Marvisak/VIctory:review:6077814550 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/3302#pullrequestreview-6077814550). Originally posted by [Dukestar69](https://github.com/Dukestar69) on 2026-11-26T16:48:59Z. **Review:** APPROVED --- Reviewed the diff and verified locally on Windows 11 x64: - Serializing blocking device enumeration with concurrent polling via `Registration::enumerating` + RAII `Running` / `Enumerating` guards (and publishing the out-token before async callbacks run) cleanly preserves event ordering and unregister synchronization. - `cargo test -p victory-hle --lib modules::gameinput:: -- --test-threads=1`: 14 passed, 0 failed (including `blocking_enumeration_serializes_with_concurrent_poll_and_preserves_order`, `concurrent_unregister_waits_for_blocking_enumeration_and_skips_remaining`, `out_token_is_visible_before_async_enumeration_callback_runs`, and `self_unregister_during_blocking_enumeration_cancels_remaining_and_deferred_events`). - `cargo clippy -p victory-hle --all-targets -- -D warnings`: clean. - `cargo xtask policy`: 191 files ok. Synthetic tests only (no game files used). AI-assisted (Gemini 4 Neon).
cloph-dsp commented 2026-11-26 16:50:08 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by cloph-dsp on 2026-11-26T16:50:08Z.


Independent Windows verification of PR #3302 (head 078bdca).

  • Environment: Windows, Rust 1.100.0/MSVC, per-worktree CARGO_TARGET_DIR=D:\Victory-Contrib\target-3302, CARGO_BUILD_JOBS=2.
  • Command: cargo test --locked -p victory-hle --lib -- --exact modules::gameinput::tests::blocking_enumeration_serializes_with_concurrent_poll_and_preserves_order modules::gameinput::tests::self_unregister_during_blocking_enumeration_cancels_remaining_and_deferred_events modules::gameinput::tests::concurrent_unregister_waits_for_blocking_enumeration_and_skips_remaining modules::gameinput::tests::out_token_is_visible_before_async_enumeration_callback_runs
  • Result: 4 passed; 0 failed; 152 filtered out, exit 0.
  • git diff --check origin/main HEAD clean.
  • Negative control: none of the four new test names exists on the merge base 63bcca43 (synthetic tests are added by this PR).
  • Scope: deterministic CPU/HLE barrier-synchronized scenarios; no real device enumeration or title run.
  • AI-assisted (Hy4 flash). GPU/driver: N/A (CPU-only). No game data or assets used.
<!-- greyfinch-sync:Marvisak/VIctory:comment:7198155707 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/3302#issuecomment-7198155707). Originally posted by [cloph-dsp](https://github.com/cloph-dsp) on 2026-11-26T16:50:08Z. --- Independent Windows verification of PR #3302 (head 078bdca). - Environment: Windows, Rust 1.100.0/MSVC, per-worktree `CARGO_TARGET_DIR=D:\Victory-Contrib\target-3302`, `CARGO_BUILD_JOBS=2`. - Command: `cargo test --locked -p victory-hle --lib -- --exact modules::gameinput::tests::blocking_enumeration_serializes_with_concurrent_poll_and_preserves_order modules::gameinput::tests::self_unregister_during_blocking_enumeration_cancels_remaining_and_deferred_events modules::gameinput::tests::concurrent_unregister_waits_for_blocking_enumeration_and_skips_remaining modules::gameinput::tests::out_token_is_visible_before_async_enumeration_callback_runs` - Result: `4 passed; 0 failed; 152 filtered out`, exit 0. - `git diff --check origin/main HEAD` clean. - Negative control: none of the four new test names exists on the merge base 63bcca43 (synthetic tests are added by this PR). - Scope: deterministic CPU/HLE barrier-synchronized scenarios; no real device enumeration or title run. - AI-assisted (Hy4 flash). GPU/driver: N/A (CPU-only). No game data or assets used.
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/3302:upstream-pr/3302'
git switch 'upstream-pr/3302'

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/3302'
git switch 'upstream-pr/3302'
git rebase 'main'
git switch 'main'
git merge --ff-only 'upstream-pr/3302'
git switch 'upstream-pr/3302'
git rebase 'main'
git switch 'main'
git merge --no-ff 'upstream-pr/3302'
git switch 'main'
git merge --squash 'upstream-pr/3302'
git switch 'main'
git merge --ff-only 'upstream-pr/3302'
git switch 'main'
git merge 'upstream-pr/3302'
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
Marvisak/VIctory!3302
No description provided.