XAsync: a canceled pending call's work touches neither freed state nor the next call #236
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!236
Loading…
Reference in a new issue
No description provided.
Delete branch "github-bot/gh-236"
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 01:42 UTC · original
Dufftwt:fix/xasync-canceled-pending-call→main· +63 −9 · 1 files · 1 commits · openWhat's wrong
xasync::pending_withruns an async call's work on a thread of its own and completes the call when the work returns.XGameUiShowTextEntryAsyncuses it: the work is the text-entry dialog, which runs until the user closes it.If the title cancels such a call,
XAsyncCancelcompletes it at once withE_ABORT. That releases the call, and the provider's cleanup frees its state. The work thread doesn't know: the dialog is still open. When it finally returns, the thread does two things wrong:XAsyncCompleteon the block, not on its call. By then the title may have begun its next call on the same block, as titles commonly do. That call is then completed with the old call's status and size, and its own work is still running:(If B's state happens to reuse A's freed allocation, B even returns A's text.)
What the fix does
Two changes in
pending_with, both contained in that function:Arcinstead of a raw pointer. Cleanup drops the call's reference; the thread keeps its own, so writing the result is always safe.OP_BEGINthe thread records which call it belongs to (theArc<Call>thatXAsyncBeginjust registered for the block). When the work returns, the thread completes the block only if that call is still the block's call. A canceled call has ended, so its late result is dropped.How it behaves after the fix
E_ABORT, as before.Tests
The new test
a_canceled_pending_call_leaves_the_next_call_alonefollows the sequence above. Each call's work waits on a channel until the test lets it finish:[9]).I ran
cargo test --workspaceon GitHub'swindows-latestwith Rust 1.98.1 fromrust-toolchain.toml:victory-hlehas 87 passed, the other crates have 0 failed.main: it fails at step 2. B's status isS_OK(left: 0) instead ofE_PENDING(right: -2147483638, i.e.0x8000000A): A's late completion ended B.--test-threads=1. With 4 threads, the shader crate's GPU tests sometimes crash on that runner (STATUS_ACCESS_VIOLATION), on unmodifiedmainas well.Not tested: a title canceling its text entry.
Related: #122 fixes the same pattern ("complete the call that finished, not whatever call the block holds now") for work scheduled with
XAsyncSchedule. This PR doesn't depend on it, and the two merge cleanly in either order. They also merge cleanly with #114 and #132.@marquezinii on GitHub · 2026-11-24 04:22 UTC · original
Reviewed
f0393dd. Sharing the state through anArcdoes remove the use-after-free, andArc::ptr_eqcannot be fooled by a reused address because the thread keepsminealive.cargo test -p victory-hle --libpasses on Windows 11.One window is left open. The worker checks that the block still holds its own call, and then
XAsyncComplete(block, hr, len)looks the call up again by block address. If the title cancels call A and begins call B on the same block between those two lookups, A's late result completes B, which is the case the PR sets out to prevent. With a 100 ms sleep inserted between the check and the complete (to widen the window, not committed), a test that cancels A and begins B meanwhile fails 5 of 5: B's status isS_OKinstead ofE_PENDING. Without the sleep the window is tiny, so the normal tests do not show it.A way to close it: complete through the
Arc<Call>the worker captured (an internalcomplete_call(block, &call, hr, len)that returns early when that call'sdoneis already set), so no second lookup by address happens. Cancel setsdoneon A, so A's late result is dropped.@Marvisak on GitHub · 2026-11-25 00:22 UTC · original
Automated triage for the maintainers.
Needs rebase. This PR does not merge cleanly into current
main(ac2525d). Conflicting files:crates/victory-hle/src/modules/xasync.rs.The content has not been reviewed yet; it will be once the branch is rebased onto
main.Generated by Claude Code
View command line instructions
Manual merge helper
Use this merge commit message when completing the merge manually.
Checkout
From your project repository, check out a new branch and test the changes.Merge
Merge the changes and update on Forgejo.