HLE: defer XAsync cleanup until in-flight provider ops return #3281
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!3281
Loading…
Reference in a new issue
No description provided.
Delete branch "github-bot/gh-3281"
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?
@fuzeofficlal on GitHub · 2026-11-26 15:40 UTC · original
fuzeofficlal:fix/hle-xasync-provider-lifecycle-sync→main· +608 −36 · 1 files · 1 commits · openSummary
Synchronize
XAsyncprovider operation lifetimes (OP_BEGIN,OP_DO_WORK, andOP_CANCEL) with completion andOP_CLEANUPincrates/victory-hle/src/modules/xasync.rsso cleanup and status publication cannot free provider context or the title'sAsyncBlockwhile another thread is still executing a provider callback.Problem
Previously,
Callcoordinated completion with a singleclaimed: AtomicBool, and neitherXAsyncBegin,XAsyncSchedule's worker (OP_DO_WORK), norXAsyncCancel(OP_CANCEL) tracked in-flight provider calls:OP_DO_WORK(context + block UAF): If a title callsXAsyncCancel(block)while a worker thread is executingOP_DO_WORK,XAsyncCancelimmediately runscomplete_call(E_ABORT, 0), which invokesOP_CLEANUP(freeing the provider's context) and publishes completion / invokes the completion callback (allowing the title to freeblock) whileOP_DO_WORKis still running on the worker thread.XAsyncCancel(OP_CANCELafter or duringOP_CLEANUP, duplicateOP_CANCEL):XAsyncCancelcheckscall.claimed.load(Acquire)non-atomically before callingOP_CANCEL. A concurrentXAsyncCompletecan runOP_CLEANUPeither beforeOP_CANCELenters or whileOP_CANCELis executing, and two concurrentXAsyncCancelcalls can invokeOP_CANCELtwice.OP_DO_WORKand completion duringOP_BEGIN: A scheduled worker that passesis_currentbefore a concurrent cancel/completion can still enterOP_DO_WORKafter cleanup, and an off-threadXAsyncCompletetriggered insideOP_BEGINcan runOP_CLEANUPbeforeOP_BEGINreturns.Fix
Call::claimedwithCall::lifecycle: Mutex<Lifecycle>tracking active provider op threads (active: Vec<ThreadId>), cancellation dispatch (canceling), the winning completion (pending), and one-shot finalization (finalizing), guarded by an RAIIOpGuard.OP_BEGINbefore insertingcallintoCALLS, and atomically gateOP_DO_WORKandOP_CANCELentry on!canceling && pending.is_none() && !finalizing(settingcanceling = trueonce forOP_CANCEL).complete_call, record the first winning(result, required_size). Finalize immediately when no other thread is inside a provider op (preserving synchronous reentrant completion fromOP_BEGIN/OP_DO_WORK/OP_CANCELon the same thread); otherwise defer finalization without blocking (preventing AB-BA deadlocks when a title holds a lock acrossXAsyncCancelthatOP_DO_WORKalso acquires) so the last activeOpGuardto exit runsfinalizeexactly once.Arc::ptr_eqinrelease_callbefore removing fromCALLSand invokingOP_CLEANUP, keepingCALLS,call.lifecycle, andcall.donestrictly unnested leaf locks never held across provider or title callbacks.Verification
crates/victory-hle/src/modules/xasync.rs(all three fail deterministically on63bcca43and pass with the fix, including 20 consecutive stress rounds):cancel_waits_for_in_flight_do_work_before_cleanup_and_completioncomplete_waits_for_in_flight_cancel_and_blocks_late_cancel(includes a 64-iteration 3-thread barrier race)concurrent_completion_defers_cleanup_during_begin_and_preserves_payload_on_cancelcargo test -p victory-hle --lockedand--all-features(155 unit + 2 integration tests passed).cargo clippy --workspace --all-targets --locked -- -D warningsand--all-features(0 warnings).cargo test -p xtask --locked policy::tests::repository_passes_policy(passed).@Dukestar69 on GitHub · 2026-11-26 15:48 UTC · original · approved these changes
Reviewed the diff and verified locally on Windows 11 x64:
OP_BEGIN/OP_DO_WORK/OP_CANCELwith completion claiming viaCall::lifecycleand RAIIOpGuardcleanly defersOP_CLEANUPand completion publication until the last in-flight provider op exits, preventing use-after-cleanup races across concurrent completion and cancellation.cargo test -p victory-hle --lib modules::xasync:: -- --test-threads=1: 16 passed, 0 failed (includingcancel_waits_for_in_flight_do_work_before_cleanup_and_completion,complete_waits_for_in_flight_cancel_and_blocks_late_cancel, andconcurrent_completion_defers_cleanup_during_begin_and_preserves_payload_on_cancel).cargo clippy -p victory-hle --all-targets -- -D warnings: clean.cargo xtask policy: 191 files ok.Synthetic tests only (no game files used). AI-assisted (Gemini 4 Neon).
@oni-swr on GitHub · 2026-11-26 16:55 UTC · original
Independent verification of #3281 (claimed on #1633). Test report only, no code changes. AI use: Claude Code (Claude Sonnet 5.5), run on behalf of oni-swr.
658b2fdeb90a19c1e2b8840351b444df0f6ec43f. Control:main=63bcca43c6a00a758510398fa43096ce7e4f7df0(the PR's merge base).modules::xasync::filter with--test-threads=1, and had no control on main, so I did not treat it as covering this.cargo test -j 4 -p victory-hle --lib --lockedon head and on the control;cargo test -j 4 -p victory-hle --lockedon head; the 3 new tests run 10 times on head and 7 times on the control.All three claimed tests exist at head:
cancel_waits_for_in_flight_do_work_before_cleanup_and_completion,complete_waits_for_in_flight_cancel_and_blocks_late_cancel,concurrent_completion_defers_cleanup_during_begin_and_preserves_payload_on_cancel.Do they catch a real bug on main? Yes, all three, 7 of 7 control runs, and they fail fast (assertions, about 0.00 s) rather than hanging on their barriers. Failure messages on main:
cleanup freed provider context while OP_DO_WORK was still running,cleanup ran while OP_CANCEL was still executing, and!begin_ctx.cleaned_during_begin(cleanup ran duringOP_BEGIN).No flakes seen. The PR description says the last test includes a "64-iteration 3-thread barrier race"; I ran it 10 times on head without a failure. Not verified: real titles, other CPUs or memory models, clippy and policy xtask.
View command line instructions
Checkout
From your project repository, check out a new branch and test the changes.Merge
Merge the changes and update on Forgejo.