XTaskQueue: a dispatch serves every port, not only manual ones #2616

Open
github-bot wants to merge 1 commit from github-bot/gh-2616 into main AGit
Owner

@⁠F1reworks10 on GitHub · 2026-11-25 16:56 UTC · original
F1reworks10:xtaskqueue-dispatch-any-mode → main · +122 −14 · 1 files · 1 commits · open

XTaskQueueDispatch returned false immediately for thread-pool, serialized and immediate ports. It ran nothing and did not wait out its timeout.

The GDK documentation for XTaskQueueDispatch says:

  • "XTaskQueueDispatch can be called for any dispatch mode but note that a call is always dispatched on the thread that calls XTaskQueueDispatch."
  • timeoutInMs: "The number of milliseconds to wait for work to arrive before returning false."

The public reference implementation behaves the same way. In libHttpClient's TaskQueue.cpp, TaskQueuePortImpl::Dispatch takes one entry from the port's queue whatever the port's mode, and waits up to the timeout for one.

On main this has two consequences:

  • A title loop that dispatches a thread-pool port with a timeout spins instead of waiting.
  • A thread that helps a busy port by dispatching it never runs anything.

Change (crates/victory-hle/src/modules/xasync.rs)

  • Every port except an immediate one queues its callbacks, and XTaskQueueDispatch takes the first one on the calling thread whatever the mode.
  • An immediate port never queues anything, so dispatching it waits out the timeout and returns false, as the reference does.
  • A thread-pool port gives the pool one turn per callback (run_first). That turn runs the port's first callback unless a dispatch already took it, so each callback still runs exactly once.
  • XTaskQueueTerminate now also cancels thread-pool callbacks that are still waiting for a pool thread. This matches its documentation: "All callbacks for both ports will be invoked with their canceled parameter set to true". On main the pool ran them uncancelled.

Tests

  • dispatch_runs_a_serialized_ports_callback_on_the_calling_thread: fails on main, because the dispatch returns false immediately.
  • dispatch_waits_its_timeout_on_a_thread_pool_port: fails on main, because the dispatch returns at once instead of after 200 ms.
  • thread_pool_callbacks_run_once_wherever_they_run: checks that no callback runs twice when dispatches and the pool compete for them.

cargo test -p victory-hle passes (149 tests). Clippy is clean with -D warnings, with and without --all-features. Tested on Windows 11 with an RTX 3060 Ti.

🤖 Generated with Claude Code

