HLE: serialize device callbacks across delivery threads #613

Closed
github-bot wants to merge 2 commits from gh-pr/613/head into gh-pr/613/base
Owner

@⁠Quat3rnion on GitHub · 2026-11-24 05:36 UTC · original
Quat3rnion:victory-input-serialization → main · +270 −45 · 1 files · 2 commits · closed without merging

What

The registration thread and the poll thread both called deliver directly,
so an initial enumeration callback and a hotplug notification could run at
the same time. GameInput guarantees callbacks arrive one at a time in order,
and titles rely on that when maintaining a device list. Events now go through
one queue in the hub's state with a single delivery loop; a callback that
enqueues more events joins the same loop instead of nesting. The existing
running-callback tracking still lets UnregisterCallback wait for a callback
that is in flight.

An AI tool wrote part of this change.

Tested

  • Linux: cargo test --workspace: 165 passed, 0 failed, 3 ignored. A regression holds a blocking enumeration callback open on one thread while another thread connects a fake pad and polls, then checks peak simultaneous callbacks is one and that all notifications were delivered.
  • Windows cross-build: cargo check --target x86_64-pc-windows-gnu --workspace --tests passes. No Windows host here.
  • cargo xtask policy passes (150 files ok). No game files or data from them were added.

Checklist

  • Workspace tests and Windows cross-check pass.
  • cargo xtask policy passes. No game files or data from them were added.
  • AI authorship is disclosed above.
