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

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

@⁠hajduzsolt on GitHub · 2026-11-24 17:56 UTC · original
hajduzsolt:xgameruntime/device-association-unregister-sync → main · +194 −21 · 1 files · 2 commits · closed without merging

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.
**@⁠hajduzsolt** on GitHub · 2026-11-24 17:56 UTC · [original](https://github.com/xcl-dev/xcl/pull/1583) `hajduzsolt:xgameruntime/device-association-unregister-sync` → `main` · +194 −21 · 1 files · 2 commits · closed without merging 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.
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 #1389

Assisted-by: Antigravity (Gemini 4 Hydrogen, high)
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 04:58:20 +00:00
github-bot closed this pull request 2026-11-26 04:58:20 +00:00
Author
Owner

@⁠pickles976 on GitHub · 2026-11-24 18:18 UTC · original

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.

**@⁠pickles976** on GitHub · 2026-11-24 18:18 UTC · [original](https://github.com/xcl-dev/xcl/pull/1583#issuecomment-7140467715) 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.
Author
Owner

@⁠hajduzsolt on GitHub · 2026-11-24 19:16 UTC · original

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

**@⁠hajduzsolt** on GitHub · 2026-11-24 19:16 UTC · [original](https://github.com/xcl-dev/xcl/pull/1583#issuecomment-7141866434) 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).
Author
Owner

@⁠DavidM1911 on GitHub · 2026-11-24 20:02 UTC · original

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!

**@⁠DavidM1911** on GitHub · 2026-11-24 20:02 UTC · [original](https://github.com/xcl-dev/xcl/pull/1583#issuecomment-7142966156) 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!
Author
Owner

@⁠throny on GitHub · 2026-11-25 00:27 UTC · original

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.

**@⁠throny** on GitHub · 2026-11-25 00:27 UTC · [original](https://github.com/xcl-dev/xcl/pull/1583#issuecomment-7148522880) <!-- xcl-1607-hint:D58 --> 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.
Author
Owner

@⁠JJAutoCompare on GitHub · 2026-11-25 05:14 UTC · original

Empirical test report (8e894c59)

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.

**@⁠JJAutoCompare** on GitHub · 2026-11-25 05:14 UTC · [original](https://github.com/xcl-dev/xcl/pull/1583#issuecomment-7153587454) ### Empirical test report (8e894c59) **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`: ```powershell $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.
Author
Owner

@⁠iggyvilla on GitHub · 2026-11-25 14:27 UTC · original

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

**@⁠iggyvilla** on GitHub · 2026-11-25 14:27 UTC · [original](https://github.com/xcl-dev/xcl/pull/1583#issuecomment-7165307874) Closing as a duplicate of #1418 (D39 in #1607, four agreeing checks).

Pull request closed

This discussion has been locked. Commenting is limited to contributors.
No description provided.