HLE: XTaskQueueTerminate cancels other queues' queued tasks on shared ports (fix on a fork) #1637

Open
opened 2026-11-26 04:39:15 +00:00 by github-bot · 3 comments
Owner

@⁠AndyLatimer on GitHub · 2026-11-24 20:15 UTC · original

Summary

XTaskQueueTerminate in crates/victory-hle/src/modules/xasync.rs drains port.tasks unconditionally for both ports of the terminated queue:

for port in &queue.ports {
    let pending: Vec<Task> = lock(&port.tasks).drain(..).collect();
    ...
    for task in pending {
        task.run(true);
    }
}

When multiple queues share a Port via XTaskQueueGetPort + XTaskQueueCreateComposite (manual or serialized ports, including composite queues that map both WORK and COMPLETION to the same shared port), terminating one queue steals and cancels every queued callback and pending XAsync work item submitted through other still-open queues on that port.

This also leaves a race window between Queue::submit checking self.terminated and Port::submit locking port.tasks, where a task from a queue being terminated concurrently could still be pushed onto a shared port after XTaskQueueTerminate already drained it.

Per Microsoft's GDK XTaskQueue semantics (as implemented in microsoft/libHttpClient's TaskQueuePortImpl::CancelPendingEntries / IsCallCanceled), queued entries belong to the submitting queue's port context: terminating one queue cancels only its own pending entries and leaves other queues' entries untouched on the shared port.

Fix on a fork

What the commit changes in crates/victory-hle/src/modules/xasync.rs:

  • Assigns each Queue a monotonic id: u64 (NEXT_QUEUE), shared across duplicated handles (XTaskQueueDuplicateHandle) and distinct for composite queues (XTaskQueueCreateComposite).
  • Tags queued entries in Port::tasks with (owner_id, Task).
  • Adds Port::take_owned(owner) to remove and return only tasks submitted through the terminating queue in FIFO order, preserving other queues' queued tasks in FIFO order.
  • Re-checks queue.terminated under port.tasks inside Port::submit so a task racing with XTaskQueueTerminate is canceled instead of being enqueued after the drain.
  • Adds three synthetic unit tests covering shared manual ports (including duplicate handles and a composite queue with both ports mapped to the same shared port), shared serialized ports, and pending XAsyncRun calls across composite queues.

Verification

Before fix (ffe310e + new tests)

running 5 tests
test modules::xasync::tests::terminate_cancels_what_is_queued ... ok
test modules::xasync::tests::terminate_composite_queue_preserves_other_queues_async_calls ... FAILED
test modules::xasync::tests::terminate_on_shared_manual_port_only_cancels_its_own_tasks ... FAILED
test modules::xasync::tests::terminate_on_shared_serialized_port_keeps_other_queues_tasks ... FAILED
test modules::xasync::tests::terminate_ends_a_waiting_dispatch ... ok
  • Manual shared port: terminating b canceled all 8 queued tasks across a, a_dup, b, and c ([(1, true), (2, true), (3, true), (4, true), (5, true), (6, true), (10, true), (11, true)] instead of [(2, true), (5, true), (11, true)]).
  • Serialized shared port: terminating b canceled a's queued callbacks ([(1, true), (2, true), (3, true)] instead of [(2, true)]).
  • XAsyncRun on shared port: terminating b aborted a's pending async call (E_ABORT instead of E_PENDING).

After fix (47e61a2)

  • cargo test -p victory-hle: 97 passed; 0 failed (lib) + 1 passed; 0 failed (no_dll_planting)
  • cargo clippy -p victory-hle --all-targets -- -D warnings: pass (0 warnings)
  • cargo check -p victory --features diagnostics: pass
  • rustfmt --edition 2024 --check crates/victory-hle/src/modules/xasync.rs: 0 diffs on touched lines (only pre-existing untouched lines from origin/main)
  • cargo xtask policy: asset policy: 154 files ok

Disclosures

  • Synthetic unit tests only; no game data, binaries, or console SDK files were used.
  • Diagnosed, implemented, and verified with AI assistance (Google Antigravity).
