Skip to content

HLE: serialize device callbacks across delivery threads - #613

Closed
Quat3rnion wants to merge 2 commits into
Marvisak:mainfrom
Quat3rnion:victory-input-serialization
Closed

Quat3rnion wants to merge 2 commits into
Marvisak:mainfrom
Quat3rnion:victory-input-serialization

Conversation

@Quat3rnion

Copy link
Copy Markdown
Contributor

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.

@LAKSHYA-cmd-tech LAKSHYA-cmd-tech left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tested on native Windows 11 with Rust 1.98.1 (x86_64-pc-windows-msvc).

  • cargo test -p victory-hle: all 82 tests in �ictory_hle and
    o_dll_planting passed cleanly.
  • callbacks_from_registration_and_poll_are_serialized successfully verified that concurrent enumeration and poll threads serialize device callback delivery with a peak of 1.
  • cargo xtask policy: 150 files ok.

@Quat3rnion
Quat3rnion force-pushed the victory-input-serialization branch from b275dfa to 2750ab1 Compare November 24, 2026 07:24
@dpalacios69

Copy link
Copy Markdown
Contributor

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.

@geiyiliyabumo

Copy link
Copy Markdown
Contributor

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.

@MichaelSmithAI

Copy link
Copy Markdown

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

@mercuryVM mercuryVM left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tested 2750ab1 on Windows 11, RTX 5070, MSVC: cargo test -p victory-hle 88 passed, including callbacks_from_registration_and_poll_are_serialized.

The single queue does serialize callbacks, but it breaks blocking enumeration in the other ordering:

  • gameinput.rs (register_device, then Hub::deliver): when a blocking-enumeration RegisterDeviceCallback arrives while the poll thread is already inside deliver (delivering == true), the registration's found events are appended to queue and deliver returns at once. register_device then returns S_OK before any of its enumeration callbacks have run, and they run later on the poll thread. The doc comment above register_device promises they run "blocking on the caller's thread before we return". GameInput's blocking enumeration means the title's device list is complete when the call returns, and the title holds the lock its callback needs, so it can't observe the late callbacks until it releases that lock. The new test only covers registration-first-then-poll, not poll-first-then-register.
  • Possible fix: don't make the registration visible to the poll thread until its own enumeration is done. Deliver found synchronously on the caller's thread, then push the registration into state.callbacks and queue any hotplug events that arrived meanwhile. That keeps one-at-a-time delivery per callback and keeps the blocking guarantee. Waiting on a condvar for the queue to drain would deadlock in exactly the lock-holding case the comment describes.

AI-assisted (Claude Code).

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.
@Quat3rnion
Quat3rnion force-pushed the victory-input-serialization branch from 2750ab1 to 7da55be Compare November 24, 2026 14:13
@Quat3rnion

Copy link
Copy Markdown
Contributor Author

@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?

@mercuryVM mercuryVM left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Re-tested 7da55be on Windows 11, RTX 5070, MSVC: cargo test -p victory-hle 98 passed, including the new regression, where a blocking enumeration callback is held open on one thread while another connects a fake pad and polls. cargo clippy -p victory-hle --all-targets is clean.

This addresses my earlier review:

  • When a blocking registration arrives while another thread is delivering, its own enumeration events run inline on the caller's thread, and the registration sits in enumerating, invisible to the poll thread, until they're done. So RegisterDeviceCallback returns only after its enumeration ran.
  • Hotplug events that arrive in the meantime are held in the Enumeration's hotplug list and queued afterwards, keeping order per callback.
  • When no one is delivering, the caller claims delivery and drains synchronously, which also satisfies the blocking contract.

AI-assisted (Claude Code).

mercuryVM added a commit to mercuryVM/VIctory that referenced this pull request Nov 24, 2026
…s delivery threads

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@ThomasHFWright

Copy link
Copy Markdown

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.

Yaekai pushed a commit to Yaekai/VIctory that referenced this pull request Nov 24, 2026
github-actions Bot added a commit to KevinPratap/VIctory that referenced this pull request Nov 24, 2026
github-actions Bot added a commit to KevinPratap/VIctory that referenced this pull request Nov 24, 2026
github-actions Bot added a commit to KevinPratap/VIctory that referenced this pull request Nov 24, 2026

Copy link
Copy Markdown
Owner

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

github-actions Bot added a commit to KevinPratap/VIctory that referenced this pull request Nov 25, 2026
arnoudstorm pushed a commit to arnoudstorm/VIctory that referenced this pull request Nov 25, 2026
github-actions Bot added a commit to KevinPratap/VIctory that referenced this pull request Nov 25, 2026
@Marvisak

Copy link
Copy Markdown
Owner

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 Marvisak closed this Nov 25, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

8 participants