Skip to content

d3d12-hle: terminating a composite task queue cancels only its own tasks (D3D12-HLE-XTASKQUEUE-COMPOSITE-TERMINATE) - #2824

Merged
OreoFR merged 1 commit into
ProjectLeonida:mainfrom
Kal0s-design:fix/xasync-composite-terminate
Dec 1, 2026
Merged

OreoFR merged 1 commit into
ProjectLeonida:mainfrom
Kal0s-design:fix/xasync-composite-terminate

Conversation

@Kal0s-design

Copy link
Copy Markdown
Contributor

Claimed in the coordination issue as: D3D12-HLE-XTASKQUEUE-COMPOSITE-TERMINATE (#2570 (comment))

What and why

A composite task queue (XTaskQueueCreateComposite) shares its ports with the queue they came from. XTaskQueueTerminate (d3d12/crates/leonida-hle/src/modules/xasync.rs) drained every task on each of the terminated queue's ports. Terminating the composite therefore also took the other queue's pending tasks and ran them as canceled, while that queue was still open. The comment next to the drain already said the other queues' dispatches wait on; the code didn't do it for their tasks.

The change:

  • Each queued task carries the id of the queue that queued it (Queue::id, from a counter, never reused).
  • Termination drains only the terminated queue's own tasks; the others stay queued and run uncanceled on the next dispatch.

This ports ahmetbagci's fix from the sibling VIctory tree (TaynanGT/Vice-Tay fa10fa3, GPL-3.0), credited as co-author.

Tested

  • New test modules::xasync::tests::terminating_a_composite_queue_leaves_other_queues_tasks_queued: queue A and a composite B on A's ports each queue a task; terminating B cancels B's task and leaves A's queued, and A's then runs uncanceled. On main (90c514437) it fails (the other queue's task stays queued, left: 2, right: 0); with the change it passes.
  • cargo test -p leonida-hle --lib: 241 passed, 0 failed, 2 ignored.
  • cargo clippy -p leonida-hle --lib --tests: no warning in the changed lines.
  • Windows only, unit tests; the title wasn't run.

GPUs tested: n/a (CPU unit tests)

AI-assisted: yes

Checklist

  • Based on current main and merges cleanly (a conflicting pull request is labeled needs rebase).
  • 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 change)

🤖 Generated with Claude Code

A composite queue (XTaskQueueCreateComposite) shares its ports with the
queue they came from. XTaskQueueTerminate drained every task on each of
the queue's ports, so terminating the composite also took the other
queue's pending tasks and ran them as canceled, while that queue was
still open. Each queued task now carries the id of the queue that queued
it, and termination drains only those of the terminated queue; the
others stay queued and run uncanceled on the next dispatch.

Ported from ahmetbagci's fix in Vice-Tay (fa10fa3).

Co-Authored-By: ahmetbagci <6625856+ahmetbagci@users.noreply.github.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@fiestorra

Copy link
Copy Markdown
Contributor

Verified on Windows with a mutation at the line the change exists for, and it fails in both directions - cancel your own, spare the others.

main 90c514437, cherry-picked (the branch is at main), cargo 1.98.1:

cargo test -p leonida-hle
  with this pull request:     241 passed; 0 failed; 2 ignored
  with main's xasync.rs:      240 passed; 0 failed; 2 ignored    (the test arrives with it)

The test arriving with the code is not the interesting part, so I made the change it names in xasync.rs:415 and watched both halves:

tasks.drain(..).partition(|(owner, _)| *owner == queue.id);   // as this branch has it

with `true`   (cancel every task):   FAILED  terminating_a_composite_queue_leaves_other_queues_tasks_queued
                                     assertion `left == right` failed: the other queue's task stays queued   left: 2
with `false`  (cancel none):         FAILED  terminate_cancels_what_is_queued
                                     FAILED  terminating_a_composite_queue_leaves_other_queues_tasks_queued

So the tests pin the property from both sides: true breaks only the "spare the others" case, false breaks that one and the "cancel your own" case. That is the shape a fix like this needs - a one-sided test would pass with false in place and the queue would never cancel anything.

What the change is, from the code: each queued task carries the id of the queue that queued it (submit(owner, task)), and terminating partitions the queue by that id instead of draining it. A static NEXT_QUEUE: AtomicU64 hands out the ids; the comment says never reused.

What I did not test: the real XTaskQueueTerminate path against a title's composite queue - that needs the game. The mock's record callback and the two states it stores are what the tests drive, and they are green.

AI-assisted: yes - DeepSeek v4.1 (flash), two mutations and three runs.

Written with an AI assistant.

@KelboLive KelboLive left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Verified, and the direction is pinned both ways. The new test fails on the base 90c514437 (left: 2, right: 0, "the other queue's task stays queued") and passes on the head; cargo test --locked -p leonida-hle --lib is 241 passed / 0 failed / 2 ignored on the head, and main plus the new test is 240/1/2 with only that test failing. Mutation check: draining with partition(|_| true) reproduces the old bug and is caught at xasync.rs:1703; partition(|_| false) is caught by the new test (:1702) and by terminate_cancels_what_is_queued, so the ownership predicate is pinned both ways, not just one.

The mechanism reads right: the queue id is per queue and shared by duplicated handles (:197-218), every enqueue goes through Queue::deliver with self.id (:254-255, :121-130), a completion already queued is drained with canceled=true and one arriving after termination runs canceled through submit's E_ABORT (:704-709), the drain is under the port lock and scoped to that id (:408-426), and the sibling's dispatcher still pops its own tasks (:375). terminate_ends_a_waiting_dispatch still passes, so a wait across queues is not short-circuited.

One follow-up, pre-existing and not introduced here: submit checks terminated outside the port lock, and the delayed timer delivers after its own check, so a task can still land on a port after a composite's drain — the terminated queue's dispatch will never run it and the sibling's will run it uncanceled. Re-checking terminated under the port lock inside deliver would close that. Also note _wait is still ignored, so wait=true returns before in-flight work finishes.

@OreoFR

OreoFR commented Dec 1, 2026

Copy link
Copy Markdown
Collaborator

Merging as part of a group. Head 156827c; the group branch main 1e3e316 + #2862 + #2824 + #2873 (7988c1c, merged in that order) passed the full CI (https://github.com/OreoFR/leonida/actions/runs/46740411028, the same workflow file as here, run on that branch) and No Game Data (https://github.com/OreoFR/leonida/actions/runs/46740416586); main was 1e3e316 with its CI green when the group merged. Claim: D3D12-HLE-XTASKQUEUE-COMPOSITE-TERMINATE (#2570 (comment)). Red/green: fiestorra (Windows, mutation fails both ways), KelboLive review on this head (test fails on base, passes here). I ran: the five PRs #2860-#2862, #2824 and #2873 merged together onto main ab374c7 (main since gained only #2935, a CI workflow change, and #2860 and #2861, both part of that test), with the shader (418 passed, 0 failed), gpu (644/0) and hle (247/0) cargo tests. In-game run: RTX 4070 Laptop 8 GB, NVIDIA 621.40, a cold start to the world then a 330 s warm walking run, 0 device lost, no panic. CI: green on head, main and the group branch. AI-assisted: Claude Code (oreofr-agent), merge approved in writing by OreoFR.

@imShangLi

Copy link
Copy Markdown
Contributor

VERIFY-2824 — Linux, Fedora 45, Rust 1.98.1 (the pinned toolchain), Wine 11.21, no GPU device, no game files

Verdict: PASS. The repository's own red/green idea holds under Wine: the new test fails on the merge base and passes with the pull request, and nothing that worked before is broken.

Head 156827c1, base the current main, merge base 90c51443, 1 file, +54/−7: d3d12/crates/leonida-hle/src/modules/xasync.rs.

1. What it changes

A composite task queue shares its ports with the queue it came from. XTaskQueueTerminate drained the port's whole task queue:

let pending: Vec<Task> = lock(&port.tasks).drain(..).collect();

so terminating a composite queue also cancelled the parent queue's tasks that were still queued on the shared port. The pull request gives every queue an id that is never reused, tags each queued task with the queue that queued it, and drains only that queue's own tasks:

// Only this queue's tasks: a composite queue shares the port with
// the queue it came from, whose tasks stay queued.
let pending: Vec<Task> = {
    let mut tasks = lock(&port.tasks);
    let (mine, others): (VecDeque<_>, VecDeque<_>) =
        tasks.drain(..).partition(|(owner, _)| *owner == queue.id);
    *tasks = others;
    mine.into_iter().map(|(_, task)| task).collect()
};

2. Red and green, run under Wine

leonida-hle does not build on Linux (upstream: "The crates are Windows-only today … Linux follows #2577"), so I cross-compiled it for x86_64-pc-windows-gnu and ran the tests under Wine 11.21:

the merge base with only this pull request's tests 244 tests
the pull request's head 244 tests
red → green modules::xasync::tests::terminating_a_composite_queue_leaves_other_queues_tasks_queued — fails on the base, passes with the pull request
regressions 0
failing on both sides 2 — modules::net::tests::only_loopback_is_reachable ("ws2_32.dll lacks WSAConnectByList") and modules::xgamesave::tests::a_kept_blob_past_the_link_limit_is_copied ("the temp folder must be on NTFS"). Both are the environment, not this change

The new test is the right shape for the change: it queues one task on the parent queue and one on the composite, terminates the composite, and checks that the composite's own task was canceled while the parent's stays queued and later runs uncanceled — which is what the old drain(..) made impossible.

3. The ids are never reused

static NEXT_QUEUE: AtomicU64 = AtomicU64::new(1);
...
id: NEXT_QUEUE.fetch_add(1, Ordering::Relaxed),

A queue's id is taken once, when the queue is made, from a counter that only goes up — so a task tagged with a dead queue's id can never be mistaken for a later queue's task, which is what makes the partition by owner safe. Relaxed is enough for it: the id is only ever compared with another id taken from the same counter.

4. What I did not check

  • The title's own use of composite queues. That terminating a composite queue cancelled the parent's tasks was a bug the title hit is a claim about the game and about how the title uses the API; I checked the mechanism, not the title.
  • The console's task queue semantics. The Xbox side of XTaskQueue* is documented elsewhere in the runtime; I did not compare the composite-queue behaviour with the console's.
  • Windows and MSVC. Everything above is Linux with Wine 11.21 (staging) and no GPU device.
  • The other two failing tests are Wine's missing WSAConnectByList and its filesystem, not this change.

AI-assisted: MiniMax M3.1 Flash.

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants