Skip to content

XGameRuntime: honor wait/cancel in device association unregister (#1389) - #1583

Closed
hajduzsolt wants to merge 2 commits into
xcl-dev:mainfrom
hajduzsolt:xgameruntime/device-association-unregister-sync
Closed

hajduzsolt wants to merge 2 commits into
xcl-dev:mainfrom
hajduzsolt:xgameruntime/device-association-unregister-sync

Conversation

@hajduzsolt

@hajduzsolt hajduzsolt commented Nov 24, 2026 •

Copy link
Copy Markdown

Fixes use-after-free and sign-in/registration races in XUserRegisterForDeviceAssociationChanged / XUserUnregisterForDeviceAssociationChanged by tracking per-registration stopped and in_flight state with a Condvar and checking canceled in ^Gssociation_call. Fixes #1389.

What is in it

  • crates/xcl/src/hle/xgameruntime/mod.rs: replace the unsynchronized USER_SIGNED_IN + ASSOCIATION_CALLBACKS pair with a single ASSOCIATIONS mutex holding signed_in and AssociationEntry records; bail out of ^Gssociation_call when canceled || stopped, guard in_flight with Condvar wakeups, return false for unknown tokens, and wait while wait && in_flight > 0 on non-self unregister.
  • crates/xcl/src/hle/xgameruntime/mod.rs: add unit test device_association_unregister_and_cancel_do_not_invoke_or_race covering unregister before dispatch, XTaskQueueTerminate cancellation, double-unregister return value, and wait = true blocking until an in-flight callback exits.

Tested

  • Hardware & toolchain: AMD Radeon RX 9070 XT (driver 32.0.32029.2010), AMD Ryzen 7 9800X3D, Windows 11 10.0.26300.9974, rustc 1.98.1 (48a229cea 2026-09-01), debug (cargo test).
  • cargo test -p xcl --lib device_association -- --test-threads=2: 3 passed, 0 failed, 0 ignored.
  • cargo test --workspace -- --test-threads=2: 777 passed, 0 failed, 41 ignored (xcl --lib: 728 passed, 0 failed, 21 ignored; integration/e2e suites: 49 passed, 0 failed, 20 ignored).
  • SKIPPED runtime tests: SKIPPED heap_window_out_of_memory_falls_back_to_the_switch_form: no heap windows.

Regression run

main:

xcl-smoke v1
commit: c160d86ef2efe3a07e4dfb473e4fa72eafb413c0
gpu: AMD Radeon RX 9070 XT
title: A newer retail game
seconds: 60
exit: 0x0
frames shown: 1594 (PresentX 1594)
presents per 2 s (last 3): 49 48 14
device removals: 0
crashes: 0
peak commit: 20195 MB

PR:

xcl-smoke v1
commit: 8e894c59103c0da692e614d7d2299f3abe911430
gpu: AMD Radeon RX 9070 XT
title: A newer retail game
seconds: 60
exit: 0x0
frames shown: 1682 (PresentX 1682)
presents per 2 s (last 3): 60 60 60
device removals: 0
crashes: 0
peak commit: 14840 MB

Not tested

  • Forwarding through system xgameruntime.dll.

AI use

  • Antigravity (Gemini 4 Hydrogen, high): implemented serialized ASSOCIATIONS registry, canceled/stopped checks, Condvar wait synchronization on XUserUnregisterForDeviceAssociationChanged, added unit tests, ran the workspace test suite, and drafted commit and PR description.

@pickles976

Copy link
Copy Markdown

Reviewed the interleavings rather than the shape — this is correct, including the case that kills most removal-wait patterns:\n\n- fire-before-unregister: shim locks state, passes stopped check, in_flight += 1 in the same critical section → unregisters stop-then-wait sees in_flight == 1 and waits for the guard drop. No window between "is it stopped" and "am I counted" because both are one lock acquisition — which is exactly where my #836-lane incidents usually live; this PR gets it right by construction.\n- unregister-before-fire: queued record runs after stopped = true, returns under the same lock, the Box::from_raw record drops on the early return → freed exactly once.\n- unregister from inside the callback (reentrant): the s.owner != Some(me) clause is what most implementations get wrong — waiting on your own in-flight callback is a guaranteed self-deadlock, and the GDK contract forbids it; skipping the wait when owner == me matches wait=true semantics (unregister returns; the in-flight invocation completes normally).\n\nOne assumption worth a code comment while it is fresh: the single Option<ThreadId> owner slot presumes a given entry's notifications serialize on their one registered queue — true today (the queue is captured at register), but a future per-notification queue choice would make owner a set, and until that day the invariant is invisible. Suggest pinning it as a comment on AssociationCallState so the field cannot be "generalized" wrongly later. LGTM.

…-dev#1389)

association_call previously ignored task-queue cancellation and invoked the callback even after XUserUnregisterForDeviceAssociationChanged returned; XUserUnregisterForDeviceAssociationChanged ignored wait, and USER_SIGNED_IN raced with registration outside the mutex.

Track per-registration stopped/in-flight state with Condvar notification, bail on canceled or stopped callbacks, serialize signed_in under the registry lock, and block when wait=true until in-flight callbacks finish.

Fixes xcl-dev#1389

Assisted-by: Antigravity (Gemini 4 Hydrogen, high)
@hajduzsolt
hajduzsolt force-pushed the xgameruntime/device-association-unregister-sync branch from dac0255 to b027c8a Compare November 24, 2026 18:54
@hajduzsolt
hajduzsolt marked this pull request as ready for review November 24, 2026 19:11
@hajduzsolt
hajduzsolt marked this pull request as draft November 24, 2026 19:16
@hajduzsolt

Copy link
Copy Markdown
Author

Thanks for calling out the owner invariant. I documented the actual reason: the registry lock makes each registration eligible for exactly one notification, whether it registers before or after sign-in. A single queue alone would not guarantee serialization for thread-pool dispatch. cargo test --workspace -- --test-threads=2 passed (810 library tests passed, 22 ignored).

@DavidM1911

Copy link
Copy Markdown
Contributor

Verification on RTX 3060 Ti (Windows 11), branch merged with current main:

  • cargo test -p xcl --lib: all passed, 0 failed, ~22 ignored. (Intermittent single-test flake under parallel scheduling; clean on single-threaded rerun — same flake seen on unmodified main.)

Thanks!

@throny

throny commented Nov 25, 2026

Copy link
Copy Markdown
Collaborator

Two contributors checked independently and found that #1418 #1753 solves the same problem (for #1389) in a different way: #1607, group D58. You may want to compare them with the other author and agree on one, or say in the PR why both are needed.

This is a hint from the community triage in #1607, not a maintainer decision.

AI use: Claude Code (Claude Opus 5.5), posted by a script from the #1607 table.

@JJAutoCompare

Copy link
Copy Markdown

Empirical test report (8e894c5)

Hardware / environment

  • Windows 11 Enterprise Evaluation x64 build 26200.9974, AMD Ryzen 7 3800X, Intel Arc A770 16 GiB with driver 32.0.101.9175. The focused test ran on the CPU.
  • Rust: rustc 1.100.0 (0ef1bc386 2026-11-09)
  • DXC: dxcompiler.dll: 1.9(5512-01b62ad4)(1.9.2609.5) - 1.9.2609.5 (01b62ad47)
  • Tested commit: 8e894c59103c0da692e614d7d2299f3abe911430

Commands and results

  • cargo test --workspace --no-run exited 0 and compiled the workspace test executables. Test counts are inapplicable to this command.

  • Focused run, outside Invoke-ArcTest.ps1:

    $env:XCL_ADAPTER = 'NONE_CPU_ONLY'
    $env:RUST_TEST_NOCAPTURE = '1'
    cargo test -p xcl --lib hle::xgameruntime::tests::device_association_unregister_and_cancel_do_not_invoke_or_race -- --exact --nocapture 2>&1 | Tee-Object -FilePath 'L:\XclTooling\neon-slot-4\tmp\pr1583-focused.log'

    Cargo exited 0. 1 passed / 0 failed / 0 ignored / 0 skipped.

Observed behavior

The single unit test passed all three scenarios. Unregistering before dispatch suppressed the queued callback and a second unregister returned false. Queue termination suppressed the canceled callback. With wait enabled, unregister was still pending while the callback was held open and completed after the callback was released.

Not tested

Coverage was limited to workspace test compilation and the PR's focused CPU test. The full GPU workspace suite and game smoke runs were omitted.

AI use

Verified with T3 Code / Codex (gpt-6.2-sol, reasoningEffort: high). Coordinator #2 (T3 Code / Antigravity, gemini-4-neon) verified counts against pr1583-focused.log and published.

@iggyvilla

Copy link
Copy Markdown
Collaborator

Closing as a duplicate of #1418 (D39 in #1607, four agreeing checks).

@iggyvilla iggyvilla closed this Nov 25, 2026
rootw0rm pushed a commit to rootw0rm/xcl that referenced this pull request Nov 26, 2026
…cl-dev#1753, Windows verbatim + Linux twin)

Windows hle/xgameruntime/mod.rs is the upstream hunk verbatim
(reference-only on Linux): AssociationCall gains token + Queued/
Running/Done state + condvar, ASSOCIATION_CALLS liveset tracks
in-flight callbacks; association_call skips canceled/non-queued
records, unregister marks queued ones Done and with wait=true joins
running ones. Upstream test ported verbatim.

Linux twin hle/xuser_linux.rs mirrors the same state machine on
xtaskqueue_linux (win64 ABI, submit_now bool path) with a
Linux-shaped twin test (manual queue + dispatch + token+1 pin).

Triage per keeper (land ONE, xcl-dev#1753 preferred): xcl-dev#1583 closes with
citation (its +2 is a comment on its own competing stopped/in_flight
registry, which stays out); xcl-dev#1418 stays out (Entry/manager +
running_threads + unknown-token-false + re-entrant self-skip is the
competing design; xcl-dev#1753 keeps the existing always-true unregister).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

duplicate This issue or pull request already exists

Projects

None yet

Development

Successfully merging this pull request may close these issues.

XGameRuntime: use-after-free in XUserRegisterForDeviceAssociationChanged / Unregister

8 participants