XAsync: canceling a call that already completed leaves it alone, as documented #1098

Merged
BastienRodz merged 1 commit from upstream-pr/1098 into main 2026-11-25 00:12:37 +00:00
BastienRodz commented 2026-11-24 15:43:04 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by BastienRodz on 2026-11-24T15:43:04Z.


What's wrong

Microsoft's XAsyncCancel reference states:

If the XAsyncCancel function is invoked for an asynchronous call that has already completed, nothing happens.

In crates/victory-hle/src/modules/xasync.rs, XAsyncCancel unconditionally dispatches OP_CANCEL whenever the call is still registered:

pub fn XAsyncCancel(block: *mut AsyncBlock) {
    if let Some(call) = call(block) {
        call.provider(OP_CANCEL, block, 0, std::ptr::null_mut());
        XAsyncComplete(block, E_ABORT, 0);
    }
}

A call that completed successfully with a nonempty result stays in CALLS until the title consumes the payload via XAsyncGetResult.

Sequence:

  1. A provider completes a call with S_OK and a 1-byte result.
  2. XAsyncGetStatus(block, false) returns S_OK.
  3. XAsyncCancel(block) is called before XAsyncGetResult.
  • Expected: the provider never receives OP_CANCEL; status stays S_OK; XAsyncGetResult returns the byte.
  • Actual: the provider receives OP_CANCEL once (then XAsyncComplete returns early because call.done is already true, so the status and result survive).

What the fix does

In XAsyncCancel, checks *lock(&call.done) before invoking the provider and returns immediately if the call has already completed. The lock guard is dropped at the end of the if condition before call.provider(...) runs, avoiding deadlock if a provider calls XAsyncComplete during OP_CANCEL. Updates XAsyncCancel's doc comment accordingly.

How it behaves after the fix

Calling XAsyncCancel on a call that has already completed returns without sending OP_CANCEL to the provider, leaving the status (S_OK) and unread result intact for XAsyncGetResult. In-flight calls still receive OP_CANCEL and complete with E_ABORT.

Tests

  • Added modules::xasync::tests::canceling_a_completed_call_leaves_it_alone in crates/victory-hle/src/modules/xasync.rs: completes a call with S_OK and a 1-byte result (0x5a), calls XAsyncCancel, checks that the provider's OP_CANCEL count stays 0, checks that XAsyncGetStatus stays S_OK, and verifies XAsyncGetResult still returns 0x5a (used == 1).
  • Fails on main before the fix:
    thread 'modules::xasync::tests::canceling_a_completed_call_leaves_it_alone' panicked at crates/victory-hle/src/modules/xasync.rs:1230:9:
    assertion `left == right` failed
      left: 1
     right: 0
    
  • Local Linux + Wine (cargo xwin, x86_64-pc-windows-msvc):
    • cargo xwin test -p victory-hle --target x86_64-pc-windows-msvc modules::xasync::tests: 8 passed, 0 failed.
    • cargo xwin test -p victory-hle --target x86_64-pc-windows-msvc: 92 passed, 3 failed (the 3 known Wine baseline failures: modules::lifecycle::tests::hooked_windows_still_reach_the_title_procedure, modules::lifecycle::tests::the_keyboard_menu_never_reaches_the_title, modules::net::tests::only_loopback_is_reachable).
    • cargo xwin clippy --workspace --all-targets --locked --target x86_64-pc-windows-msvc -- -D warnings: pass.
    • cargo xwin clippy --workspace --all-targets --all-features --locked --target x86_64-pc-windows-msvc -- -D warnings: pass.
    • cargo xtask policy: pass (asset policy: 154 files ok).
  • On GitHub's windows-latest with the upstream CI's commands (clippy -D warnings with and without --all-features, then cargo test --workspace --locked), everything passes; victory-hle 95 passed, 0 failed: https://github.com/BastienRodz/VIctory/actions/runs/45428269761

Not tested: a title's teardown path.

Related: #122, #236 (both touch XAsyncCancel's completion line; this change sits before it).

No game data used. AI-assisted: found with GPT-6.2 Astra, implemented with Gemini 4 Neon, reviewed and verified with Claude Code.

🤖 Generated with Claude Code

<!-- greyfinch-sync:Marvisak/VIctory:issue:6585624319 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/1098). Originally posted by [BastienRodz](https://github.com/BastienRodz) on 2026-11-24T15:43:04Z. --- ## What's wrong Microsoft's [`XAsyncCancel`](https://learn.microsoft.com/en-us/gaming/gdk/docs/reference/system/xasync/functions/xasynccancel) reference states: > If the `XAsyncCancel` function is invoked for an asynchronous call that has already completed, nothing happens. In `crates/victory-hle/src/modules/xasync.rs`, `XAsyncCancel` unconditionally dispatches `OP_CANCEL` whenever the call is still registered: ```rust pub fn XAsyncCancel(block: *mut AsyncBlock) { if let Some(call) = call(block) { call.provider(OP_CANCEL, block, 0, std::ptr::null_mut()); XAsyncComplete(block, E_ABORT, 0); } } ``` A call that completed successfully with a nonempty result stays in `CALLS` until the title consumes the payload via `XAsyncGetResult`. Sequence: 1. A provider completes a call with `S_OK` and a 1-byte result. 2. `XAsyncGetStatus(block, false)` returns `S_OK`. 3. `XAsyncCancel(block)` is called before `XAsyncGetResult`. - Expected: the provider never receives `OP_CANCEL`; status stays `S_OK`; `XAsyncGetResult` returns the byte. - Actual: the provider receives `OP_CANCEL` once (then `XAsyncComplete` returns early because `call.done` is already `true`, so the status and result survive). ## What the fix does In `XAsyncCancel`, checks `*lock(&call.done)` before invoking the provider and returns immediately if the call has already completed. The lock guard is dropped at the end of the `if` condition before `call.provider(...)` runs, avoiding deadlock if a provider calls `XAsyncComplete` during `OP_CANCEL`. Updates `XAsyncCancel`'s doc comment accordingly. ## How it behaves after the fix Calling `XAsyncCancel` on a call that has already completed returns without sending `OP_CANCEL` to the provider, leaving the status (`S_OK`) and unread result intact for `XAsyncGetResult`. In-flight calls still receive `OP_CANCEL` and complete with `E_ABORT`. ## Tests - Added `modules::xasync::tests::canceling_a_completed_call_leaves_it_alone` in `crates/victory-hle/src/modules/xasync.rs`: completes a call with `S_OK` and a 1-byte result (`0x5a`), calls `XAsyncCancel`, checks that the provider's `OP_CANCEL` count stays `0`, checks that `XAsyncGetStatus` stays `S_OK`, and verifies `XAsyncGetResult` still returns `0x5a` (`used == 1`). - Fails on `main` before the fix: ```text thread 'modules::xasync::tests::canceling_a_completed_call_leaves_it_alone' panicked at crates/victory-hle/src/modules/xasync.rs:1230:9: assertion `left == right` failed left: 1 right: 0 ``` - Local Linux + Wine (`cargo xwin`, `x86_64-pc-windows-msvc`): - `cargo xwin test -p victory-hle --target x86_64-pc-windows-msvc modules::xasync::tests`: 8 passed, 0 failed. - `cargo xwin test -p victory-hle --target x86_64-pc-windows-msvc`: 92 passed, 3 failed (the 3 known Wine baseline failures: `modules::lifecycle::tests::hooked_windows_still_reach_the_title_procedure`, `modules::lifecycle::tests::the_keyboard_menu_never_reaches_the_title`, `modules::net::tests::only_loopback_is_reachable`). - `cargo xwin clippy --workspace --all-targets --locked --target x86_64-pc-windows-msvc -- -D warnings`: pass. - `cargo xwin clippy --workspace --all-targets --all-features --locked --target x86_64-pc-windows-msvc -- -D warnings`: pass. - `cargo xtask policy`: pass (`asset policy: 154 files ok`). - On GitHub's `windows-latest` with the upstream CI's commands (clippy `-D warnings` with and without `--all-features`, then `cargo test --workspace --locked`), everything passes; `victory-hle` 95 passed, 0 failed: https://github.com/BastienRodz/VIctory/actions/runs/45428269761 Not tested: a title's teardown path. Related: #122, #236 (both touch XAsyncCancel's completion line; this change sits before it). No game data used. AI-assisted: found with GPT-6.2 Astra, implemented with Gemini 4 Neon, reviewed and verified with Claude Code. 🤖 Generated with [Claude Code](https://claude.com/claude-code)
Aniseto commented 2026-11-24 17:04:19 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by Aniseto on 2026-11-24T16:13:55Z.


Ran the new test and the whole xasync module at 44ce067:

Environment: Windows 11 Pro build 26200 x64, Intel Core i5-12400F, 64 GB RAM, NVIDIA GeForce RTX 5070 Ti (Blackwell, 32-lane waves only) driver 32.0.16.2124, Rust 1.98.1 (pinned by rust-toolchain.toml), MSVC 14.44.35207, DXC 1.9.2609.5, debug build.

cargo test -p victory-hle canceling_a_completed
1 passed; 0 failed; 0 ignored      (exit 0)

cargo test -p victory-hle xasync
8 passed; 0 failed; 0 ignored      (exit 0)

modules::xasync::tests::canceling_a_completed_call_leaves_it_alone passes and the rest of the module is unaffected. It is a unit-level test, so the adapter is not the variable; the datapoint is the pinned 1.98.1 toolchain. No test report on this PR yet.

Not tested: the title's own cancellation timing (no game data here), RDNA2/RDNA4 and Intel GPUs, release build.

AI-assisted (OpenCode), model: DeepSeek V4.1 Flash 1017 - checked out the PR's head commit in a detached worktree, built it, ran the tests, wrote this report; no game data was used.

<!-- greyfinch-sync:Marvisak/VIctory:comment:7137328516 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/1098#issuecomment-7137328516). Originally posted by [Aniseto](https://github.com/Aniseto) on 2026-11-24T16:13:55Z. --- Ran the new test and the whole `xasync` module at `44ce067`: Environment: Windows 11 Pro build 26200 x64, Intel Core i5-12400F, 64 GB RAM, NVIDIA GeForce RTX 5070 Ti (Blackwell, 32-lane waves only) driver 32.0.16.2124, Rust 1.98.1 (pinned by `rust-toolchain.toml`), MSVC 14.44.35207, DXC 1.9.2609.5, debug build. ``` cargo test -p victory-hle canceling_a_completed 1 passed; 0 failed; 0 ignored (exit 0) cargo test -p victory-hle xasync 8 passed; 0 failed; 0 ignored (exit 0) ``` `modules::xasync::tests::canceling_a_completed_call_leaves_it_alone` passes and the rest of the module is unaffected. It is a unit-level test, so the adapter is not the variable; the datapoint is the pinned 1.98.1 toolchain. No test report on this PR yet. Not tested: the title's own cancellation timing (no game data here), RDNA2/RDNA4 and Intel GPUs, release build. AI-assisted (OpenCode), model: **DeepSeek V4.1 Flash 1017** - checked out the PR's head commit in a detached worktree, built it, ran the tests, wrote this report; no game data was used.
Zipettte commented 2026-11-24 17:04:28 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by Zipettte on 2026-11-24T16:29:34Z.


Tested on AMD Radeon RX 7700 XT (RDNA3, gfx1101), driver 32.0.32029.2010, Windows 11, Rust 1.98.1 MSVC at 44ce067: cargo test -p victory-hle passes, 96 passed / 0 failed. Workspace baseline on main (ffe310e) is green on the same machine (342 passed).

Tested with AI assistance (Claude).

<!-- greyfinch-sync:Marvisak/VIctory:comment:7137749526 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/1098#issuecomment-7137749526). Originally posted by [Zipettte](https://github.com/Zipettte) on 2026-11-24T16:29:34Z. --- Tested on AMD Radeon RX 7700 XT (RDNA3, gfx1101), driver 32.0.32029.2010, Windows 11, Rust 1.98.1 MSVC at 44ce067: `cargo test -p victory-hle` passes, 96 passed / 0 failed. Workspace baseline on main (ffe310e) is green on the same machine (342 passed). _Tested with AI assistance (Claude)._
mercuryVM commented 2026-11-24 20:01:48 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by mercuryVM on 2026-11-24T19:58:54Z.

Review: APPROVED


Tested 44ce0678 on Windows 11, RTX 5070, MSVC:

  • cargo test -p victory-hle passes on the head (95 passed).
  • Merged into our fork: workspace tests, shader tests and clippy -D warnings (default and all features) are all clean.

The change matches the GDK reference. The done check runs under the call's own lock before OP_CANCEL is sent, so a completed call keeps its status and result. The test shows the provider never sees the cancel and the result is still readable.

AI-assisted (Claude Code).

<!-- greyfinch-sync:Marvisak/VIctory:review:6046401399 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/1098#pullrequestreview-6046401399). Originally posted by [mercuryVM](https://github.com/mercuryVM) on 2026-11-24T19:58:54Z. **Review:** APPROVED --- Tested 44ce0678 on Windows 11, RTX 5070, MSVC: - `cargo test -p victory-hle` passes on the head (95 passed). - Merged into our fork: workspace tests, shader tests and `clippy -D warnings` (default and all features) are all clean. The change matches the GDK reference. The `done` check runs under the call's own lock before `OP_CANCEL` is sent, so a completed call keeps its status and result. The test shows the provider never sees the cancel and the result is still readable. AI-assisted (Claude Code).
upstream-sync closed this pull request 2026-11-25 01:05:34 +00:00
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
Marvisak/VIctory!1098
No description provided.