HLE: XTaskQueueTerminate cancels only its own tasks on shared ports #1673

Open
github-bot wants to merge 3 commits from github-bot/gh-1673 into main AGit
Owner

@⁠Calvix2k on GitHub · 2026-11-24 20:40 UTC · original
Calvix2k:codex/hle-xtaskqueue-shared-ports-1637 → main · +600 −15 · 2 files · 3 commits · open

Fixes #1637.

Ports the verified fix from @⁠AndyLatimer's fork (commit 47e61a2 by Manuel Pérez), applied unchanged onto ffe310e; the commit keeps its original author.

Summary

XTaskQueueTerminate drained every port unconditionally, so terminating one queue cancelled the queued callbacks and pending XAsync work of every other queue sharing that port (manual, serialized, or composite queues mapping both ports to one shared port).

  • Each Queue gets a monotonic id, shared across XTaskQueueDuplicateHandle and distinct for composite queues.
  • Queued entries carry their owner id; Port::take_owned removes only the terminating queue's tasks, keeping other queues' order.
  • Port::submit re-checks queue.terminated under the port lock, so a task racing with termination is cancelled instead of enqueued after the drain.

Tests

Three new synthetic tests (also in the commit): shared manual port with duplicate handles and a composite queue on one port, shared serialized port, and pending XAsyncRun calls across composite queues. All pass with the fix and fail before it.

Validation

  • cargo test -p victory-hle --locked -- --test-threads=1 — passed (97 unit tests and 1 integration test).
  • cargo clippy -p victory-hle --all-targets --all-features --locked -- -D warnings — passed.
  • cargo xtask policy — passed (154 files).
  • rustfmt --edition 2024 --check on the file reports only pre-existing hunks in untouched code.
