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

Merged
Dufftwt merged 1 commit from upstream-pr/122 into main 2026-11-25 00:07:47 +00:00
Dufftwt commented 2026-11-24 00:41:02 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by Dufftwt on 2026-11-24T00:41:02Z.


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.

<!-- greyfinch-sync:Marvisak/VIctory:issue:6571937216 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/122). Originally posted by [Dufftwt](https://github.com/Dufftwt) on 2026-11-24T00:41:02Z. --- ## 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.
DaviTostes commented 2026-11-24 06:54:52 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by DaviTostes on 2026-11-24T01:13:13Z.


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.

<!-- greyfinch-sync:Marvisak/VIctory:comment:7118881115 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/122#issuecomment-7118881115). Originally posted by [DaviTostes](https://github.com/DaviTostes) on 2026-11-24T01:13:13Z. --- 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.
upstream-sync closed this pull request 2026-11-25 01:02:43 +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!122
No description provided.