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

Open
F1reworks10 wants to merge 1 commit from upstream-pr/2616 into main
F1reworks10 commented 2026-11-25 16:56:33 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by F1reworks10 on 2026-11-25T16:56:33Z.


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

<!-- greyfinch-sync:Marvisak/VIctory:issue:5495548774 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/2616). Originally posted by [F1reworks10](https://github.com/F1reworks10) on 2026-11-25T16:56:33Z. --- `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)
DrDarkDK commented 2026-11-25 17:00:27 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by DrDarkDK on 2026-11-25T17:00:27Z.

Review: CHANGES_REQUESTED


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.

<!-- greyfinch-sync:Marvisak/VIctory:review:6061664468 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/2616#pullrequestreview-6061664468). Originally posted by [DrDarkDK](https://github.com/DrDarkDK) on 2026-11-25T17:00:27Z. **Review:** CHANGES_REQUESTED --- 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.*
kotekompotek commented 2026-11-25 18:02:49 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by kotekompotek on 2026-11-25T17:45:04Z.


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.

<!-- greyfinch-sync:Marvisak/VIctory:comment:7169946570 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/2616#issuecomment-7169946570). Originally posted by [kotekompotek](https://github.com/kotekompotek) on 2026-11-25T17:45:04Z. --- 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.
Marvisak commented 2026-11-25 23:06:02 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by Marvisak on 2026-11-25T22:15:22Z.


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

<!-- greyfinch-sync:Marvisak/VIctory:comment:7176132067 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/2616#issuecomment-7176132067). Originally posted by [Marvisak](https://github.com/Marvisak) on 2026-11-25T22:15:22Z. --- <!-- 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)_
cloph-dsp commented 2026-11-27 14:33:25 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by cloph-dsp on 2026-11-27T14:33:25Z.


Independent CPU-only verification on Windows / Rust 1.100.0 / MSVC.

  • PR head: 55da126c87b47587c812518f352d757287f84f56, merge base 63bcca43c6a00a758510398fa43096ce7e4f7df0.
  • Command: cargo test --locked -p victory-hle --lib -- modules::xasync::tests::dispatch_runs_a_serialized_ports_callback_on_the_calling_thread --exact --test-threads=1 --quiet
  • Result: test result: ok. 1 passed; 0 failed; 0 ignored; 0 measured; 154 filtered out, exit 0.
  • Negative control: git grep -F -c dispatch_runs_a_serialized_ports_callback_on_the_calling_thread 63bcca43 -- . → exit 1 / 0 hits (test genuinely new); present on PR head.
  • git diff --check 63bcca43 HEAD → exit 0.
  • Caveat: only the named synthetic thread-pool test was replayed; real XTaskQueue host behaviour and the full crate suite were not exercised.
  • GPU/driver: N/A (CPU-only test). No game data, assets, extracted shaders or DRM involved.
  • AI-assisted: Hy4 flash.
<!-- greyfinch-sync:Marvisak/VIctory:comment:7220933398 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/2616#issuecomment-7220933398). Originally posted by [cloph-dsp](https://github.com/cloph-dsp) on 2026-11-27T14:33:25Z. --- Independent CPU-only verification on Windows / Rust 1.100.0 / MSVC. - PR head: `55da126c87b47587c812518f352d757287f84f56`, merge base `63bcca43c6a00a758510398fa43096ce7e4f7df0`. - Command: `cargo test --locked -p victory-hle --lib -- modules::xasync::tests::dispatch_runs_a_serialized_ports_callback_on_the_calling_thread --exact --test-threads=1 --quiet` - Result: `test result: ok. 1 passed; 0 failed; 0 ignored; 0 measured; 154 filtered out`, exit 0. - Negative control: `git grep -F -c dispatch_runs_a_serialized_ports_callback_on_the_calling_thread 63bcca43 -- .` → exit 1 / 0 hits (test genuinely new); present on PR head. - `git diff --check 63bcca43 HEAD` → exit 0. - Caveat: only the named synthetic thread-pool test was replayed; real XTaskQueue host behaviour and the full crate suite were not exercised. - GPU/driver: N/A (CPU-only test). No game data, assets, extracted shaders or DRM involved. - AI-assisted: Hy4 flash.
DrDarkDK commented 2026-11-27 20:24:34 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by DrDarkDK on 2026-11-27T20:24:34Z.

Review: CHANGES_REQUESTED


I reproduced the shared-port cancellation on the rebased head 55da126. I submitted queue A’s callback to a thread-pool port shared with composite queue B, held the pool turn pending, then terminated B.

On base 63bcca4, A’s callback stayed pending and ran uncanceled when I executed the pool turn. On this head, terminating B ran A’s callback immediately with canceled = true, and the pool turn found nothing left to run.

Could termination cancel only callbacks owned by the terminating queue, with this shared thread-pool case as the regression? #403 shows the queue-ownership approach.

Tested on Windows with Rust 1.100.0 MSVC in a standalone harness using the queue code from both revisions. The harness controlled the worker turn. No game or full crate run.

AI-assisted with Codex.

<!-- greyfinch-sync:Marvisak/VIctory:review:6093040956 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/2616#pullrequestreview-6093040956). Originally posted by [DrDarkDK](https://github.com/DrDarkDK) on 2026-11-27T20:24:34Z. **Review:** CHANGES_REQUESTED --- I reproduced the shared-port cancellation on the rebased head `55da126`. I submitted queue A’s callback to a thread-pool port shared with composite queue B, held the pool turn pending, then terminated B. On base `63bcca4`, A’s callback stayed pending and ran uncanceled when I executed the pool turn. On this head, terminating B ran A’s callback immediately with `canceled = true`, and the pool turn found nothing left to run. Could termination cancel only callbacks owned by the terminating queue, with this shared thread-pool case as the regression? #403 shows the queue-ownership approach. Tested on Windows with Rust 1.100.0 MSVC in a standalone harness using the queue code from both revisions. The harness controlled the worker turn. No game or full crate run. *AI-assisted with Codex.*
This pull request cannot be merged automatically due to conflicts.
Merge manually to resolve the conflicts.
View command line instructions

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u origin 'upstream-pr/2616:upstream-pr/2616'
git switch 'upstream-pr/2616'

Merge

Merge the changes and update on Forgejo.

Warning: The "Autodetect manual merge" setting is not enabled for this repository, you will have to mark this pull request as manually merged afterwards.

git switch 'main'
git merge --no-ff 'upstream-pr/2616'
git switch 'upstream-pr/2616'
git rebase 'main'
git switch 'main'
git merge --ff-only 'upstream-pr/2616'
git switch 'upstream-pr/2616'
git rebase 'main'
git switch 'main'
git merge --no-ff 'upstream-pr/2616'
git switch 'main'
git merge --squash 'upstream-pr/2616'
git switch 'main'
git merge --ff-only 'upstream-pr/2616'
git switch 'main'
git merge 'upstream-pr/2616'
git push origin 'main'
Sign in to join this conversation.
No reviewers
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!2614
No description provided.