**@⁠F1reworks10** on GitHub · 2026-11-25 16:56 UTC · [original](https://github.com/Marvisak/VIctory/pull/2616) `F1reworks10:xtaskqueue-dispatch-any-mode` → `main` · +122 −14 · 1 files · 1 commits · open `XTaskQueueDispatch` returned `false` immediately for thread-pool, serialized and immediate ports. It ran nothing and did not wait out its timeout. The GDK documentation for [XTaskQueueDispatch](https://learn.microsoft.com/en-us/gaming/gdk/docs/reference/system/xtaskqueue/functions/xtaskqueuedispatch) says: - "XTaskQueueDispatch can be called for any dispatch mode but note that a call is always dispatched on the thread that calls XTaskQueueDispatch." - `timeoutInMs`: "The number of milliseconds to wait for work to arrive before returning false." The public reference implementation behaves the same way. In [libHttpClient's `TaskQueue.cpp`](https://github.com/microsoft/libHttpClient/blob/main/Source/Task/TaskQueue.cpp), `TaskQueuePortImpl::Dispatch` takes one entry from the port's queue whatever the port's mode, and waits up to the timeout for one. On main this has two consequences: - A title loop that dispatches a thread-pool port with a timeout spins instead of waiting. - A thread that helps a busy port by dispatching it never runs anything. ### Change (`crates/victory-hle/src/modules/xasync.rs`) - Every port except an immediate one queues its callbacks, and `XTaskQueueDispatch` takes the first one on the calling thread whatever the mode. - An immediate port never queues anything, so dispatching it waits out the timeout and returns `false`, as the reference does. - A thread-pool port gives the pool one turn per callback (`run_first`). That turn runs the port's first callback unless a dispatch already took it, so each callback still runs exactly once. - `XTaskQueueTerminate` now also cancels thread-pool callbacks that are still waiting for a pool thread. This matches [its documentation](https://learn.microsoft.com/en-us/gaming/gdk/docs/reference/system/xtaskqueue/functions/xtaskqueueterminate): "All callbacks for both ports will be invoked with their canceled parameter set to true". On main the pool ran them uncancelled. ### Tests - `dispatch_runs_a_serialized_ports_callback_on_the_calling_thread`: fails on main, because the dispatch returns `false` immediately. - `dispatch_waits_its_timeout_on_a_thread_pool_port`: fails on main, because the dispatch returns at once instead of after 200 ms. - `thread_pool_callbacks_run_once_wherever_they_run`: checks that no callback runs twice when dispatches and the pool compete for them. `cargo test -p victory-hle` passes (149 tests). Clippy is clean with `-D warnings`, with and without `--all-features`. Tested on Windows 11 with an RTX 3060 Ti. 🤖 Generated with [Claude Code](https://claude.com/claude-code)
What was wrong: XTaskQueueDispatch returned false at once for a
thread-pool, serialized or immediate port, without running anything and
without waiting out its timeout. The GDK documentation of the function
says it "can be called for any dispatch mode", that "a call is always
dispatched on the thread that calls XTaskQueueDispatch", and that the
timeout is "the number of milliseconds to wait for work to arrive before
returning false". The public reference (libHttpClient's TaskQueue.cpp)
drains one entry of the port's queue whatever the port's mode. A title
loop that dispatches a thread-pool port with a timeout spun instead of
waiting, and a thread that helps a busy port by dispatching it never ran
anything.

What changed: every port but an immediate one queues its callbacks, and
XTaskQueueDispatch takes the first on the calling thread whatever the
mode; an immediate port never queues one, so a dispatch of it waits out
its timeout, as in the reference. A thread-pool port gives the pool one
turn per callback, which runs the port's first callback unless a
dispatch took it, so each callback still runs exactly once. Callbacks of
a thread-pool port still waiting for a pool thread are now among those
XTaskQueueTerminate cancels, as its documentation says ("All callbacks
for both ports will be invoked with their canceled parameter set to
true"); before, the pool ran them uncanceled.

Evidence: `dispatch_runs_a_serialized_ports_callback_on_the_calling_thread`
and `dispatch_waits_its_timeout_on_a_thread_pool_port` fail on main (the
dispatch returns false at once). `thread_pool_callbacks_run_once_wherever_
they_run` checks that a callback a dispatch took is not run by the pool
as well. `cargo test -p victory-hle` passes; clippy (with and without
--all-features) is clean.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 05:10:25 +00:00
Author
Owner

@⁠kotekompotek on GitHub · 2026-11-25 17:45 UTC · original

Reviewed: moving ThreadPool onto the per-port task list plus a pool 'turn' that pops the front keeps FIFO order while letting XTaskQueueDispatch serve any mode (per the docs, dispatch works on any dispatch mode). Turns are 1:1 with submissions, so a dispatch that drained the queue leaves a turn that harmlessly finds it empty; the extra Arc raw-ptr round trip is reclaimed in run_first. CI green.

**@⁠kotekompotek** on GitHub · 2026-11-25 17:45 UTC · [original](https://github.com/Marvisak/VIctory/pull/2616#issuecomment-7169946570) Reviewed: moving ThreadPool onto the per-port task list plus a pool 'turn' that pops the front keeps FIFO order while letting `XTaskQueueDispatch` serve any mode (per the docs, dispatch works on any dispatch mode). Turns are 1:1 with submissions, so a dispatch that drained the queue leaves a turn that harmlessly finds it empty; the extra Arc raw-ptr round trip is reclaimed in `run_first`. CI green.
Author
Owner

@⁠Marvisak on GitHub · 2026-11-25 22:15 UTC · original

Automated triage for the maintainers.

This PR no longer merges cleanly into main (f3578e3). The conflict is in crates/victory-hle/src/modules/xasync.rs. It will be reviewed once it is rebased onto current main.


Generated by Claude Code

**@⁠Marvisak** on GitHub · 2026-11-25 22:15 UTC · [original](https://github.com/Marvisak/VIctory/pull/2616#issuecomment-7176132067) <!-- victory-triage v1 head=2db677657bb9680c419394be4e8ccaed7c630700 verdict=needs-rebase at=2026-11-25T22:15:30Z --> Automated triage for the maintainers. This PR no longer merges cleanly into `main` (f3578e3). The conflict is in `crates/victory-hle/src/modules/xasync.rs`. It will be reviewed once it is rebased onto current `main`. --- _Generated by [Claude Code](https://claude.ai/code)_
github-bot force-pushed github-bot/gh-2616 from 2db677657b to 93e93a0e9e 2026-11-26 16:22:13 +00:00 Compare
Author
Owner

@⁠DrDarkDK on GitHub · 2026-11-25 17:00 UTC · original · requested changes

This extends the shared-port cancellation bug from #403 to thread-pool ports. On the base, those callbacks go straight to POOL. With this PR they enter the shared Port::tasks deque, which XTaskQueueTerminate drains in full. If queue A and composite queue B share a thread-pool port, terminating B can now invoke A’s queued callback with canceled = true.

Could we reuse #403’s approach of tracking which queue owns each callback and add a regression test for shared thread-pool ports? libHttpClient also limits cancellation to the terminating queue’s port context. Reference implementation

Static review only.

AI-assisted with Codex.

**@⁠DrDarkDK** on GitHub · 2026-11-25 17:00 UTC · [original](https://github.com/Marvisak/VIctory/pull/2616#pullrequestreview-6061664468) · **requested changes** This extends the shared-port cancellation bug from #403 to thread-pool ports. On the base, those callbacks go straight to `POOL`. With this PR they enter the shared `Port::tasks` deque, which `XTaskQueueTerminate` drains in full. If queue A and composite queue B share a thread-pool port, terminating B can now invoke A’s queued callback with `canceled = true`. Could we reuse #403’s approach of tracking which queue owns each callback and add a regression test for shared thread-pool ports? libHttpClient also limits cancellation to the terminating queue’s port context. [Reference implementation](https://github.com/microsoft/libHttpClient/blob/main/Source/Task/TaskQueue.cpp#L1017) Static review only. *AI-assisted with Codex.*
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/2616/head:github-bot/gh-2616'
git switch 'github-bot/gh-2616'

Merge

Merge the changes and update on Forgejo.
git switch 'main'
git merge --no-ff 'github-bot/gh-2616'
git switch 'github-bot/gh-2616'
git rebase 'main'
git switch 'main'
git merge --ff-only 'github-bot/gh-2616'
git switch 'github-bot/gh-2616'
git rebase 'main'
git switch 'main'
git merge --no-ff 'github-bot/gh-2616'
git switch 'main'
git merge --squash 'github-bot/gh-2616'
git switch 'main'
git merge --ff-only 'github-bot/gh-2616'
git switch 'main'
git merge 'github-bot/gh-2616'
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!2616
No description provided.