HLE: serialize device callbacks across delivery threads #613
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/613"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Imported from Marvisak/VIctory. Originally posted by Quat3rnion on 2026-11-24T05:36:12Z.
What
The registration thread and the poll thread both called
deliverdirectly,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
UnregisterCallbackwait for a callbackthat is in flight.
An AI tool wrote part of this change.
Tested
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.cargo check --target x86_64-pc-windows-gnu --workspace --testspasses. No Windows host here.cargo xtask policypasses (150 files ok). No game files or data from them were added.Checklist
cargo xtask policypasses. No game files or data from them were added.Imported from Marvisak/VIctory. Originally posted by LAKSHYA-cmd-tech on 2026-11-24T06:01:50Z.
Review: APPROVED
Tested on native Windows 11 with Rust 1.98.1 (x86_64-pc-windows-msvc).
o_dll_planting passed cleanly.
Imported from Marvisak/VIctory. Originally posted by dpalacios69 on 2026-11-24T10:46:10Z.
Nice cleanup on serializing
Hub::deliver! One edge case to double-check forGameInputBlockingEnumeration:In
register_device_callback(gameinput.rs:781),hub.deliver(found)is called right before returningS_OK. With the new early return whenstate.deliveringis alreadytrue(gameinput.rs:416–445), if the poll thread (or another thread) is currently inside a callback,RegisterDeviceCallback(..., BlockingEnumeration, ...)appendsfoundtostate.queueand returnsS_OKbefore the initial enumeration callbacks have actually run.Per the public GameInput API contract,
GameInputBlockingEnumerationguarantees initial callbacks complete synchronously beforeRegisterDeviceCallbackreturns. (Waiting onself.returnedfrom 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
BlockingEnumerationshould wait for its queued batch to drain (with a regression test coveringpoll()active during registration), or documenting the behavior if titles don't depend on it here.Static code review only; AI-assisted.
Imported from Marvisak/VIctory. Originally posted by geiyiliyabumo on 2026-11-24T10:55:29Z.
Independent verification & code review on Windows 11 Pro (build 26100, Rust
1.98.1), cleanly merged ontomain(ffe310e):cargo xtask policy: PASS (154 files ok)cargo clippy -p victory-hle --all-targets --locked -- -D warnings: PASScargo clippy -p victory-hle --all-targets --features diagnostics --locked -- -D warnings: PASScargo test -p victory-hle --locked: 95 passed + 1 integration test (no_dll_planting), 0 failed (including newcallbacks_from_registration_and_poll_are_serialized)Diff review notes (
crates/victory-hle/src/modules/gameinput.rs)BlockingEnumerationcontract race (register_device->deliver): Whenregister_deviceis called withBlockingEnumeration(2) from thread B while thread A (e.g.poll) is currently inside a callback (state.delivering == true),deliver(found)appendsfoundtostate.queueand returns immediately — soRegisterDeviceCallbackreturnsS_OKbefore its initial enumeration callbacks have actually run. Trackingdelivering: Option<ThreadId>(instead ofbool) would let a blocking registration from another thread wait onself.returneduntil its queued events drain, while still returning without deadlocking if called re-entrantly from the delivering thread.deliverlines 435–452): Poppingstate.queue.pop_front(), checkingstate.callbacks, and pushing(r.token, me)tostate.runningcan happen under a singleself.state()lock acquisition instead of unlocking and immediately relocking. (Alsoforgetcould optionally prunestate.queuealongsidestate.pending, thoughdeliveralready skips unregistered tokens safely.)AI-assisted; synthetic tests only, no game files.
Imported from Marvisak/VIctory. Originally posted by mercuryVM on 2026-11-24T11:26:20Z.
Review: CHANGES_REQUESTED
Tested
2750ab1on Windows 11, RTX 5070, MSVC:cargo test -p victory-hle88 passed, includingcallbacks_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, thenHub::deliver): when a blocking-enumerationRegisterDeviceCallbackarrives while the poll thread is already insidedeliver(delivering == true), the registration'sfoundevents are appended toqueueanddeliverreturns at once.register_devicethen returnsS_OKbefore any of its enumeration callbacks have run, and they run later on the poll thread. The doc comment aboveregister_devicepromises 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.foundsynchronously on the caller's thread, then push the registration intostate.callbacksand 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).
Imported from Marvisak/VIctory. Originally posted by MichaelSmithAI on 2026-11-24T11:10:57Z.
We ran a focused test for the blocking-enumeration question above on exact head
2750ab1with Win64 under Wine. One thread held an existing callback open. A second thread calledRegisterDeviceCallbackthrough the GameInput vtable withGameInputBlockingEnumeration. It returnedS_OKwith 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
Imported from Marvisak/VIctory. Originally posted by mercuryVM on 2026-11-24T14:32:06Z.
Review: APPROVED
Re-tested
7da55beon Windows 11, RTX 5070, MSVC:cargo test -p victory-hle98 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-targetsis clean.This addresses my earlier review:
enumerating, invisible to the poll thread, until they're done. SoRegisterDeviceCallbackreturns only after its enumeration ran.Enumeration'shotpluglist and queued afterwards, keeping order per callback.AI-assisted (Claude Code).
Imported from Marvisak/VIctory. Originally posted by Quat3rnion on 2026-11-24T14:13:28Z.
@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-fastpassed all 54 tests on Linux at7da55bef. Could you re-check?Imported from Marvisak/VIctory. Originally posted by ThomasHFWright on 2026-11-24T15:08:28Z.
Re-tested the revised callback implementation at
7da55befe68142103d81110c5eaab47a8f4d6b43under Wine, with this PR's complete diff applied to currentmainffe310eb2098e52169f4c44db5ffdabf43a72622in an isolated checkout.All 9 GameInput fake-host tests pass, including:
callbacks_from_registration_and_poll_are_serializedblocking_enumeration_finishes_on_registering_thread_while_poll_deliversblocking_enumeration_reports_devices_connected_during_its_callbackThis 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:
ffe310eAll 14 Windows test executables were built with
cargo test --workspace --all-features --locked --offline --target x86_64-pc-windows-msvc --no-runand 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 Linuxcargo test -p xtaskseparately passes all four tests, asset policy passes, and Windows-target HLE Clippy with all targets/features and-D warningspasses.Environment: Rust 1.98.1, Wine 11.22, Linux, RTX 5090 / NVIDIA 620.73.04, vkd3d-proton
30b0eb9bc12dc9fwith app-local DXVK and natived3dcompiler_47for 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.
Imported from Marvisak/VIctory. Originally posted by Marvisak on 2026-11-25T01:28:34Z.
Automated triage for the maintainers.
Merge candidate.
Hub::deliver(crates/victory-hle/src/modules/gameinput.rs) is replaced by a queue inStatethat one thread drains (claim_delivery,drain,invoke). Callbacks frompolland from registration therefore no longer overlap.invokechecks the registration and marks it running under the same lock, soUnregisterCallbackstill 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_deviceruns that registration's initial callbacks on the calling thread, which avoids a deadlock. It holds back that registration's hotplug events inState::enumeratinguntil enumeration ends, soRegisterDeviceCallbackreturns 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:
forgetcould also prunestate.queue. It isn't needed for correctness, becauseinvokeskips unregistered tokens.CI: passing.
Generated by Claude Code
Imported from Marvisak/VIctory. Originally posted by Marvisak on 2026-11-25T21:59:07Z.
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, whichmaindoes. 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 publicRegisterDeviceCallbackpage 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