XAsync: end the call that finished, not whatever call the block holds now #122
No reviewers
Labels
No labels
accessibility
area: build
area: formats
area: gpu
area: loader
area: runtime
area: shader
bug
documentation
duplicate
enhancement
good first issue
gpu: amd
gpu: intel
gpu: nvidia
help wanted
host: wine
invalid
out of scope
question
request
run report
triage: confirmed
triage: has patch
triage: merge candidate
triage: needs info
triage: needs rebase
triage: needs work
triage: proposed close
wontfix
No milestone
No assignees
1 participant
Notifications
Due date
No due date set.
Dependencies
No dependencies set
Reference
github/Marvisak--VIctory!122
Loading…
Reference in a new issue
No description provided.
Delete branch "gh-pr/122/head"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
@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 UTCWhat'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 pollsXAsyncGetStatus.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:
DoWorkcallsXAsyncComplete(block, S_OK, size)and then returnsS_OK. Here is what happens next:Call B is reported as finished with
S_OKand no result before it has done anything. Its cleanup runs, and the completion routine fires a second time.XAsyncCanceldoes the same thing after the provider'sCancel(B ends withE_ABORT), and so does a failedBegin.2. Stale queued work runs the next call's
DoWork. If call A is canceled while itsDoWorkis still queued, and B is begun on the block, A's queued work item later looks up the block and runs B'sDoWork.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
DoWorknow 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,XAsyncCanceland a failedXAsyncBeginnow complete that same call, through a newcomplete_call(&call, ...). A call that is already over ignores a second complete, as it did before, so nothing reaches the next call.XAsyncCompleteitself 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
XAsyncCompletejust moved intocomplete_call, so it can be given the call directly.How it behaves after the fix
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 fromDoWorkand returnsS_OK. The next call must still be pending.a_canceled_call_leaves_the_next_call_alone: the provider completes on cancel while itsDoWorkis still queued. The next call must still be pending, and itsDoWorkmust not run when the old work item is dispatched.I ran
cargo test --workspace --no-fail-faston GitHub'swindows-latestwith Rust 1.98.1 fromrust-toolchain.toml:victory-hlehas 83 passed and 0 failed.main: both fail. The next call is ended withS_OK(left: 0) and withE_ABORT(left: -2147467260) when it should still beE_PENDING.victory-gpu'sroot::tests::host_serialized_blobs_create_root_signatures, which fails the same way on unmodifiedmainon that runner (#36 changes that test).Not tested: a run of the game itself.
Not changed here:
pending_withalso 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 intocomplete_call, so whichever lands second needs a small rebase. I'll do it if this one is second.@DaviTostes on GitHub · 2026-11-24 01:13 UTC · original
Tested
d01b170: pass, no regression.cargo xwin ... --target x86_64-pc-windows-msvc -p victory-hle.: 82 passed, 1 failed.modules::net::tests::only_loopback_is_reachable(Wine lacksWSAConnectByList, as in #118).No game data used.