victory-hle: publish XAsyncComplete status before dropping call lookup #579

Open
github-bot wants to merge 3 commits from github-bot/gh-579 into a-publier AGit
Owner

@⁠SalmanBarkat on GitHub · 2026-11-27 12:15 UTC · original
SalmanBarkat:fix/xasync-complete-release-order → a-publier · +68 −9 · 1 files · 3 commits · open

Summary

  • Fix a race in complete_call() (crates/victory-hle/src/modules/xasync.rs) where release(block) removed the AsyncBlock from CALLS before status.store(result, Ordering::Release) and *done = true ran. A concurrent XAsyncGetStatus(block, true) call could see call(block) == None, skip waiting on call.finished, and read E_PENDING even though wait = true.
  • Claim completion atomically via completing: AtomicBool before call.cleanup(block) runs so OP_CLEANUP executes outside call.done and CALLS while block still cannot be freed, then lock call.done to publish size/status, remove the entry from CALLS, and wake waiters. Document that OP_CLEANUP must not synchronously re-enter XAsyncGetStatus(block, true) for the same block.
  • Add status_waits_through_cleanup deterministic regression test that holds OP_CLEANUP open while XAsyncGetStatus(&raw mut block, true) waits and verifies both E_ABORT completion and single-invocation cleanup.

Verification

  • cargo clippy --workspace --all-targets --locked -- -D warnings (passed)
  • cargo clippy --workspace --all-targets --all-features --locked -- -D warnings (passed)
  • cargo test --locked -p victory-hle --lib modules::xasync (13 passed, 0 failed)
  • cargo test --locked -p victory-hle --lib -- --skip only_loopback_is_reachable --skip a_kept_blob_past_the_link_limit_is_copied (161 passed, 0 failed, 2 ignored, 2 sandbox-excluded)
  • cargo test -p xtask --locked (9 passed, 0 failed)
