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

Closed
hajduzsolt wants to merge 2 commits from upstream-pr/1583 into main
hajduzsolt commented 2026-11-24 17:56:32 +00:00 (Migrated from github.com)

Imported from ivall/xcl. Originally posted by hajduzsolt on 2026-11-24T17:56:32Z.


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.
<!-- greyfinch-sync:ivall/xcl:issue:6588103567 --> Imported from [ivall/xcl](https://github.com/xcl-dev/xcl/pull/1583). Originally posted by [hajduzsolt](https://github.com/hajduzsolt) on 2026-11-24T17:56:32Z. --- 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 commented 2026-11-24 19:07:23 +00:00 (Migrated from github.com)

Imported from ivall/xcl. Originally posted by pickles976 on 2026-11-24T18:18:52Z.


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.

<!-- greyfinch-sync:ivall/xcl:comment:7140467715 --> Imported from [ivall/xcl](https://github.com/xcl-dev/xcl/pull/1583#issuecomment-7140467715). Originally posted by [pickles976](https://github.com/pickles976) on 2026-11-24T18:18:52Z. --- 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.
hajduzsolt commented 2026-11-24 21:08:43 +00:00 (Migrated from github.com)

Imported from ivall/xcl. Originally posted by hajduzsolt on 2026-11-24T19:16:53Z.


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

<!-- greyfinch-sync:ivall/xcl:comment:7141866434 --> Imported from [ivall/xcl](https://github.com/xcl-dev/xcl/pull/1583#issuecomment-7141866434). Originally posted by [hajduzsolt](https://github.com/hajduzsolt) on 2026-11-24T19:16:53Z. --- 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 commented 2026-11-24 21:09:13 +00:00 (Migrated from github.com)

Imported from ivall/xcl. Originally posted by DavidM1911 on 2026-11-24T20:02:52Z.


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!

<!-- greyfinch-sync:ivall/xcl:comment:7142966156 --> Imported from [ivall/xcl](https://github.com/xcl-dev/xcl/pull/1583#issuecomment-7142966156). Originally posted by [DavidM1911](https://github.com/DavidM1911) on 2026-11-24T20:02:52Z. --- 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 commented 2026-11-25 01:10:42 +00:00 (Migrated from github.com)

Imported from ivall/xcl. Originally posted by throny on 2026-11-25T00:27:04Z.


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.

<!-- greyfinch-sync:ivall/xcl:comment:7148522880 --> Imported from [ivall/xcl](https://github.com/xcl-dev/xcl/pull/1583#issuecomment-7148522880). Originally posted by [throny](https://github.com/throny) on 2026-11-25T00:27:04Z. --- <!-- 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.
JJAutoCompare commented 2026-11-25 06:05:43 +00:00 (Migrated from github.com)

Imported from ivall/xcl. Originally posted by JJAutoCompare on 2026-11-25T05:14:55Z.


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.

<!-- greyfinch-sync:ivall/xcl:comment:7153587454 --> Imported from [ivall/xcl](https://github.com/xcl-dev/xcl/pull/1583#issuecomment-7153587454). Originally posted by [JJAutoCompare](https://github.com/JJAutoCompare) on 2026-11-25T05:14:55Z. --- ### 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.
upstream-sync closed this pull request 2026-11-25 15:05:44 +00:00
iggyvilla commented 2026-11-25 15:07:49 +00:00 (Migrated from github.com)

Imported from ivall/xcl. Originally posted by iggyvilla on 2026-11-25T14:27:29Z.


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

<!-- greyfinch-sync:ivall/xcl:comment:7165307874 --> Imported from [ivall/xcl](https://github.com/xcl-dev/xcl/pull/1583#issuecomment-7165307874). Originally posted by [iggyvilla](https://github.com/iggyvilla) on 2026-11-25T14:27:29Z. --- Closing as a duplicate of #1418 (D39 in #1607, four agreeing checks).

Pull request closed

Sign in to join this conversation.
No reviewers
No labels
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.

Reference
xcl-dev/xcl!1573
No description provided.