**@⁠Quat3rnion** on GitHub · 2026-11-24 05:36 UTC · [original](https://github.com/Marvisak/VIctory/pull/613) `Quat3rnion:victory-input-serialization` → `main` · +270 −45 · 1 files · 2 commits · closed without merging ## What The registration thread and the poll thread both called `deliver` directly, so an initial enumeration callback and a hotplug notification could run at the same time. GameInput guarantees callbacks arrive one at a time in order, and titles rely on that when maintaining a device list. Events now go through one queue in the hub's state with a single delivery loop; a callback that enqueues more events joins the same loop instead of nesting. The existing running-callback tracking still lets `UnregisterCallback` wait for a callback that is in flight. An AI tool wrote part of this change. ## Tested - Linux: `cargo test --workspace`: 165 passed, 0 failed, 3 ignored. A regression holds a blocking enumeration callback open on one thread while another thread connects a fake pad and polls, then checks peak simultaneous callbacks is one and that all notifications were delivered. - Windows cross-build: `cargo check --target x86_64-pc-windows-gnu --workspace --tests` passes. No Windows host here. - `cargo xtask policy` passes (150 files ok). No game files or data from them were added. ## Checklist - [x] Workspace tests and Windows cross-check pass. - [x] `cargo xtask policy` passes. No game files or data from them were added. - [x] AI authorship is disclosed above.
Registration and poll threads both called deliver directly, so an initial enumeration callback and a hotplug could run at the same time; GameInput guarantees one callback at a time in order. Events now go through one queue in Hub state, with a single thread draining it; callbacks that enqueue more events join the same loop instead of nesting. Add a regression that holds a registration callback open while another thread polls and checks peak concurrency is one.
Run blocking enumeration on the caller when another delivery is active. Hold hotplug events for that registration until enumeration ends, and track inline callbacks for unregistration.
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 04:10:04 +00:00
github-bot closed this pull request 2026-11-26 04:10:04 +00:00
Author
Owner

@⁠dpalacios69 on GitHub · 2026-11-24 10:46 UTC · original

Nice cleanup on serializing Hub::deliver! One edge case to double-check for GameInputBlockingEnumeration:

In register_device_callback (gameinput.rs:781), hub.deliver(found) is called right before returning S_OK. With the new early return when state.delivering is already true (gameinput.rs:416–445), if the poll thread (or another thread) is currently inside a callback, RegisterDeviceCallback(..., BlockingEnumeration, ...) appends found to state.queue and returns S_OK before the initial enumeration callbacks have actually run.

Per the public GameInput API contract, GameInputBlockingEnumeration guarantees initial callbacks complete synchronously before RegisterDeviceCallback returns. (Waiting on self.returned from a different thread would address that, though it would need care if an in-flight callback can ever synchronize with the registering thread to avoid a deadlock — and same-thread re-entrant registration must still return immediately.)

Worth considering whether BlockingEnumeration should wait for its queued batch to drain (with a regression test covering poll() active during registration), or documenting the behavior if titles don't depend on it here.

Static code review only; AI-assisted.

**@⁠dpalacios69** on GitHub · 2026-11-24 10:46 UTC · [original](https://github.com/Marvisak/VIctory/pull/613#issuecomment-7129219998) Nice cleanup on serializing `Hub::deliver`! One edge case to double-check for `GameInputBlockingEnumeration`: In `register_device_callback` (`gameinput.rs:781`), `hub.deliver(found)` is called right before returning `S_OK`. With the new early return when `state.delivering` is already `true` (`gameinput.rs:416–445`), if the poll thread (or another thread) is currently inside a callback, `RegisterDeviceCallback(..., BlockingEnumeration, ...)` appends `found` to `state.queue` and returns `S_OK` before the initial enumeration callbacks have actually run. Per the public GameInput API contract, `GameInputBlockingEnumeration` guarantees initial callbacks complete synchronously before `RegisterDeviceCallback` returns. (Waiting on `self.returned` from a different thread would address that, though it would need care if an in-flight callback can ever synchronize with the registering thread to avoid a deadlock — and same-thread re-entrant registration must still return immediately.) Worth considering whether `BlockingEnumeration` should wait for its queued batch to drain (with a regression test covering `poll()` active during registration), or documenting the behavior if titles don't depend on it here. Static code review only; AI-assisted.
Author
Owner

@⁠geiyiliyabumo on GitHub · 2026-11-24 10:55 UTC · original

Independent verification & code review on Windows 11 Pro (build 26100, Rust 1.98.1), cleanly merged onto main (ffe310e):

  • cargo xtask policy: PASS (154 files ok)
  • cargo clippy -p victory-hle --all-targets --locked -- -D warnings: PASS
  • cargo clippy -p victory-hle --all-targets --features diagnostics --locked -- -D warnings: PASS
  • cargo test -p victory-hle --locked: 95 passed + 1 integration test (no_dll_planting), 0 failed (including new callbacks_from_registration_and_poll_are_serialized)

Diff review notes (crates/victory-hle/src/modules/gameinput.rs)

  1. BlockingEnumeration contract race (register_device -> deliver): When register_device is called with BlockingEnumeration (2) from thread B while thread A (e.g. poll) is currently inside a callback (state.delivering == true), deliver(found) appends found to state.queue and returns immediately — so RegisterDeviceCallback returns S_OK before its initial enumeration callbacks have actually run. Tracking delivering: Option<ThreadId> (instead of bool) would let a blocking registration from another thread wait on self.returned until its queued events drain, while still returning without deadlocking if called re-entrantly from the delivering thread.
  2. Minor lock merge (deliver lines 435–452): Popping state.queue.pop_front(), checking state.callbacks, and pushing (r.token, me) to state.running can happen under a single self.state() lock acquisition instead of unlocking and immediately relocking. (Also forget could optionally prune state.queue alongside state.pending, though deliver already skips unregistered tokens safely.)

AI-assisted; synthetic tests only, no game files.

**@⁠geiyiliyabumo** on GitHub · 2026-11-24 10:55 UTC · [original](https://github.com/Marvisak/VIctory/pull/613#issuecomment-7129424157) Independent verification & code review on Windows 11 Pro (build 26100, Rust `1.98.1`), cleanly merged onto `main` (`ffe310e`): - `cargo xtask policy`: PASS (`154 files ok`) - `cargo clippy -p victory-hle --all-targets --locked -- -D warnings`: PASS - `cargo clippy -p victory-hle --all-targets --features diagnostics --locked -- -D warnings`: PASS - `cargo test -p victory-hle --locked`: **95 passed + 1 integration test (`no_dll_planting`), 0 failed** (including new `callbacks_from_registration_and_poll_are_serialized`) ### Diff review notes (`crates/victory-hle/src/modules/gameinput.rs`) 1. **`BlockingEnumeration` contract race (`register_device` -> `deliver`)**: When `register_device` is called with `BlockingEnumeration` (`2`) from thread B while thread A (e.g. `poll`) is currently inside a callback (`state.delivering == true`), `deliver(found)` appends `found` to `state.queue` and returns immediately — so `RegisterDeviceCallback` returns `S_OK` before its initial enumeration callbacks have actually run. Tracking `delivering: Option<ThreadId>` (instead of `bool`) would let a blocking registration from another thread wait on `self.returned` until its queued events drain, while still returning without deadlocking if called re-entrantly from the delivering thread. 2. **Minor lock merge (`deliver` lines 435–452)**: Popping `state.queue.pop_front()`, checking `state.callbacks`, and pushing `(r.token, me)` to `state.running` can happen under a single `self.state()` lock acquisition instead of unlocking and immediately relocking. (Also `forget` could optionally prune `state.queue` alongside `state.pending`, though `deliver` already skips unregistered tokens safely.) *AI-assisted; synthetic tests only, no game files.*
Author
Owner

@⁠MichaelSmithAI on GitHub · 2026-11-24 11:10 UTC · original

We ran a focused test for the blocking-enumeration question above on exact head 2750ab1 with Win64 under Wine. One thread held an existing callback open. A second thread called RegisterDeviceCallback through the GameInput vtable with GameInputBlockingEnumeration. It returned S_OK with zero initial callbacks completed. After the first callback was released, the initial callback ran once.

This gives a regression assertion for the queue change: on the registering thread, read the completed-callback count immediately after the API returns and expect one. The existing serialization test checks the complementary requirement that callbacks do not overlap. GameInput registration contract

**@⁠MichaelSmithAI** on GitHub · 2026-11-24 11:10 UTC · [original](https://github.com/Marvisak/VIctory/pull/613#issuecomment-7129763766) We ran a focused test for the blocking-enumeration question above on exact head `2750ab1` with Win64 under Wine. One thread held an existing callback open. A second thread called `RegisterDeviceCallback` through the GameInput vtable with `GameInputBlockingEnumeration`. It returned `S_OK` with **zero** initial callbacks completed. After the first callback was released, the initial callback ran once. This gives a regression assertion for the queue change: on the registering thread, read the completed-callback count immediately after the API returns and expect one. The existing serialization test checks the complementary requirement that callbacks do not overlap. [GameInput registration contract](https://learn.microsoft.com/en-us/gaming/gdk/docs/reference/input/gameinput/interfaces/igameinput/methods/igameinput_registerdevicecallback)
Author
Owner

@⁠Quat3rnion on GitHub · 2026-11-24 14:13 UTC · original

@⁠mercuryVM Blocking enumeration now finishes on the registering thread before returning, even when a poll callback is active. Hotplug events for that registration wait until enumeration finishes. Both cases have regression tests, and cargo test -p victory-hle --no-fail-fast passed all 54 tests on Linux at 7da55bef. Could you re-check?

**@⁠Quat3rnion** on GitHub · 2026-11-24 14:13 UTC · [original](https://github.com/Marvisak/VIctory/pull/613#issuecomment-7134031454) @⁠mercuryVM Blocking enumeration now finishes on the registering thread before returning, even when a poll callback is active. Hotplug events for that registration wait until enumeration finishes. Both cases have regression tests, and `cargo test -p victory-hle --no-fail-fast` passed all 54 tests on Linux at `7da55bef`. Could you re-check?
Author
Owner

@⁠ThomasHFWright on GitHub · 2026-11-24 15:08 UTC · original

Re-tested the revised callback implementation at 7da55befe68142103d81110c5eaab47a8f4d6b43 under Wine, with this PR's complete diff applied to current main ffe310eb2098e52169f4c44db5ffdabf43a72622 in an isolated checkout.

All 9 GameInput fake-host tests pass, including:

  • callbacks_from_registration_and_poll_are_serialized
  • blocking_enumeration_finishes_on_registering_thread_while_poll_delivers
  • blocking_enumeration_reports_devices_connected_during_its_callback

This exercises the revised completion/thread and hotplug-ordering assertions, rather than the older revision discussed in the earlier Wine report.

Full workspace unit/integration comparison:

Configuration Passed Failed Ignored
Unmodified ffe310e 341 5 6
Same main + this PR 344 5 6

All 14 Windows test executables were built with cargo test --workspace --all-features --locked --offline --target x86_64-pc-windows-msvc --no-run and run under Wine with --nocapture --test-threads=1. The HLE crate changes from 93 passed / 1 failed to 96 passed / 1 failed. No new failures appeared.

The five shared failures are the existing Wine missing-Winsock-export test (only_loopback_is_reachable), nested Cargo fixture build (loads_binds_and_calls_probe), empty pipeline library (a_rejected_blob_gives_an_empty_library, addressed separately by #347), shader-checksum rejection expectation (stub_pipeline_is_created), and Windows xtask's inability to run host Git (repository_passes_policy). Native Linux cargo test -p xtask separately passes all four tests, asset policy passes, and Windows-target HLE Clippy with all targets/features and -D warnings passes.

Environment: Rust 1.98.1, Wine 11.22, Linux, RTX 5090 / NVIDIA 620.73.04, vkd3d-proton 30b0eb9bc12dc9f with app-local DXVK and native d3dcompiler_47 for the GPU fixtures. Both full-suite configurations use the same runtime and compiler DLLs. Tests ran offline under Xvfb; the GameInput tests use fake devices.

Limits: no physical-controller or gameplay test, native Windows/WARP run, or Wine doctests. Some existing tests return early when optional console compilers or the DXIL validator are absent; the aggregate does not establish coverage of those paths. No game files or generated game data were used or attached.

AI-assisted testing with Codex.

**@⁠ThomasHFWright** on GitHub · 2026-11-24 15:08 UTC · [original](https://github.com/Marvisak/VIctory/pull/613#issuecomment-7135524549) Re-tested the revised callback implementation at `7da55befe68142103d81110c5eaab47a8f4d6b43` under Wine, with this PR's complete diff applied to current `main` `ffe310eb2098e52169f4c44db5ffdabf43a72622` in an isolated checkout. All **9 GameInput fake-host tests pass**, including: - `callbacks_from_registration_and_poll_are_serialized` - `blocking_enumeration_finishes_on_registering_thread_while_poll_delivers` - `blocking_enumeration_reports_devices_connected_during_its_callback` This exercises the revised completion/thread and hotplug-ordering assertions, rather than the older revision discussed in the earlier Wine report. Full workspace unit/integration comparison: | Configuration | Passed | Failed | Ignored | | --- | ---: | ---: | ---: | | Unmodified `ffe310e` | 341 | 5 | 6 | | Same main + this PR | 344 | 5 | 6 | All 14 Windows test executables were built with `cargo test --workspace --all-features --locked --offline --target x86_64-pc-windows-msvc --no-run` and run under Wine with `--nocapture --test-threads=1`. The HLE crate changes from **93 passed / 1 failed** to **96 passed / 1 failed**. No new failures appeared. The five shared failures are the existing Wine missing-Winsock-export test (`only_loopback_is_reachable`), nested Cargo fixture build (`loads_binds_and_calls_probe`), empty pipeline library (`a_rejected_blob_gives_an_empty_library`, addressed separately by #347), shader-checksum rejection expectation (`stub_pipeline_is_created`), and Windows xtask's inability to run host Git (`repository_passes_policy`). Native Linux `cargo test -p xtask` separately passes all four tests, asset policy passes, and Windows-target HLE Clippy with all targets/features and `-D warnings` passes. Environment: Rust 1.98.1, Wine 11.22, Linux, RTX 5090 / NVIDIA 620.73.04, vkd3d-proton `30b0eb9bc12dc9f` with app-local DXVK and native `d3dcompiler_47` for the GPU fixtures. Both full-suite configurations use the same runtime and compiler DLLs. Tests ran offline under Xvfb; the GameInput tests use fake devices. Limits: no physical-controller or gameplay test, native Windows/WARP run, or Wine doctests. Some existing tests return early when optional console compilers or the DXIL validator are absent; the aggregate does not establish coverage of those paths. No game files or generated game data were used or attached. AI-assisted testing with Codex.
Author
Owner

@⁠Marvisak on GitHub · 2026-11-25 01:28 UTC · original

Automated triage for the maintainers.

Merge candidate.

Hub::deliver (crates/victory-hle/src/modules/gameinput.rs) is replaced by a queue in State that one thread drains (claim_delivery, drain, invoke). Callbacks from poll and from registration therefore no longer overlap. invoke checks the registration and marks it running under the same lock, so UnregisterCallback still waits for a callback that is running.

The blocking-enumeration issue raised in review is fixed in 7da55bef. If another thread is already delivering, register_device runs that registration's initial callbacks on the calling thread, which avoids a deadlock. It holds back that registration's hotplug events in State::enumerating until enumeration ends, so RegisterDeviceCallback returns only after its initial callbacks have run. The three new tests cover serialization, completion on the registering thread, and a hotplug during enumeration.

Known trade-off: in that one inline case, the registering thread's enumeration callbacks can run at the same time as another registration's callback on the poll thread. Contention is the price of not blocking there.

Optional cleanup: forget could also prune state.queue. It isn't needed for correctness, because invoke skips unregistered tokens.

CI: passing.


Generated by Claude Code

**@⁠Marvisak** on GitHub · 2026-11-25 01:28 UTC · [original](https://github.com/Marvisak/VIctory/pull/613#issuecomment-7149588486) <!-- victory-triage v1 head=7da55befe68142103d81110c5eaab47a8f4d6b43 verdict=merge-candidate at=2026-11-25T01:28:33Z --> Automated triage for the maintainers. **Merge candidate.** `Hub::deliver` (`crates/victory-hle/src/modules/gameinput.rs`) is replaced by a queue in `State` that one thread drains (`claim_delivery`, `drain`, `invoke`). Callbacks from `poll` and from registration therefore no longer overlap. `invoke` checks the registration and marks it running under the same lock, so `UnregisterCallback` still waits for a callback that is running. The blocking-enumeration issue raised in review is fixed in `7da55bef`. If another thread is already delivering, `register_device` runs that registration's initial callbacks on the calling thread, which avoids a deadlock. It holds back that registration's hotplug events in `State::enumerating` until enumeration ends, so `RegisterDeviceCallback` returns only after its initial callbacks have run. The three new tests cover serialization, completion on the registering thread, and a hotplug during enumeration. Known trade-off: in that one inline case, the registering thread's enumeration callbacks can run at the same time as another registration's callback on the poll thread. Contention is the price of not blocking there. Optional cleanup: `forget` could also prune `state.queue`. It isn't needed for correctness, because `invoke` skips unregistered tokens. CI: passing. --- _Generated by [Claude Code](https://claude.ai/code)_
Author
Owner

@⁠Marvisak on GitHub · 2026-11-25 21:59 UTC · original

Automated triage for the maintainers, second look (a review against current main, with builds and tests where they could change the verdict).

Closing because what the title needs from device callbacks is already in main, and this adds risk on a path every boot runs. The title's two registrations are blocking, and the keyboard one is made under a lock that its own callback takes again, so the enumeration has to run on the registering thread, which main does. The races closed here need a pad to connect or disconnect in the microseconds between a registration's snapshot and its enumeration. Against that, the new delivery state still runs callbacks on two threads at once in the inline case, and the registering thread can end up draining other registrations' callbacks while it holds that lock. The public RegisterDeviceCallback page does not promise one callback at a time across registrations either.

If this is mistaken, reply here with what was missed and it will be looked at again. Thank you for the contribution.

**@⁠Marvisak** on GitHub · 2026-11-25 21:59 UTC · [original](https://github.com/Marvisak/VIctory/pull/613#issuecomment-7175789708) Automated triage for the maintainers, second look (a review against current `main`, with builds and tests where they could change the verdict). Closing because what the title needs from device callbacks is already in `main`, and this adds risk on a path every boot runs. The title's two registrations are blocking, and the keyboard one is made under a lock that its own callback takes again, so the enumeration has to run on the registering thread, which `main` does. The races closed here need a pad to connect or disconnect in the microseconds between a registration's snapshot and its enumeration. Against that, the new delivery state still runs callbacks on two threads at once in the inline case, and the registering thread can end up draining other registrations' callbacks while it holds that lock. The public `RegisterDeviceCallback` page does not promise one callback at a time across registrations either. If this is mistaken, reply here with what was missed and it will be looked at again. Thank you for the contribution.

Pull request closed

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!613
No description provided.