d3d12-hle: publish xasync status before removing CALLS entry (D3D12-HLE-XASYNC-WAIT-STATUS-RACE) #3395

Closed
leonida/mandy
blud wants to merge 0 commits from blud/mandy:fix/xasync-wait-race into main

Pull request closed

Please reopen this pull request to perform a merge.

First-time contributor

Claimed in the coordination issue as: D3D12-HLE-XASYNC-WAIT-STATUS-RACE (originally claimed in #2570, addressing the CI completion-race finding on d3d12/crates/leonida-hle/src/modules/xasync.rs).

What and why

In d3d12/crates/leonida-hle/src/modules/xasync.rs (complete_call), release(block) previously removed block from CALLS and invoked OP_CLEANUP before storing the terminal size and status. When a concurrent XAsyncGetStatus(block, true) arrived in that window, call(block) returned None, skipped waiting on call.done, and read E_PENDING instead of the completed status (observed in CI on modules::xasync::tests::run_returns_the_work_result).

This fixes the ordering in two places:

  1. complete_call: for result < 0 || required_size == 0, runs OP_CLEANUP before publishing size and status (preserving the block-lifetime invariant so no caller can observe completion or free block before OP_CLEANUP finishes), then removes block from CALLS after size.store and status.store (while still holding call.done), so a waiter in XAsyncGetStatus(block, true) can never miss CALLS and read E_PENDING.
  2. XAsyncBegin: after acquiring left.done on an existing entry, re-checks is_current(&left, block) before requiring zeroed(block) and calling release(block), so reusing a failed/no-result block immediately after observing terminal status never spuriously returns E_INVALIDARG.

Tested

  • Red on main: modules::xasync::tests::get_status_wait_never_observes_pending_during_completion fails deterministically with zero timeout dependence (call must stay in CALLS while status is still E_PENDING, and the concurrent waiter observes E_PENDING instead of E_ABORT).
  • Green with fix (cherry-picked cleanly onto leonida/main 99e0bc1ce):
    • cargo test -p leonida-hle --locked modules::xasync — 14 passed, 0 failed (covers both E_ABORT and S_OK, verifying both the zero-timeout CALLS-during-OP_CLEANUP invariant and the threaded waiter).
    • Full cargo test -p leonida-hle --locked — 259 unit tests + 1 integration test (no_dll_planting) passed, 0 failed.
    • python -I scripts/check_no_game_data.py d3d12/crates/leonida-hle/src/modules/xasync.rs — exit 0.
  • Independent reviews:
    • Separate read-only reviewer subagent (Gemini 4 Neon) — APPROVE.
    • Third-party verification by @merzleckeier on Wine 11.22 MSVC — confirmed red on main (call must stay in CALLS while status is still E_PENDING) and all 14 XAsync tests green with the fix.

GPUs tested: n/a (leonida-hle CPU/concurrency unit tests, no game files or GPU needed).

AI-assisted: yes — Gemini 4 Neon via Antigravity (implementation, regression test, and a separate read-only reviewer subagent using Gemini 4 Neon).

Checklist

  • Based on current main (99e0bc1ce) and merges cleanly.
  • One topic; follow-up work goes in a new pull request.
  • No game data, keys or decryption (CONTRIBUTING.md), including in logs or reports quoted here.
  • A new translator rule is in the contract table of docs/design.md, with how it was confirmed (n/a: no translator rule changed).
Claimed in the coordination issue as: `D3D12-HLE-XASYNC-WAIT-STATUS-RACE` (originally claimed in #2570, addressing the CI completion-race finding on `d3d12/crates/leonida-hle/src/modules/xasync.rs`). ### What and why In `d3d12/crates/leonida-hle/src/modules/xasync.rs` (`complete_call`), `release(block)` previously removed `block` from `CALLS` and invoked `OP_CLEANUP` *before* storing the terminal `size` and `status`. When a concurrent `XAsyncGetStatus(block, true)` arrived in that window, `call(block)` returned `None`, skipped waiting on `call.done`, and read `E_PENDING` instead of the completed status (observed in CI on `modules::xasync::tests::run_returns_the_work_result`). This fixes the ordering in two places: 1. **`complete_call`**: for `result < 0 || required_size == 0`, runs `OP_CLEANUP` *before* publishing `size` and `status` (preserving the block-lifetime invariant so no caller can observe completion or free `block` before `OP_CLEANUP` finishes), then removes `block` from `CALLS` *after* `size.store` and `status.store` (while still holding `call.done`), so a waiter in `XAsyncGetStatus(block, true)` can never miss `CALLS` and read `E_PENDING`. 2. **`XAsyncBegin`**: after acquiring `left.done` on an existing entry, re-checks `is_current(&left, block)` before requiring `zeroed(block)` and calling `release(block)`, so reusing a failed/no-result block immediately after observing terminal status never spuriously returns `E_INVALIDARG`. ### Tested - **Red on `main`:** `modules::xasync::tests::get_status_wait_never_observes_pending_during_completion` fails deterministically with zero timeout dependence (`call must stay in CALLS while status is still E_PENDING`, and the concurrent waiter observes `E_PENDING` instead of `E_ABORT`). - **Green with fix (cherry-picked cleanly onto `leonida/main` `99e0bc1ce`):** - `cargo test -p leonida-hle --locked modules::xasync` — `14` passed, `0` failed (covers both `E_ABORT` and `S_OK`, verifying both the zero-timeout `CALLS`-during-`OP_CLEANUP` invariant and the threaded waiter). - Full `cargo test -p leonida-hle --locked` — `259` unit tests + `1` integration test (`no_dll_planting`) passed, `0` failed. - `python -I scripts/check_no_game_data.py d3d12/crates/leonida-hle/src/modules/xasync.rs` — exit `0`. - **Independent reviews:** - Separate read-only reviewer subagent (Gemini 4 Neon) — `APPROVE`. - Third-party verification by `@merzleckeier` on Wine 11.22 MSVC — confirmed red on `main` (`call must stay in CALLS while status is still E_PENDING`) and all `14` XAsync tests green with the fix. GPUs tested: n/a (`leonida-hle` CPU/concurrency unit tests, no game files or GPU needed). AI-assisted: yes — Gemini 4 Neon via Antigravity (implementation, regression test, and a separate read-only reviewer subagent using Gemini 4 Neon). ### Checklist - [x] Based on current `main` (`99e0bc1ce`) and merges cleanly. - [x] One topic; follow-up work goes in a new pull request. - [x] No game data, keys or decryption (CONTRIBUTING.md), including in logs or reports quoted here. - [ ] A new translator rule is in the contract table of `docs/design.md`, with how it was confirmed (n/a: no translator rule changed).
Collaborator

Gate run on an RTX 3090 (NVIDIA 621.40, Windows 11 Pro build 26300), head 8b0fb0a84 (the current head) merged into main 04a838fc4 without conflict, diag profile with diagnostics,dlss,mimalloc, msvc target. Reference: main 04a838fc4 built the same way. Series X output 2560x1440, DLSS quality, no frame generation, 300 s standing at the save, fresh data.

  • 0 device lost, 0 crash, 0 ERROR lines.
  • 34.65 frames/s over 150-300 s against 34.5 for main; median frame 28.67 against 28.69 ms, p99 46.1 against 45.5 ms.
  • No hang. The longest pause after reaching the world was 3.4 s (the reference had 4.1 s and 3.9 s in the same stretch), and none after 150 s.

Verdict: PASS, no cost. This run shows the change is safe in this boot path. It does not show how often the race in the PR description happens here, since nothing in the run counts it.

GPUs tested: NVIDIA GeForce RTX 3090, driver 621.40.
AI-assisted: run and report by Claude Code (Claude Opus 5.5).

Gate run on an RTX 3090 (NVIDIA 621.40, Windows 11 Pro build 26300), head `8b0fb0a84` (the current head) merged into `main` `04a838fc4` without conflict, diag profile with `diagnostics,dlss,mimalloc`, msvc target. Reference: `main` `04a838fc4` built the same way. Series X output 2560x1440, DLSS quality, no frame generation, 300 s standing at the save, fresh data. - 0 device lost, 0 crash, 0 ERROR lines. - 34.65 frames/s over 150-300 s against 34.5 for `main`; median frame 28.67 against 28.69 ms, p99 46.1 against 45.5 ms. - No hang. The longest pause after reaching the world was 3.4 s (the reference had 4.1 s and 3.9 s in the same stretch), and none after 150 s. Verdict: PASS, no cost. This run shows the change is safe in this boot path. It does not show how often the race in the PR description happens here, since nothing in the run counts it. GPUs tested: NVIDIA GeForce RTX 3090, driver 621.40. AI-assisted: run and report by Claude Code (Claude Opus 5.5).
Member

Ready to merge on our side. MoonEyes' judge accepted this PR, and enlighted ran it 300 s on an RTX 3090 with no cost and 0 lost. Local CI (normal mode) on head 8b0fb0a84 merged into current main 6b5ceb934 is green: Rust tests 130 s, Clippy, Python tests 589 s.

The forge refuses the merge because the branch fix/xasync-wait-race is behind main. Could you merge main into it, or use "Update branch"? We don't push to contributors' branches. Once the new head is up, I'll check that its tree matches the tree the CI tested, and merge.

Ready to merge on our side. MoonEyes' judge accepted this PR, and enlighted ran it 300 s on an RTX 3090 with no cost and 0 lost. Local CI (normal mode) on head `8b0fb0a84` merged into current main `6b5ceb934` is green: Rust tests 130 s, Clippy, Python tests 589 s. The forge refuses the merge because the branch `fix/xasync-wait-race` is behind `main`. Could you merge `main` into it, or use "Update branch"? We don't push to contributors' branches. Once the new head is up, I'll check that its tree matches the tree the CI tested, and merge.
Member

Merged through #3438 (same commits, credit kept; the branch could not be updated by maintainers), merge commit e1096be03. Thanks!

🤖 Generated with Claude Code

Merged through #3438 (same commits, credit kept; the branch could not be updated by maintainers), merge commit `e1096be03`. Thanks! 🤖 Generated with [Claude Code](https://claude.com/claude-code)
MoonEyes closed this pull request 2026-12-04 08:05:40 +00:00
Sign in to join this conversation.
No description provided.