Skip to content

XAsync: a stale work item completes a reused block, a closed queue handle drops the completion callback, a wait returns before the callback #576

Backlog

Description

@gta5all

Static read of crates/xcl/src/hle/xgameruntime/xasync.rs on main (508a0c7), compared with libHttpClient Source/Task/AsyncLib.cpp (the contract the file's header names). It concerns XCL's own XAsync, not the PC runtime's (pc()).

Points 1–3 share one cause: the raw XAsyncBlock* is the task-queue callback context, and b.queue / b.callback are read again later. The reference passes a refcounted AsyncState as the context, and that state owns a queue handle duplicated at Begin.

  1. A stale work item runs, and can complete, a later call on the same block (xasync.rs:198-218, scheduled at :273)

    • do_work_cb gets only the block address and calls lookup(b), which returns whichever call is registered at that address now. The id check in lookup does not help, because the item carries no id.
    • Trigger (manual work port): XAsyncBegin(&b, P1) where P1 calls XAsyncSchedule; XAsyncComplete(&b, E_ABORT, 0); XAsyncBegin(&b, P2) where P2 never schedules; XTaskQueueDispatch(q, Work, 0).
    • Result: P2 receives OP_DO_WORK, and since it returns S_OK the "finished synchronously" path at :211-216 completes the second call. Expected: P2 gets no DoWork and stays E_PENDING. If the block was freed instead of reused, read_header(b) reads freed memory.
    • Reference: WorkerCallback returns early on if (!state->valid).
    • Suggested fix: pass the Arc<State> (Arc::into_raw) as the item's context, keep the block address in State, and return when that state is no longer Pending.
  2. Closing the queue handle during a call silently drops the completion callback (xasync.rs:322, also :269)

    • XAsyncComplete resolves (*b).queue through taskqueue::queue_from at completion time, and queue_from accepts only handles still in HANDLES. After XTaskQueueCloseHandle(q) it returns None and the if let Some(q) skips the callback without an error.
    • Trigger: XAsyncBegin(&b, ..) with a callback set; XTaskQueueCloseHandle(q); XAsyncComplete(&b, S_OK, 0). The callback never runs. Expected: it runs once on the completion port.
    • Reference: XTaskQueueDuplicateHandleWithOptions(queue, Reference, &state->queue) at Begin, so the title may close its own handle.
    • Suggested fix: store taskqueue::queue_from((*b).queue) in State at XAsyncBegin / XAsyncRun, and use it in XAsyncSchedule and XAsyncComplete.
  3. XAsyncGetStatus(b, true) returns before the completion callback has run (xasync.rs:338, :319-326, :220-226)

    • The waiter is released as soon as the status is published. The reference waiter waits for waitSatisfied, which is set only after the callback returns.
    • A title that sets a callback and also waits can free a stack block while completion_cb(b) is still queued. That item then reads (*b).callback from dead memory. The comment above the snapshot in XAsyncComplete covers XAsyncComplete itself, but not completion_cb.
    • Trigger: manual completion port, block with a callback; thread A calls XAsyncGetStatus(b, true); XAsyncComplete(b, S_OK, 4) with no dispatch. A returns at once. Expected: A blocks until the completion port is dispatched.
    • Suggested fix: give completion_cb the state, with the callback pointer snapshotted, and have the wait cover the callback. The paths with no callback, no queue or a refused submit must satisfy the wait themselves, otherwise terminate_manual_completion_port_with_delayed_async_does_not_deadlock hangs.
  4. XAsyncGetResultSize leaves *size unwritten while pending and after a failed call (xasync.rs:357-359, :366), low severity

    • The reference writes it in every case: *bufferSize = state == nullptr ? 0 : state->providerData.bufferSize;.
    • Suggested fix: write *size = 0 before returning E_PENDING, and drop && h.status >= 0 in the Finished arm.

Each trigger can be a deterministic unit test in the file's mod tests (manual ports, a counter). Open PR #197 rewrites parts of the same functions without changing these behaviours, so a fix would conflict with it textually.

Not tested: nothing was built or run (read on Linux; no game files). The triggers are traced through the code by hand. Not checked: whether a retail title does any of this.

AI use: found and written with Claude Code (Claude Fable 5.2), static code review only, under human review.

Activity

  1. WebIdentity1 commented on Nov 24, 2026

    @WebIdentity1

    Draft #641, head 8d0bbc8, addresses item 2 only by retaining the operation's queue. Native controls: 0/3 before, 3/0 after; XGameRuntime 76/0/0. Full four-thread workspace on base 18c105a: 691 passed / 1 known CLAMP failure / 41 ignored, four explicit skips. No paired retail smoke; remains draft.

    Item 1 follow-up: reproduced and patched on a dependency integration
    Top commit 06198fe contains only the stale-work fix, three tests and README change (105 additions / 4 deletions). Its parent 86bbd00 integrates main dd2ae9a with Michael-Glaser's #315 (04b8042) and #641 (8d0bbc8). This is a fork-only validation branch, not a competing implementation or a merge-ready PR. Separate #315 validation.

    Queued work now owns its original Arc<State> and discards non-pending operations before reading their block; refused submission releases that reference. #315 is needed for accepted timer callbacks to be delivered during termination, and #641 retains the queue. The tests cover normal/canceled stale dispatch after block reuse, dispatch after a completed block's page becomes inaccessible, and immediate/delayed submission refusal.

    Tested
    Windows 10 build 19045, Ryzen 7 7800X3D, RTX 3090 driver 591.74 and AMD integrated graphics driver 32.0.21030.2001, Rust 1.98.1 MSVC, SDK DXC 1.8.2502.11, Agility 1.620.2, debug.

    • Negative controls with the original work callback and initial fixtures: cargo test --locked -p xcl --lib stale_queued_work_does_not_run_reused_block -- --test-threads=1 --show-output: 0 passed / 1 failed / 0 ignored, replacement provider wrongly invoked. The corresponding stale_queued_work_does_not_read_completed_block filter exited with access violation 0xc0000005 on an owned synthetic PAGE_NOACCESS page; no timeout. This is a crash reproduction, not a passing test.
    • Candidate cargo test --locked -p xcl --lib queued_work -- --test-threads=1 --show-output: 3/0/0. hle::xgameruntime:: filter: 81/0/0, no skips. These focused runs preceded adding the canceled-dispatch case to the reuse fixture.
    • Final source, SHA256-verified against 06198fe: cargo build --locked --workspace succeeds; cargo test --locked --workspace --no-fail-fast -- --test-threads=4 --show-output: 697 passed / 1 failed / 41 ignored, no timeout. All three new tests pass, including canceled reuse. Only gpu_clamp_and_omod_per_instruction fails, as previously reproduced on main. Presentation tests ran in desktop session 1; XCL_ADAPTER=3090, with some fixtures selecting AMD directly.
    • Four explicit skips: gpu_geometry_wave32_prefix_count_over_a_mask, gpu_wave32_program_in_wave64_hardware (no native wave64 on NVIDIA), test_title_draws_with_a_root_signature_under_the_debug_layer, uploads_start_from_each_subresources_state (debug layer unavailable). Sample variables unset; no installed system XGameRuntime.

    Not tested
    Retail smoke/gameplay, release builds, Wine, installed PC runtime, or concurrent completion/disposal after work has already started. This does not fix item 3's completion callback/wait ordering or item 4. #197 overlaps the same functions and still needs reconciliation; not tested together. #235 fallback selection is separate.

    AI use: Codex (GPT-6), public-source inspection, focused implementation, synthetic reproduction, native validation and this report. No proprietary inputs.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions