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

Open
opened 2026-11-24 20:15:59 +00:00 by AndyLatimer · 3 comments
AndyLatimer commented 2026-11-24 20:15:59 +00:00 (Migrated from github.com)
Contributor

Imported from Marvisak/VIctory. Originally posted by AndyLatimer on 2026-11-24T20:15:59Z.


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).
<!-- greyfinch-sync:Marvisak/VIctory:issue:6590559341 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/issues/1637). Originally posted by [AndyLatimer](https://github.com/AndyLatimer) on 2026-11-24T20:15:59Z. --- ## 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).
CaravelaLabs commented 2026-11-25 01:07:35 +00:00 (Migrated from github.com)
Author
Contributor

Imported from Marvisak/VIctory. Originally posted by CaravelaLabs on 2026-11-25T00:10:19Z.


@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.

<!-- greyfinch-sync:Marvisak/VIctory:comment:7148224502 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/issues/1637#issuecomment-7148224502). Originally posted by [CaravelaLabs](https://github.com/CaravelaLabs) on 2026-11-25T00:10:19Z. --- @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.
Calvix2k commented 2026-11-25 01:08:13 +00:00 (Migrated from github.com)
Author
Contributor

Imported from Marvisak/VIctory. Originally posted by Calvix2k on 2026-11-25T00:41:08Z.


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.

<!-- greyfinch-sync:Marvisak/VIctory:comment:7148769577 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/issues/1637#issuecomment-7148769577). Originally posted by [Calvix2k](https://github.com/Calvix2k) on 2026-11-25T00:41:08Z. --- 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.
Marvisak commented 2026-11-25 13:03:01 +00:00 (Migrated from github.com)
Author
Contributor

Imported from Marvisak/VIctory. Originally posted by Marvisak on 2026-11-25T12:51:52Z.


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

<!-- greyfinch-sync:Marvisak/VIctory:comment:7162962361 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/issues/1637#issuecomment-7162962361). Originally posted by [Marvisak](https://github.com/Marvisak) on 2026-11-25T12:51:52Z. --- <!-- 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)_
Sign in to join this conversation.
No labels
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.

Reference
Marvisak/VIctory#1636
No description provided.