**@⁠SalmanBarkat** on GitHub · 2026-11-27 12:15 UTC · [original](https://github.com/Belobug/VIctory-Belobug-AMD/pull/579) `SalmanBarkat:fix/xasync-complete-release-order` → `a-publier` · +68 −9 · 1 files · 3 commits · open ## Summary - Fix a race in `complete_call()` (`crates/victory-hle/src/modules/xasync.rs`) where `release(block)` removed the `AsyncBlock` from `CALLS` before `status.store(result, Ordering::Release)` and `*done = true` ran. A concurrent `XAsyncGetStatus(block, true)` call could see `call(block) == None`, skip waiting on `call.finished`, and read `E_PENDING` even though `wait = true`. - Claim completion atomically via `completing: AtomicBool` before `call.cleanup(block)` runs so `OP_CLEANUP` executes outside `call.done` and `CALLS` while `block` still cannot be freed, then lock `call.done` to publish `size`/`status`, remove the entry from `CALLS`, and wake waiters. Document that `OP_CLEANUP` must not synchronously re-enter `XAsyncGetStatus(block, true)` for the same block. - Add `status_waits_through_cleanup` deterministic regression test that holds `OP_CLEANUP` open while `XAsyncGetStatus(&raw mut block, true)` waits and verifies both `E_ABORT` completion and single-invocation cleanup. ## Verification - `cargo clippy --workspace --all-targets --locked -- -D warnings` (passed) - `cargo clippy --workspace --all-targets --all-features --locked -- -D warnings` (passed) - `cargo test --locked -p victory-hle --lib modules::xasync` (13 passed, 0 failed) - `cargo test --locked -p victory-hle --lib -- --skip only_loopback_is_reachable --skip a_kept_blob_past_the_link_limit_is_copied` (161 passed, 0 failed, 2 ignored, 2 sandbox-excluded) - `cargo test -p xtask --locked` (9 passed, 0 failed)
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-27 12:22:31 +00:00
Author
Owner

@⁠lvndrpup on GitHub · 2026-11-27 12:54 UTC · original · reviewed

Tested on an RTX 3060 Ti PC (Windows 11, debug build), base 4eb6cfd against head df83f3c.

base 4eb6cfd head df83f3c
get_status_wait_never_observes_pending_after_error_completion, copied onto base 0 of 1,020 runs failed (510,000 iterations) 0 of 20 runs failed
probe below: XAsyncGetStatus(_, true) while OP_CLEANUP runs E_PENDING, 3 of 3 E_ABORT, 3 of 3
cargo test -p victory-hle --lib modules::xasync 13 passed
cargo clippy -p victory-hle --all-targets -- -D warnings, with and without --all-features clean

The race is real and this closes it. But the new test doesn't catch it on this machine: it passed on the base every time. The waiting thread almost always finds the call in CALLS before the worker gets to complete_call, so the window between the removal and the status store is rarely hit. Holding the worker inside that window makes it deterministic. This fails on the base and passes here, in 0.2 s:

#[test]
fn status_waits_through_cleanup() {
    use std::sync::atomic::AtomicBool;
    static IN_CLEANUP: AtomicBool = AtomicBool::new(false);
    extern "system" fn provider(op: u32, _: *const ProviderData) -> HRESULT {
        if op == OP_CLEANUP {
            IN_CLEANUP.store(true, Ordering::SeqCst);
            std::thread::sleep(std::time::Duration::from_millis(200));
        }
        S_OK
    }
    let mut block = AsyncBlock { queue: std::ptr::null(), context: std::ptr::null_mut(), callback: None, internal: [0; 4] };
    assert_eq!(XAsyncBegin(&mut block, std::ptr::null_mut(), std::ptr::null(), std::ptr::null(), Some(provider)), S_OK);
    let at = std::ptr::addr_of_mut!(block) as usize;
    let worker = std::thread::spawn(move || XAsyncComplete(at as *mut AsyncBlock, E_ABORT, 0));
    while !IN_CLEANUP.load(Ordering::SeqCst) {
        std::hint::spin_loop();
    }
    let got = XAsyncGetStatus(std::ptr::addr_of_mut!(block), true);
    worker.join().unwrap();
    assert_eq!(got, E_ABORT);
}

On the code itself: the new order takes CALLS while holding the call's done. Nothing takes them the other way round (call, is_current, release and XAsyncBegin hold CALLS only briefly), so it can't deadlock. The ptr::eq check keeps a new call that the title began on the same block from being removed, and cleaned keeps a later release from running OP_CLEANUP again. It merges cleanly with #461, the other open PR on xasync.rs that still merges with a-publier.

**@⁠lvndrpup** on GitHub · 2026-11-27 12:54 UTC · [original](https://github.com/Belobug/VIctory-Belobug-AMD/pull/579#pullrequestreview-6088444762) · **reviewed** Tested on an RTX 3060 Ti PC (Windows 11, debug build), base `4eb6cfd` against head `df83f3c`. | | base `4eb6cfd` | head `df83f3c` | |---|---|---| | `get_status_wait_never_observes_pending_after_error_completion`, copied onto base | 0 of 1,020 runs failed (510,000 iterations) | 0 of 20 runs failed | | probe below: `XAsyncGetStatus(_, true)` while `OP_CLEANUP` runs | `E_PENDING`, 3 of 3 | `E_ABORT`, 3 of 3 | | `cargo test -p victory-hle --lib modules::xasync` | | 13 passed | | `cargo clippy -p victory-hle --all-targets -- -D warnings`, with and without `--all-features` | | clean | The race is real and this closes it. But the new test doesn't catch it on this machine: it passed on the base every time. The waiting thread almost always finds the call in `CALLS` before the worker gets to `complete_call`, so the window between the removal and the status store is rarely hit. Holding the worker inside that window makes it deterministic. This fails on the base and passes here, in 0.2 s: ```rust #[test] fn status_waits_through_cleanup() { use std::sync::atomic::AtomicBool; static IN_CLEANUP: AtomicBool = AtomicBool::new(false); extern "system" fn provider(op: u32, _: *const ProviderData) -> HRESULT { if op == OP_CLEANUP { IN_CLEANUP.store(true, Ordering::SeqCst); std::thread::sleep(std::time::Duration::from_millis(200)); } S_OK } let mut block = AsyncBlock { queue: std::ptr::null(), context: std::ptr::null_mut(), callback: None, internal: [0; 4] }; assert_eq!(XAsyncBegin(&mut block, std::ptr::null_mut(), std::ptr::null(), std::ptr::null(), Some(provider)), S_OK); let at = std::ptr::addr_of_mut!(block) as usize; let worker = std::thread::spawn(move || XAsyncComplete(at as *mut AsyncBlock, E_ABORT, 0)); while !IN_CLEANUP.load(Ordering::SeqCst) { std::hint::spin_loop(); } let got = XAsyncGetStatus(std::ptr::addr_of_mut!(block), true); worker.join().unwrap(); assert_eq!(got, E_ABORT); } ``` On the code itself: the new order takes `CALLS` while holding the call's `done`. Nothing takes them the other way round (`call`, `is_current`, `release` and `XAsyncBegin` hold `CALLS` only briefly), so it can't deadlock. The `ptr::eq` check keeps a new call that the title began on the same block from being removed, and `cleaned` keeps a later `release` from running `OP_CLEANUP` again. It merges cleanly with #461, the other open PR on `xasync.rs` that still merges with `a-publier`.
Author
Owner

@⁠SalmanBarkat on GitHub · 2026-11-27 13:01 UTC · original

Thanks for the verification and the deterministic cleanup-window reproducer @⁠lvndrpup. Replaced the probabilistic loop with status_waits_through_cleanup (plus an assertion that OP_CLEANUP executes once) in b94b57a and re-ran Clippy + victory-hle tests cleanly.

**@⁠SalmanBarkat** on GitHub · 2026-11-27 13:01 UTC · [original](https://github.com/Belobug/VIctory-Belobug-AMD/pull/579#issuecomment-7218965385) Thanks for the verification and the deterministic cleanup-window reproducer @⁠lvndrpup. Replaced the probabilistic loop with `status_waits_through_cleanup` (plus an assertion that `OP_CLEANUP` executes once) in `b94b57a` and re-ran Clippy + `victory-hle` tests cleanly.
Author
Owner

@⁠gut0to on GitHub · 2026-11-27 13:05 UTC · original · reviewed

AI-assisted review. I traced the completion and cleanup lock ordering and left one reentrancy question inline.

**@⁠gut0to** on GitHub · 2026-11-27 13:05 UTC · [original](https://github.com/Belobug/VIctory-Belobug-AMD/pull/579#pullrequestreview-6088580829) · **reviewed** AI-assisted review. I traced the completion and cleanup lock ordering and left one reentrancy question inline.
@ -684,0 +691,4 @@
// `block`) and without holding `call.done` or `CALLS`. `OP_CLEANUP`
// must not synchronously re-enter `XAsyncGetStatus(block, true)` for
// the same block, as its status is not published until cleanup ends.
call.cleanup(block);
Author
Owner

@⁠gut0to on GitHub · 2026-11-27 13:05 UTC · original

AI-assisted review: This invokes the provider's OP_CLEANUP callback while complete_call still holds call.done. If cleanup can re-enter XAsyncGetStatus(block, true), it will wait on the same mutex and deadlock before status is published. Could you document that reentrancy is forbidden or restructure the in-progress state so cleanup can run without holding the waiter mutex?

**@⁠gut0to** on GitHub · 2026-11-27 13:05 UTC · [original](https://github.com/Belobug/VIctory-Belobug-AMD/pull/579#discussion_r4719734645) AI-assisted review: This invokes the provider's OP_CLEANUP callback while complete_call still holds call.done. If cleanup can re-enter XAsyncGetStatus(block, true), it will wait on the same mutex and deadlock before status is published. Could you document that reentrancy is forbidden or restructure the in-progress state so cleanup can run without holding the waiter mutex?
Author
Owner

@⁠SalmanBarkat on GitHub · 2026-11-27 13:12 UTC · original

Thanks for catching the lock scope @⁠gust0to. Moved completion claiming onto completing: AtomicBool in e55aa74 so OP_CLEANUP now runs without holding call.done or CALLS, and documented that OP_CLEANUP must not synchronously re-enter XAsyncGetStatus(block, true) on the same block.

**@⁠SalmanBarkat** on GitHub · 2026-11-27 13:12 UTC · [original](https://github.com/Belobug/VIctory-Belobug-AMD/pull/579#issuecomment-7219200821) Thanks for catching the lock scope @⁠gust0to. Moved completion claiming onto `completing: AtomicBool` in `e55aa74` so `OP_CLEANUP` now runs without holding `call.done` or `CALLS`, and documented that `OP_CLEANUP` must not synchronously re-enter `XAsyncGetStatus(block, true)` on the same block.
Author
Owner

@⁠lvndrpup on GitHub · 2026-11-27 14:09 UTC · original

Re-tested at e55aa74 on the same RTX 3060 Ti PC (Windows 11, debug build).

e55aa74
status_waits_through_cleanup, 30 runs 30 passed
cargo test -p victory-hle --lib modules::xasync, 20 runs 13 passed each time
cargo test -p victory-hle --lib, one run 163 passed, 2 ignored
cargo clippy -p victory-hle --all-targets -- -D warnings, with and without --all-features clean

Taking done after OP_CLEANUP keeps the fix. The call stays in CALLS until the status is stored, so a waiter that finds it still waits on done, and one that doesn't find it reads the stored status. completing now does the first-result-wins check that done did.

One case is unchanged. If XAsyncCancel lands while another thread's completion is in OP_CLEANUP, it returns at once, and XAsyncGetStatus(_, false) right after it reads E_PENDING. I checked with a probe like the one above, completing with S_OK, 0 and cancelling during the cleanup: E_PENDING on the base and on e55aa74, 3 of 3 each. So it isn't a regression. Only a title that reads the status without waiting, right after a cancel that races a completion, would see it.

**@⁠lvndrpup** on GitHub · 2026-11-27 14:09 UTC · [original](https://github.com/Belobug/VIctory-Belobug-AMD/pull/579#issuecomment-7220414415) Re-tested at `e55aa74` on the same RTX 3060 Ti PC (Windows 11, debug build). | | `e55aa74` | |---|---| | `status_waits_through_cleanup`, 30 runs | 30 passed | | `cargo test -p victory-hle --lib modules::xasync`, 20 runs | 13 passed each time | | `cargo test -p victory-hle --lib`, one run | 163 passed, 2 ignored | | `cargo clippy -p victory-hle --all-targets -- -D warnings`, with and without `--all-features` | clean | Taking `done` after `OP_CLEANUP` keeps the fix. The call stays in `CALLS` until the status is stored, so a waiter that finds it still waits on `done`, and one that doesn't find it reads the stored status. `completing` now does the first-result-wins check that `done` did. One case is unchanged. If `XAsyncCancel` lands while another thread's completion is in `OP_CLEANUP`, it returns at once, and `XAsyncGetStatus(_, false)` right after it reads `E_PENDING`. I checked with a probe like the one above, completing with `S_OK, 0` and cancelling during the cleanup: `E_PENDING` on the base and on `e55aa74`, 3 of 3 each. So it isn't a regression. Only a title that reads the status without waiting, right after a cancel that races a completion, would see it.
Author
Owner

@⁠MamaLeVrai on GitHub · 2026-11-27 15:56 UTC · original

Tested on an RX 6950 XT (RDNA 2), driver 32.0.21045.29003, Windows 11 build 26300, Rust 1.98.1, release. At df83f3c: victory-hle 163 passed, 0 failed, 2 ignored.

AI-assisted (Claude Code); no game data.

**@⁠MamaLeVrai** on GitHub · 2026-11-27 15:56 UTC · [original](https://github.com/Belobug/VIctory-Belobug-AMD/pull/579#issuecomment-7222656357) Tested on an RX 6950 XT (RDNA 2), driver 32.0.21045.29003, Windows 11 build 26300, Rust 1.98.1, release. At `df83f3c`: `victory-hle` 163 passed, 0 failed, 2 ignored. AI-assisted (Claude Code); no game data.
This pull request can be merged automatically.
You are not authorized to merge this pull request.
View command line instructions

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u origin '+refs/pull/579/head:github-bot/gh-579'
git switch 'github-bot/gh-579'

Merge

Merge the changes and update on Forgejo.
git switch 'a-publier'
git merge --no-ff 'github-bot/gh-579'
git switch 'github-bot/gh-579'
git rebase 'a-publier'
git switch 'a-publier'
git merge --ff-only 'github-bot/gh-579'
git switch 'github-bot/gh-579'
git rebase 'a-publier'
git switch 'a-publier'
git merge --no-ff 'github-bot/gh-579'
git switch 'a-publier'
git merge --squash 'github-bot/gh-579'
git switch 'a-publier'
git merge --ff-only 'github-bot/gh-579'
git switch 'a-publier'
git merge 'github-bot/gh-579'
git push origin 'a-publier'
This discussion has been locked. Commenting is limited to contributors.
No description provided.