**@⁠AndyLatimer** on GitHub · 2026-11-24 20:15 UTC · [original](https://github.com/Marvisak/VIctory/issues/1637) ## Summary `XTaskQueueTerminate` in `crates/victory-hle/src/modules/xasync.rs` drains `port.tasks` unconditionally for both ports of the terminated queue: ```rust for port in &queue.ports { let pending: Vec<Task> = lock(&port.tasks).drain(..).collect(); ... for task in pending { task.run(true); } } ``` When multiple queues share a `Port` via `XTaskQueueGetPort` + `XTaskQueueCreateComposite` (manual or serialized ports, including composite queues that map both `WORK` and `COMPLETION` to the same shared port), terminating one queue steals and cancels every queued callback and pending `XAsync` work item submitted through other still-open queues on that port. This also leaves a race window between `Queue::submit` checking `self.terminated` and `Port::submit` locking `port.tasks`, where a task from a queue being terminated concurrently could still be pushed onto a shared port after `XTaskQueueTerminate` already drained it. Per Microsoft's GDK `XTaskQueue` semantics (as implemented in `microsoft/libHttpClient`'s `TaskQueuePortImpl::CancelPendingEntries` / `IsCallCanceled`), queued entries belong to the submitting queue's port context: terminating one queue cancels only its own pending entries and leaves other queues' entries untouched on the shared port. ## Fix on a fork - Branch: [`AndyLatimer/VIctory@fix/hle-xtaskqueue-terminate-shared-ports`](https://github.com/AndyLatimer/VIctory/tree/fix/hle-xtaskqueue-terminate-shared-ports) (based on `main` @ `ffe310e`) - Commit: [`47e61a2`](https://github.com/AndyLatimer/VIctory/commit/47e61a2476de7791055797c81208d8c2d1615371) What the commit changes in `crates/victory-hle/src/modules/xasync.rs`: - Assigns each `Queue` a monotonic `id: u64` (`NEXT_QUEUE`), shared across duplicated handles (`XTaskQueueDuplicateHandle`) and distinct for composite queues (`XTaskQueueCreateComposite`). - Tags queued entries in `Port::tasks` with `(owner_id, Task)`. - Adds `Port::take_owned(owner)` to remove and return only tasks submitted through the terminating queue in FIFO order, preserving other queues' queued tasks in FIFO order. - Re-checks `queue.terminated` under `port.tasks` inside `Port::submit` so a task racing with `XTaskQueueTerminate` is canceled instead of being enqueued after the drain. - Adds three synthetic unit tests covering shared manual ports (including duplicate handles and a composite queue with both ports mapped to the same shared port), shared serialized ports, and pending `XAsyncRun` calls across composite queues. ## Verification ### Before fix (`ffe310e` + new tests) ``` running 5 tests test modules::xasync::tests::terminate_cancels_what_is_queued ... ok test modules::xasync::tests::terminate_composite_queue_preserves_other_queues_async_calls ... FAILED test modules::xasync::tests::terminate_on_shared_manual_port_only_cancels_its_own_tasks ... FAILED test modules::xasync::tests::terminate_on_shared_serialized_port_keeps_other_queues_tasks ... FAILED test modules::xasync::tests::terminate_ends_a_waiting_dispatch ... ok ``` - Manual shared port: terminating `b` canceled all 8 queued tasks across `a`, `a_dup`, `b`, and `c` (`[(1, true), (2, true), (3, true), (4, true), (5, true), (6, true), (10, true), (11, true)]` instead of `[(2, true), (5, true), (11, true)]`). - Serialized shared port: terminating `b` canceled `a`'s queued callbacks (`[(1, true), (2, true), (3, true)]` instead of `[(2, true)]`). - `XAsyncRun` on shared port: terminating `b` aborted `a`'s pending async call (`E_ABORT` instead of `E_PENDING`). ### After fix (`47e61a2`) - `cargo test -p victory-hle`: `97 passed; 0 failed` (`lib`) + `1 passed; 0 failed` (`no_dll_planting`) - `cargo clippy -p victory-hle --all-targets -- -D warnings`: pass (0 warnings) - `cargo check -p victory --features diagnostics`: pass - `rustfmt --edition 2024 --check crates/victory-hle/src/modules/xasync.rs`: 0 diffs on touched lines (only pre-existing untouched lines from `origin/main`) - `cargo xtask policy`: `asset policy: 154 files ok` ## Disclosures - Synthetic unit tests only; no game data, binaries, or console SDK files were used. - Diagnosed, implemented, and verified with AI assistance (Google Antigravity).
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 04:39:15 +00:00
Author
Owner

@⁠CaravelaLabs on GitHub · 2026-11-25 00:10 UTC · original

@⁠AndyLatimer this is still on main 70564ae. Terminate has not changed since ffe310e.

Two manual queues, the second a composite on the first's ports. A callback is already queued on the first. Terminating the second runs that callback as canceled (stored 2, wanted 0 still queued). Nothing is left to dispatch. Wine 11.22. This is the queue code, not the GPU.

The test already on main waits until a callback has run, then terminates the other queue, then submits a new one. It never leaves a callback queued across that terminate, so it still passes: github.com/Marvisak/VIctory@70564ae08c/crates/victory-hle/src/modules/xasync.rs (L1450-L1459)

Terminate still drains every task on both ports: github.com/Marvisak/VIctory@70564ae08c/crates/victory-hle/src/modules/xasync.rs (L384-L392)

@⁠Calvix2k's #1673 ports your fork commit and is still open. I did not re-run that head. @⁠avt0m4t48 already ran dda166c8 and reported a separate serialized-completion case there. I have not checked that one.

I did not run a title. AI-assisted; tests run on this machine.

**@⁠CaravelaLabs** on GitHub · 2026-11-25 00:10 UTC · [original](https://github.com/Marvisak/VIctory/issues/1637#issuecomment-7148224502) @⁠AndyLatimer this is still on main `70564ae`. Terminate has not changed since `ffe310e`. Two manual queues, the second a composite on the first's ports. A callback is already queued on the first. Terminating the second runs that callback as canceled (stored 2, wanted 0 still queued). Nothing is left to dispatch. Wine 11.22. This is the queue code, not the GPU. The test already on main waits until a callback has run, then terminates the other queue, then submits a new one. It never leaves a callback queued across that terminate, so it still passes: https://github.com/Marvisak/VIctory/blob/70564ae08c82d43df5fad97b08d34628b07b1d9d/crates/victory-hle/src/modules/xasync.rs#L1450-L1459 Terminate still drains every task on both ports: https://github.com/Marvisak/VIctory/blob/70564ae08c82d43df5fad97b08d34628b07b1d9d/crates/victory-hle/src/modules/xasync.rs#L384-L392 @⁠Calvix2k's #1673 ports your fork commit and is still open. I did not re-run that head. @⁠avt0m4t48 already ran `dda166c8` and reported a separate serialized-completion case there. I have not checked that one. I did not run a title. AI-assisted; tests run on this machine.
Author
Owner

@⁠Calvix2k on GitHub · 2026-11-25 00:41 UTC · original

Rebased #1673 onto current main (4118f33) and pushed. Conflict was only the doc comment plus a test added on main in the same place; the resolution keeps both tests:

  • erminate_cancels_delayed_callbacks (from main), and
  • erminate_on_shared_manual_port_only_cancels_its_own_tasks (this fix: a callback already queued on queue A still runs when composite queue B, sharing A's ports, is terminated).

Validation on Windows 10 x64 (Ryzen 7 5800X, RX 7800 XT): cargo test -p victory-hle --locked xasync → 15 passed; cargo clippy -p victory-hle --all-targets --all-features --locked -- -D warnings clean; cargo xtask policy 182 files ok. No title run needed for the port semantics. PR is mergeable again.

**@⁠Calvix2k** on GitHub · 2026-11-25 00:41 UTC · [original](https://github.com/Marvisak/VIctory/issues/1637#issuecomment-7148769577) Rebased #1673 onto current main (4118f33) and pushed. Conflict was only the doc comment plus a test added on main in the same place; the resolution keeps **both** tests: - erminate_cancels_delayed_callbacks (from main), and - erminate_on_shared_manual_port_only_cancels_its_own_tasks (this fix: a callback already queued on queue A still runs when composite queue B, sharing A's ports, is terminated). Validation on Windows 10 x64 (Ryzen 7 5800X, RX 7800 XT): cargo test -p victory-hle --locked xasync → 15 passed; cargo clippy -p victory-hle --all-targets --all-features --locked -- -D warnings clean; cargo xtask policy 182 files ok. No title run needed for the port semantics. PR is mergeable again.
Author
Owner

@⁠Marvisak on GitHub · 2026-11-25 12:51 UTC · original

Automated triage for the maintainers.

Confirmed on main (940d32f): XTaskQueueTerminate in crates/victory-hle/src/modules/xasync.rs drains every task from each of the queue's ports (lock(&port.tasks).drain(..)) and runs them as canceled, without checking which queue submitted them, so a queue sharing a port (via XTaskQueueGetPort + XTaskQueueCreateComposite) loses its queued callbacks when another queue on that port is terminated.

Open PR #1673 carries the fork commit, rebased onto main, and keeps the existing delayed-callback test; a separate serialized-completion case was reported in that PR's review.


Generated by Claude Code

**@⁠Marvisak** on GitHub · 2026-11-25 12:51 UTC · [original](https://github.com/Marvisak/VIctory/issues/1637#issuecomment-7162962361) <!-- victory-issue-triage v1 verdict=confirmed seen=2026-11-25T00:41:08Z at=2026-11-25T12:51:38Z --> Automated triage for the maintainers. Confirmed on `main` (`940d32f`): `XTaskQueueTerminate` in `crates/victory-hle/src/modules/xasync.rs` drains every task from each of the queue's ports (`lock(&port.tasks).drain(..)`) and runs them as canceled, without checking which queue submitted them, so a queue sharing a port (via `XTaskQueueGetPort` + `XTaskQueueCreateComposite`) loses its queued callbacks when another queue on that port is terminated. Open PR #1673 carries the fork commit, rebased onto `main`, and keeps the existing delayed-callback test; a separate serialized-completion case was reported in that PR's review. --- _Generated by [Claude Code](https://claude.ai/code)_
This discussion has been locked. Commenting is limited to contributors.
No milestone
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set

Reference
github/Marvisak--VIctory#1637
No description provided.