XAsync: end the call that finished, not whatever call the block holds now #122

Manually merged
github merged 1 commit from gh-pr/122/head into gh-pr/122/base 2026-11-26 03:51:06 +00:00
Owner

@⁠Dufftwt on GitHub · 2026-11-24 00:41 UTC · original
Dufftwt:fix/xasync-ended-call-reused-block → main · +155 −14 · 1 files · 1 commits · merged 2026-11-25 00:07 UTC

What's wrong

Every GDK async API (saves, users, the store, package queries, ...) runs through XAsync. A title passes an XAsyncBlock, and the call runs until it is completed. Titles often keep one block and reuse it for the next call as soon as the previous one has finished: from the completion routine, or from a loop that polls XAsyncGetStatus.

In xasync.rs, our own code finds "the call on this block" by the block's address each time it needs it. That breaks as soon as a block is reused, because by then the address points at the new call. It goes wrong in two places.

1. A stale result ends the next call. Providers normally finish their call themselves: DoWork calls XAsyncComplete(block, S_OK, size) and then returns S_OK. Here is what happens next:

DoWork      -> XAsyncComplete(block, S_OK)   call A is over
            -> completion routine runs       title begins call B on the same block
DoWork returns S_OK
our worker  -> XAsyncComplete(block, S_OK)   looks up the block -> finds B -> ends B

Call B is reported as finished with S_OK and no result before it has done anything. Its cleanup runs, and the completion routine fires a second time. XAsyncCancel does the same thing after the provider's Cancel (B ends with E_ABORT), and so does a failed Begin.

2. Stale queued work runs the next call's DoWork. If call A is canceled while its DoWork is still queued, and B is begun on the block, A's queued work item later looks up the block and runs B's DoWork.

For a title, this shows up as async calls that "succeed" with no data, completion routines that run twice, or work done twice. It depends on timing, so it looks random.

The reference XAsync (libHttpClient's AsyncLib.cpp) avoids this because each scheduled work item keeps a reference to its own call state, not to the user's block.

What the fix does

  • A queued DoWork now carries the call that scheduled it. When it runs, it checks that this call is still the block's current call. If the call is over (canceled, completed, cleaned up), it does nothing.
  • DoWork, XAsyncCancel and a failed XAsyncBegin now complete that same call, through a new complete_call(&call, ...). A call that is already over ignores a second complete, as it did before, so nothing reaches the next call.
  • XAsyncComplete itself is unchanged for providers. They still name their call by its block, which is correct while the call is in flight.

The logic of completing a call is unchanged. The body of XAsyncComplete just moved into complete_call, so it can be given the call directly.

How it behaves after the fix

  • Reusing a block right after a call finishes is safe. The new call runs and completes on its own, the old call's late result or queued work is dropped, and the completion routine runs once per call.
  • Calls that don't reuse blocks behave exactly as before.

Tests

There are two new tests in xasync.rs. Each uses a queue with a manual work port and an immediate completion port, so the race happens deterministically on one thread. The completion routine begins a second call on the same block, the way a title does:

  • a_finished_calls_work_result_leaves_the_next_call_alone: the provider completes from DoWork and returns S_OK. The next call must still be pending.
  • a_canceled_call_leaves_the_next_call_alone: the provider completes on cancel while its DoWork is still queued. The next call must still be pending, and its DoWork must not run when the old work item is dispatched.

I ran cargo test --workspace --no-fail-fast on GitHub's windows-latest with Rust 1.98.1 from rust-toolchain.toml:

  • This PR: both new tests pass; victory-hle has 83 passed and 0 failed.
  • The new tests alone on main: both fail. The next call is ended with S_OK (left: 0) and with E_ABORT (left: -2147467260) when it should still be E_PENDING.
  • In both runs the only other failure is victory-gpu's root::tests::host_serialized_blobs_create_root_signatures, which fails the same way on unmodified main on that runner (#36 changes that test).

Not tested: a run of the game itself.

Not changed here: pending_with also completes by block from its own thread. It has a related problem, because cancel frees its state while that thread may still use it. That needs a separate fix.

Related: #114 fixes a different problem in the same function (it runs a failed call's cleanup without holding the completion lock). The two fixes are compatible. They edit neighbouring lines of XAsyncComplete, whose body this PR moves into complete_call, so whichever lands second needs a small rebase. I'll do it if this one is second.

**@⁠Dufftwt** on GitHub · 2026-11-24 00:41 UTC · [original](https://github.com/Marvisak/VIctory/pull/122) `Dufftwt:fix/xasync-ended-call-reused-block` → `main` · +155 −14 · 1 files · 1 commits · merged 2026-11-25 00:07 UTC ## What's wrong Every GDK async API (saves, users, the store, package queries, ...) runs through XAsync. A title passes an `XAsyncBlock`, and the call runs until it is completed. Titles often keep **one block and reuse it** for the next call as soon as the previous one has finished: from the completion routine, or from a loop that polls `XAsyncGetStatus`. In `xasync.rs`, our own code finds "the call on this block" **by the block's address** each time it needs it. That breaks as soon as a block is reused, because by then the address points at the *new* call. It goes wrong in two places. **1. A stale result ends the next call.** Providers normally finish their call themselves: `DoWork` calls `XAsyncComplete(block, S_OK, size)` and then returns `S_OK`. Here is what happens next: ``` DoWork -> XAsyncComplete(block, S_OK) call A is over -> completion routine runs title begins call B on the same block DoWork returns S_OK our worker -> XAsyncComplete(block, S_OK) looks up the block -> finds B -> ends B ``` Call B is reported as finished with `S_OK` and no result before it has done anything. Its cleanup runs, and the completion routine fires a second time. `XAsyncCancel` does the same thing after the provider's `Cancel` (B ends with `E_ABORT`), and so does a failed `Begin`. **2. Stale queued work runs the next call's `DoWork`.** If call A is canceled while its `DoWork` is still queued, and B is begun on the block, A's queued work item later looks up the block and runs **B's** `DoWork`. For a title, this shows up as async calls that "succeed" with no data, completion routines that run twice, or work done twice. It depends on timing, so it looks random. The reference XAsync (libHttpClient's `AsyncLib.cpp`) avoids this because each scheduled work item keeps a reference to its own call state, not to the user's block. ## What the fix does - A queued `DoWork` now carries **the call that scheduled it**. When it runs, it checks that this call is still the block's current call. If the call is over (canceled, completed, cleaned up), it does nothing. - `DoWork`, `XAsyncCancel` and a failed `XAsyncBegin` now complete **that same call**, through a new `complete_call(&call, ...)`. A call that is already over ignores a second complete, as it did before, so nothing reaches the next call. - `XAsyncComplete` itself is unchanged for providers. They still name their call by its block, which is correct while the call is in flight. The logic of completing a call is unchanged. The body of `XAsyncComplete` just moved into `complete_call`, so it can be given the call directly. ## How it behaves after the fix - Reusing a block right after a call finishes is safe. The new call runs and completes on its own, the old call's late result or queued work is dropped, and the completion routine runs once per call. - Calls that don't reuse blocks behave exactly as before. ## Tests There are two new tests in `xasync.rs`. Each uses a queue with a manual work port and an immediate completion port, so the race happens deterministically on one thread. The completion routine begins a second call on the same block, the way a title does: - `a_finished_calls_work_result_leaves_the_next_call_alone`: the provider completes from `DoWork` and returns `S_OK`. The next call must still be pending. - `a_canceled_call_leaves_the_next_call_alone`: the provider completes on cancel while its `DoWork` is still queued. The next call must still be pending, and its `DoWork` must not run when the old work item is dispatched. I ran `cargo test --workspace --no-fail-fast` on GitHub's `windows-latest` with Rust 1.98.1 from `rust-toolchain.toml`: - **This PR:** both new tests pass; `victory-hle` has 83 passed and 0 failed. - **The new tests alone on `main`:** both fail. The next call is ended with `S_OK` (`left: 0`) and with `E_ABORT` (`left: -2147467260`) when it should still be `E_PENDING`. - In both runs the only other failure is `victory-gpu`'s `root::tests::host_serialized_blobs_create_root_signatures`, which fails the same way on unmodified `main` on that runner (#36 changes that test). **Not tested:** a run of the game itself. **Not changed here:** `pending_with` also completes by block from its own thread. It has a related problem, because cancel frees its state while that thread may still use it. That needs a separate fix. **Related:** #114 fixes a different problem in the same function (it runs a failed call's cleanup without holding the completion lock). The two fixes are compatible. They edit neighbouring lines of `XAsyncComplete`, whose body this PR moves into `complete_call`, so whichever lands second needs a small rebase. I'll do it if this one is second.
Our own completions looked their call up again by block address: after a
provider's DoWork returned, after a cancel, after a failed begin, and when
queued DoWork ran. A provider usually ends its call itself (XAsyncComplete
from DoWork or Cancel), and once it has, the title may begin a new call on
the same block, from its completion routine or from a thread that polls
the status. The lookup then found that new call:

- the old call's DoWork result (S_OK, size 0) or cancel (E_ABORT) ended
  the new call before it had done anything, ran its cleanup and fired the
  completion routine a second time;
- DoWork still queued for a canceled call ran the new call's DoWork.

Each queued DoWork now carries the call that scheduled it and runs only
while that call is still the block's, and DoWork, cancel and begin
complete that call. A call that is already over ignores a second
complete, so nothing reaches the next call. XAsyncComplete itself still
goes by block, as providers name their call by its block.
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 03:51:04 +00:00
github manually merged commit d01b17070d into gh-pr/122/base 2026-11-26 03:51:06 +00:00
Author
Owner

@⁠DaviTostes on GitHub · 2026-11-24 01:13 UTC · original

Tested d01b170: pass, no regression.

  • Tested the Windows build under Wine 11.22 + vkd3d-proton 3.1 on Arch Linux x86_64 (AMD RX 9060 XT, RADV 26.3.1), cargo xwin ... --target x86_64-pc-windows-msvc -p victory-hle.: 82 passed, 1 failed.
  • The one failure is modules::net::tests::only_loopback_is_reachable (Wine lacks WSAConnectByList, as in #118).

No game data used.

**@⁠DaviTostes** on GitHub · 2026-11-24 01:13 UTC · [original](https://github.com/Marvisak/VIctory/pull/122#issuecomment-7118881115) Tested `d01b170`: **pass, no regression**. - Tested the Windows build under **Wine 11.22 + vkd3d-proton 3.1** on Arch Linux x86_64 (AMD RX 9060 XT, RADV 26.3.1), `cargo xwin ... --target x86_64-pc-windows-msvc -p victory-hle`.: **82 passed, 1 failed**. - The one failure is `modules::net::tests::only_loopback_is_reachable` (Wine lacks `WSAConnectByList`, as in #118). No game data used.
This discussion has been locked. Commenting is limited to contributors.
No reviewers
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.

Dependencies

No dependencies set

Reference
github/Marvisak--VIctory!122
No description provided.