**@⁠Calvix2k** on GitHub · 2026-11-24 20:40 UTC · [original](https://github.com/Marvisak/VIctory/pull/1673) `Calvix2k:codex/hle-xtaskqueue-shared-ports-1637` → `main` · +600 −15 · 2 files · 3 commits · open Fixes #1637. Ports the verified fix from [@⁠AndyLatimer's fork](https://github.com/AndyLatimer/VIctory/tree/fix/hle-xtaskqueue-terminate-shared-ports) (commit 47e61a2 by Manuel Pérez), applied unchanged onto `ffe310e`; the commit keeps its original author. ## Summary `XTaskQueueTerminate` drained every port unconditionally, so terminating one queue cancelled the queued callbacks and pending `XAsync` work of every other queue sharing that port (manual, serialized, or composite queues mapping both ports to one shared port). - Each `Queue` gets a monotonic id, shared across `XTaskQueueDuplicateHandle` and distinct for composite queues. - Queued entries carry their owner id; `Port::take_owned` removes only the terminating queue's tasks, keeping other queues' order. - `Port::submit` re-checks `queue.terminated` under the port lock, so a task racing with termination is cancelled instead of enqueued after the drain. ## Tests Three new synthetic tests (also in the commit): shared manual port with duplicate handles and a composite queue on one port, shared serialized port, and pending `XAsyncRun` calls across composite queues. All pass with the fix and fail before it. ## Validation - `cargo test -p victory-hle --locked -- --test-threads=1` — passed (97 unit tests and 1 integration test). - `cargo clippy -p victory-hle --all-targets --all-features --locked -- -D warnings` — passed. - `cargo xtask policy` — passed (154 files). - `rustfmt --edition 2024 --check` on the file reports only pre-existing hunks in untouched code.
A task already taken from a shared port (the serialized worker, or a
dispatcher woken by the queue) ran with `canceled = false` even when its
own queue was terminated first: only the queue's id travelled with it.
Carry the queue's termination flag in the task and read it when the task
runs; `take_owned` matches by that flag's identity.

By avt0m4t48 (the follow-up posted on #1673), rebased onto this branch
with the queue's delayed-callback map kept, and its integration tests
included.
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 04:41:03 +00:00
Author
Owner

@⁠avt0m4t48 on GitHub · 2026-11-24 22:38 UTC · original

I independently tested exact head dda166c8 with a source-built public queue API fixture. It preserves sibling FIFO and reproduces the reviewed manual reentrant case. A separate serialized-worker control also runs the terminating owner's already queued completion with canceled=false where the owner state expects true.

A follow-up that carries the submitting queue's cancellation state into both dispatch paths passed all 13 isolated Linux tests, including the three API cases, which also passed 25 repeats. The exact PR plus follow-up passed Windows-target checks for victory-hle and victory-gpu tests with blake3/pure. Windows runtime was outside these checks. Tested follow-up with API fixtures

AI-assisted using Codex.

**@⁠avt0m4t48** on GitHub · 2026-11-24 22:38 UTC · [original](https://github.com/Marvisak/VIctory/pull/1673#issuecomment-7146472244) I independently tested exact head `dda166c8` with a source-built public queue API fixture. It preserves sibling FIFO and reproduces the reviewed manual reentrant case. A separate serialized-worker control also runs the terminating owner's already queued completion with `canceled=false` where the owner state expects true. A follow-up that carries the submitting queue's cancellation state into both dispatch paths passed all 13 isolated Linux tests, including the three API cases, which also passed 25 repeats. The exact PR plus follow-up passed Windows-target checks for `victory-hle` and `victory-gpu` tests with `blake3/pure`. Windows runtime was outside these checks. [Tested follow-up with API fixtures](https://github.com/avt0m4t48/VIctory/commit/13a01c33a1fa4488a7925bae3db93d97a0d6ab92) AI-assisted using Codex.
Author
Owner

@⁠AgneyaK on GitHub · 2026-11-25 02:17 UTC · original

Verified locally: merged 0ef707e onto main 940d32f (clean), Windows 11 26300, Rust 1.98.1 MSVC.

  • The new tests catch the bug. With only this PR's three new tests put on main's xasync.rs, all three fail: terminate_on_shared_manual_port_only_cancels_its_own_tasks, terminate_on_shared_serialized_port_keeps_other_queues_tasks, and terminate_composite_queue_preserves_other_queues_async_calls (left: -2147467260 (E_ABORT), right: -2147483638 (E_PENDING)). With the PR they pass.
  • No flakiness seen: cargo test -p victory-hle --lib -- xasync 10 runs in a row, 15 of 15 every time.
  • cargo clippy -p victory-hle --all-targets --locked -- -D warnings: clean.
  • Read the diff:
    • Owner ids come from the queue, not the handle, so a duplicated handle (a_dup in the test) shares its queue's tasks, as it should.
    • Checking terminated under the port lock in Port::submit closes the submit/terminate race from #1637.
    • take_owned keeps both lists in FIFO order.

Not tested: the title (this path matters for its shared ports, but I can't run the story load on this machine).

AI use: Claude Code (Claude Opus 5.5) ran the checks and wrote this; the results are from the runs described.

**@⁠AgneyaK** on GitHub · 2026-11-25 02:17 UTC · [original](https://github.com/Marvisak/VIctory/pull/1673#issuecomment-7150536341) Verified locally: merged `0ef707e` onto `main` `940d32f` (clean), Windows 11 26300, Rust 1.98.1 MSVC. - **The new tests catch the bug.** With only this PR's three new tests put on `main`'s `xasync.rs`, all three fail: `terminate_on_shared_manual_port_only_cancels_its_own_tasks`, `terminate_on_shared_serialized_port_keeps_other_queues_tasks`, and `terminate_composite_queue_preserves_other_queues_async_calls` (`left: -2147467260` (`E_ABORT`), `right: -2147483638` (`E_PENDING`)). With the PR they pass. - **No flakiness seen:** `cargo test -p victory-hle --lib -- xasync` 10 runs in a row, 15 of 15 every time. - `cargo clippy -p victory-hle --all-targets --locked -- -D warnings`: clean. - Read the diff: - Owner ids come from the queue, not the handle, so a duplicated handle (`a_dup` in the test) shares its queue's tasks, as it should. - Checking `terminated` under the port lock in `Port::submit` closes the submit/terminate race from #1637. - `take_owned` keeps both lists in FIFO order. Not tested: the title (this path matters for its shared ports, but I can't run the story load on this machine). AI use: Claude Code (Claude Opus 5.5) ran the checks and wrote this; the results are from the runs described.
Author
Owner

@⁠Marvisak on GitHub · 2026-11-25 03:33 UTC · original · edited 2026-11-25 22:25 UTC

Automated triage for the maintainers.

Needs rebase. This PR no longer merges cleanly into main: main has moved since the last triage, and the branch now conflicts in crates/victory-hle/src/modules/xasync.rs. It will be reviewed again once it is rebased onto current main.

Previous verdict for this head: merge candidate.


Generated by Claude Code

**@⁠Marvisak** on GitHub · 2026-11-25 03:33 UTC · [original](https://github.com/Marvisak/VIctory/pull/1673#issuecomment-7152017948) · edited 2026-11-25 22:25 UTC <!-- victory-triage v1 head=36c7741c585b616cd6384cf6359f5292ecccb522 verdict=needs-rebase at=2026-11-25T22:28:30Z --> Automated triage for the maintainers. **Needs rebase.** This PR no longer merges cleanly into `main`: `main` has moved since the last triage, and the branch now conflicts in `crates/victory-hle/src/modules/xasync.rs`. It will be reviewed again once it is rebased onto current `main`. Previous verdict for this head: merge candidate. --- _Generated by [Claude Code](https://claude.ai/code)_
Author
Owner

@⁠Calvix2k on GitHub · 2026-11-25 09:04 UTC · original

@⁠avt0m4t48 Thank you — I folded your follow-up in rather than leaving it beside the PR.

  • 36c7741 is your 13a01c3 ("HLE: use submitting queue cancellation at dispatch"), cherry-picked onto this branch with your authorship kept, rebased over the delayed-callback map this branch adds (delayed: Mutex<BTreeMap<u64, Task>> stays).
  • What changed at the merge: tasks carry Arc<AtomicBool> (the queue's termination flag) in a PendingTask instead of the queue id, take_owned matches with Arc::ptr_eq, and both dispatch paths (XTaskQueueDispatch and the serialized worker) read the flag at the moment the task runs — so a completion taken from a shared port after its queue was terminated now runs with canceled = true, which is the control you reported failing.
  • Your tests/xtaskqueue_shared_ports.rs is in the PR.

Verified here (Windows 10 19045): cargo test -p victory-hle — 146 lib tests pass, xtaskqueue_shared_ports 3/3 (serialized_worker_cancels_completion_during_work_port_termination, reentrant_dispatch_uses_the_submitting_queues_cancellation_state, terminating_composite_preserves_original_callbacks_in_fifo_order), clippy -D warnings clean. CI is running on the new head.

With that, the serialized-worker control is covered by a test in the PR itself, so the triage's merge-candidate reading now holds for that path too.

**@⁠Calvix2k** on GitHub · 2026-11-25 09:04 UTC · [original](https://github.com/Marvisak/VIctory/pull/1673#issuecomment-7157847325) @⁠avt0m4t48 Thank you — I folded your follow-up in rather than leaving it beside the PR. - **`36c7741`** is your `13a01c3` ("HLE: use submitting queue cancellation at dispatch"), cherry-picked onto this branch with your authorship kept, rebased over the delayed-callback map this branch adds (`delayed: Mutex<BTreeMap<u64, Task>>` stays). - What changed at the merge: tasks carry `Arc<AtomicBool>` (the queue's termination flag) in a `PendingTask` instead of the queue id, `take_owned` matches with `Arc::ptr_eq`, and both dispatch paths (`XTaskQueueDispatch` and the serialized worker) read the flag at the moment the task runs — so a completion taken from a shared port after its queue was terminated now runs with `canceled = true`, which is the control you reported failing. - Your `tests/xtaskqueue_shared_ports.rs` is in the PR. Verified here (Windows 10 19045): `cargo test -p victory-hle` — 146 lib tests pass, `xtaskqueue_shared_ports` **3/3** (`serialized_worker_cancels_completion_during_work_port_termination`, `reentrant_dispatch_uses_the_submitting_queues_cancellation_state`, `terminating_composite_preserves_original_callbacks_in_fifo_order`), clippy `-D warnings` clean. CI is running on the new head. With that, the serialized-worker control is covered by a test in the PR itself, so the triage's merge-candidate reading now holds for that path too.
Author
Owner

@⁠StExpeditus on GitHub · 2026-11-25 09:13 UTC · original

Test report: RTX 5060 Ti (Blackwell), Windows 11 Pro 26200, driver 621.24. Branch at 36c7741. cargo test --workspace --locked --no-fail-fast -> 702 passed, 5 failed, 10 ignored. The failures are the same 5 pre-existing NVIDIA 1M-view-heap-cap failures (also on main; see my reports on #2030 and #1940). Nothing introduced by this PR fails on this machine.

**@⁠StExpeditus** on GitHub · 2026-11-25 09:13 UTC · [original](https://github.com/Marvisak/VIctory/pull/1673#issuecomment-7158032973) **Test report: RTX 5060 Ti (Blackwell), Windows 11 Pro 26200, driver 621.24.** Branch at `36c7741`. `cargo test --workspace --locked --no-fail-fast` -> 702 passed, 5 failed, 10 ignored. The failures are the same 5 pre-existing NVIDIA 1M-view-heap-cap failures (also on main; see my reports on #2030 and #1940). Nothing introduced by this PR fails on this machine.
Author
Owner

@⁠Calvix2k on GitHub · 2026-11-25 16:39 UTC · original

Thanks — the same five NVIDIA heap-cap failures as on main, and nothing from this PR. Noted on the record.

**@⁠Calvix2k** on GitHub · 2026-11-25 16:39 UTC · [original](https://github.com/Marvisak/VIctory/pull/1673#issuecomment-7168325877) Thanks — the same five NVIDIA heap-cap failures as on `main`, and nothing from this PR. Noted on the record.
Author
Owner

@⁠BaggyALPHA on GitHub · 2026-11-24 21:28 UTC · original · reviewed

The three new shared-port tests passed independently on Windows. A synthetic reentrant-dispatch test found one remaining cancellation case. Two manual queues share their ports. Queue B has a work callback and a completion callback pending, and queue A has a completion callback pending. While B is terminating, its canceled work callback dispatches the shared completion port through A. B's completion then receives canceled=false, and A's completion correctly receives canceled=false.

The termination flag is already set on B when this happens. Port::take drops the submitting queue identity, and XTaskQueueDispatch always supplies false. The dispatcher needs to read the submitting queue's cancellation state when it invokes the callback, including on the serialized worker path. Microsoft's libHttpClient reference implementation does this in DrainOneItem using the entry's port context. Keeping shared cancellation state with the queued entry would let dispatch distinguish the two callbacks.

AI-assisted (Codex). Tested with synthetic callbacks.

**@⁠BaggyALPHA** on GitHub · 2026-11-24 21:28 UTC · [original](https://github.com/Marvisak/VIctory/pull/1673#pullrequestreview-6047705945) · **reviewed** The three new shared-port tests passed independently on Windows. A synthetic reentrant-dispatch test found one remaining cancellation case. Two manual queues share their ports. Queue B has a work callback and a completion callback pending, and queue A has a completion callback pending. While B is terminating, its canceled work callback dispatches the shared completion port through A. B's completion then receives canceled=false, and A's completion correctly receives canceled=false. The termination flag is already set on B when this happens. Port::take drops the submitting queue identity, and XTaskQueueDispatch always supplies false. The dispatcher needs to read the submitting queue's cancellation state when it invokes the callback, including on the serialized worker path. Microsoft's [libHttpClient reference implementation](https://github.com/microsoft/libHttpClient/blob/main/Source/Task/TaskQueue.cpp) does this in DrainOneItem using the entry's port context. Keeping shared cancellation state with the queued entry would let dispatch distinguish the two callbacks. AI-assisted (Codex). Tested with synthetic callbacks.
Author
Owner

@⁠catminou4 on GitHub · 2026-11-24 21:42 UTC · original · reviewed

Static review only: Windows-only code (D3D12/windows_sys/Win32) — nothing compiles or runs on macOS; source review of the diff.

XTaskQueueTerminate now cancels only the terminating queue's own tasks on shared ports: submit() takes an owner id + terminated flag, take_owned() collects only that owner's tasks, and two tests cover manual and serialized shared ports (record/hold extern callbacks log cancellation). Previously terminating one queue could cancel another queue's tasks on the same port — a real correctness fix for shared-port titles.

**@⁠catminou4** on GitHub · 2026-11-24 21:42 UTC · [original](https://github.com/Marvisak/VIctory/pull/1673#pullrequestreview-6047874747) · **reviewed** Static review only: Windows-only code (D3D12/windows_sys/Win32) — nothing compiles or runs on macOS; source review of the diff. XTaskQueueTerminate now cancels only the terminating queue's own tasks on shared ports: submit() takes an owner id + terminated flag, take_owned() collects only that owner's tasks, and two tests cover manual and serialized shared ports (record/hold extern callbacks log cancellation). Previously terminating one queue could cancel another queue's tasks on the same port — a real correctness fix for shared-port titles.
Author
Owner

@⁠andrewpozdnakov7-jpg on GitHub · 2026-11-26 13:31 UTC · original

I resolved the current xasync.rs test conflict on e02e9ef. The first parent is your exact 36c7741, so the original shared-port fix and @⁠avt0m4t48’s follow-up keep their authorship. The second parent is upstream 63bcca4. The resolution includes the shared-port regressions and main's test that checks provider cleanup runs with the completion lock released.

On Windows, the integrated tree passed all 155 HLE unit tests and all five integration tests. That includes the three shared-port integration cases for FIFO order, reentrant dispatch cancellation, and the serialized worker. Strict HLE Clippy with all features and targets and the asset policy check passed.

The branch fast-forwards from 36c7741. If you fold the integration into another commit, please add Co-authored-by: andrewpozdnakov7-jpg <[email protected]>.

AI-assisted with Codex.

**@⁠andrewpozdnakov7-jpg** on GitHub · 2026-11-26 13:31 UTC · [original](https://github.com/Marvisak/VIctory/pull/1673#issuecomment-7193477064) I resolved the current `xasync.rs` test conflict on [e02e9ef](https://github.com/andrewpozdnakov7-jpg/VIctory/commit/e02e9efd4e94d002971c717b87ef58296555c927). The first parent is your exact `36c7741`, so the original shared-port fix and @⁠avt0m4t48’s follow-up keep their authorship. The second parent is upstream `63bcca4`. The resolution includes the shared-port regressions and main's test that checks provider cleanup runs with the completion lock released. On Windows, the integrated tree passed all 155 HLE unit tests and all five integration tests. That includes the three shared-port integration cases for FIFO order, reentrant dispatch cancellation, and the serialized worker. Strict HLE Clippy with all features and targets and the asset policy check passed. The branch fast-forwards from `36c7741`. If you fold the integration into another commit, please add `Co-authored-by: andrewpozdnakov7-jpg <[email protected]>`. AI-assisted with Codex.
Author
Owner

@⁠Calvix2k on GitHub · 2026-11-26 18:51 UTC · original

@⁠andrewpozdnakov7-jpg thank you — I took your merge as the branch head (e02e9ef) unchanged: it keeps our shared-port fix and @⁠avt0m4t48's follow-up, brings main in, and cargo check -p victory-hle --all-targets passes here on Windows 10. The PR is mergeable again.

**@⁠Calvix2k** on GitHub · 2026-11-26 18:51 UTC · [original](https://github.com/Marvisak/VIctory/pull/1673#issuecomment-7200931927) @⁠andrewpozdnakov7-jpg thank you — I took your merge as the branch head (`e02e9ef`) unchanged: it keeps our shared-port fix and @⁠avt0m4t48's follow-up, brings main in, and `cargo check -p victory-hle --all-targets` passes here on Windows 10. The PR is mergeable again.
This pull request can be merged automatically.
You are not authorized to merge this pull request.
View command line instructions

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u origin '+refs/pull/1673/head:github-bot/gh-1673'
git switch 'github-bot/gh-1673'

Merge

Merge the changes and update on Forgejo.
git switch 'main'
git merge --no-ff 'github-bot/gh-1673'
git switch 'github-bot/gh-1673'
git rebase 'main'
git switch 'main'
git merge --ff-only 'github-bot/gh-1673'
git switch 'github-bot/gh-1673'
git rebase 'main'
git switch 'main'
git merge --no-ff 'github-bot/gh-1673'
git switch 'main'
git merge --squash 'github-bot/gh-1673'
git switch 'main'
git merge --ff-only 'github-bot/gh-1673'
git switch 'main'
git merge 'github-bot/gh-1673'
git push origin 'main'
This discussion has been locked. Commenting is limited to contributors.
No reviewers
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!1673
No description provided.