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

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

@⁠fuzeofficlal on GitHub · 2026-11-26 16:30 UTC · original
fuzeofficlal:fix/hle-gameinput-device-callback-sync → main · +545 −23 · 1 files · 1 commits · open

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)
**@⁠fuzeofficlal** on GitHub · 2026-11-26 16:30 UTC · [original](https://github.com/Marvisak/VIctory/pull/3302) `fuzeofficlal:fix/hle-gameinput-device-callback-sync` → `main` · +545 −23 · 1 files · 1 commits · open ## 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)
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 16:33:43 +00:00
Author
Owner

@⁠Dukestar69 on GitHub · 2026-11-26 16:48 UTC · original · approved these changes

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

**@⁠Dukestar69** on GitHub · 2026-11-26 16:48 UTC · [original](https://github.com/Marvisak/VIctory/pull/3302#pullrequestreview-6077814550) · **approved these changes** 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).
Author
Owner

@⁠cloph-dsp on GitHub · 2026-11-26 16:50 UTC · original

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.
**@⁠cloph-dsp** on GitHub · 2026-11-26 16:50 UTC · [original](https://github.com/Marvisak/VIctory/pull/3302#issuecomment-7198155707) 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 can be merged automatically.
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/3302/head:github-bot/gh-3302'
git switch 'github-bot/gh-3302'

Merge

Merge the changes and update on Forgejo.
git switch 'main'
git merge --no-ff 'github-bot/gh-3302'
git switch 'github-bot/gh-3302'
git rebase 'main'
git switch 'main'
git merge --ff-only 'github-bot/gh-3302'
git switch 'github-bot/gh-3302'
git rebase 'main'
git switch 'main'
git merge --no-ff 'github-bot/gh-3302'
git switch 'main'
git merge --squash 'github-bot/gh-3302'
git switch 'main'
git merge --ff-only 'github-bot/gh-3302'
git switch 'main'
git merge 'github-bot/gh-3302'
git push origin 'main'
This discussion has been locked. Commenting is limited to contributors.
No reviewers
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.

Dependencies

No dependencies set

Reference
github/Marvisak--VIctory!3302
No description provided.