XTaskQueue: terminating a queue ends a dispatch waiting on it #239

Manually merged
github merged 1 commit from gh-pr/239/head into gh-pr/239/base 2026-11-26 03:56:05 +00:00
Owner

@⁠Maraco91 on GitHub · 2026-11-24 01:43 UTC · original
Maraco91:xtaskqueue/terminate-ends-dispatch → main · +82 −7 · 1 files · 1 commits · merged 2026-11-24 07:57 UTC

What

A thread that pumps a manual port never comes back once its queue is terminated.

The documented way to run a manual port on a thread of one's own is

while (XTaskQueueDispatch(queue, XTaskQueuePort::Work, INFINITE));

ended by XTaskQueueTerminate(queue, ...), after which the thread is joined: terminating the queue makes the waiting dispatch return false (the GDK's own sample for manual threads says so in a comment; from memory, I could not open it here).

Here XTaskQueueDispatch waits in Port::take until the port has a task. XTaskQueueTerminate sets the queue's flag, which refuses every later submit with E_ABORT, and drains the port, but wakes no one, and the wait does not look at the flag. So after the termination nothing can ever wake that dispatch: the thread stays in it for good, and so does whoever joins the thread (a hang when the title tears a queue down). A dispatch that starts after the termination waits forever too.

Now:

  • Port::take also ends when its caller says no task will come; XTaskQueueDispatch passes its queue's terminated flag, and returns false.
  • XTaskQueueTerminate wakes the port's waiters after draining it. It does so after taking the port's lock, so a dispatch either sees the flag before it waits or is woken.

The flag is the queue's, not the port's: a composite queue shares its ports, and a dispatch on another queue of the same port keeps waiting (checked below). The serialized port's own worker is unchanged (it passes "never").

I have not seen a title do this (I have no game files); middleware built on this API (libHttpClient ships in the package) runs dispatcher threads of this shape.

Tested

Windows 11 Pro 10.0.26200, rustc 1.98.1 (x86_64-pc-windows-msvc), no game data, no GPU involved.

New unit test terminate_ends_a_waiting_dispatch: queue A with manual ports and a composite queue B on the same ports; a thread runs the loop above on A.

  1. A callback submitted to A runs (the loop works).
  2. Control: B is terminated; A's loop must still be waiting 200 ms later, and still runs the next callback.
  3. A is terminated: the loop must end (5 s allowed).
  4. A dispatch on A with INFINITE after that returns false at once.

Results:

  • main (d2a4bae) + the test only: cargo test -p victory-hle --lib terminate_ends_a_waiting fails at step 3 after the 5 s: still waiting on a terminated queue. Steps 1 and 2 pass.
  • with the fix: cargo test -p victory-hle --lib xasync::: 6 passed, 0 failed, in 0.23 s, three runs in a row.
  • cargo clippy -p victory-hle --tests: nothing in xasync.rs. rustfmt --check: nothing in the lines changed (the file has existing differences at pending_with and near it).
  • cargo xtask policy: 151 files ok.
  • Trial merges (git merge-tree) with #132, #122 and #114, which change the same file: all clean. #132 adds its delayed callbacks to XTaskQueueTerminate after the port loop; this adds the wake inside it.

Not tested: a title run.

AI-assisted (Claude Code); the tests above were run on the machine described.

🤖 Generated with Claude Code

**@⁠Maraco91** on GitHub · 2026-11-24 01:43 UTC · [original](https://github.com/Marvisak/VIctory/pull/239) `Maraco91:xtaskqueue/terminate-ends-dispatch` → `main` · +82 −7 · 1 files · 1 commits · merged 2026-11-24 07:57 UTC ## What A thread that pumps a manual port never comes back once its queue is terminated. The documented way to run a manual port on a thread of one's own is ```c while (XTaskQueueDispatch(queue, XTaskQueuePort::Work, INFINITE)); ``` ended by `XTaskQueueTerminate(queue, ...)`, after which the thread is joined: terminating the queue makes the waiting dispatch return false (the GDK's own sample for manual threads says so in a comment; from memory, I could not open it here). Here `XTaskQueueDispatch` waits in `Port::take` until the port has a task. `XTaskQueueTerminate` sets the queue's flag, which refuses every later submit with `E_ABORT`, and drains the port, but wakes no one, and the wait does not look at the flag. So after the termination nothing can ever wake that dispatch: the thread stays in it for good, and so does whoever joins the thread (a hang when the title tears a queue down). A dispatch that starts after the termination waits forever too. Now: - `Port::take` also ends when its caller says no task will come; `XTaskQueueDispatch` passes its queue's `terminated` flag, and returns false. - `XTaskQueueTerminate` wakes the port's waiters after draining it. It does so after taking the port's lock, so a dispatch either sees the flag before it waits or is woken. The flag is the queue's, not the port's: a composite queue shares its ports, and a dispatch on another queue of the same port keeps waiting (checked below). The serialized port's own worker is unchanged (it passes "never"). I have not seen a title do this (I have no game files); middleware built on this API (libHttpClient ships in the package) runs dispatcher threads of this shape. ## Tested Windows 11 Pro 10.0.26200, rustc 1.98.1 (`x86_64-pc-windows-msvc`), no game data, no GPU involved. New unit test `terminate_ends_a_waiting_dispatch`: queue A with manual ports and a composite queue B on the same ports; a thread runs the loop above on A. 1. A callback submitted to A runs (the loop works). 2. Control: B is terminated; A's loop must still be waiting 200 ms later, and still runs the next callback. 3. A is terminated: the loop must end (5 s allowed). 4. A dispatch on A with `INFINITE` after that returns false at once. Results: - `main` (d2a4bae) + the test only: `cargo test -p victory-hle --lib terminate_ends_a_waiting` **fails** at step 3 after the 5 s: `still waiting on a terminated queue`. Steps 1 and 2 pass. - with the fix: `cargo test -p victory-hle --lib xasync::`: **6 passed, 0 failed**, in 0.23 s, three runs in a row. - `cargo clippy -p victory-hle --tests`: nothing in `xasync.rs`. `rustfmt --check`: nothing in the lines changed (the file has existing differences at `pending_with` and near it). - `cargo xtask policy`: 151 files ok. - Trial merges (`git merge-tree`) with #132, #122 and #114, which change the same file: all clean. #132 adds its delayed callbacks to `XTaskQueueTerminate` after the port loop; this adds the wake inside it. Not tested: a title run. AI-assisted (Claude Code); the tests above were run on the machine described. 🤖 Generated with [Claude Code](https://claude.com/claude-code)
XTaskQueueDispatch with an infinite timeout waited until a callback was
queued. Terminating the queue refuses new callbacks and drained the
queued ones without waking anyone, so a dispatcher thread's loop
(`while (XTaskQueueDispatch(queue, port, INFINITE));`, which ends by
terminating the queue) stayed in its wait for good, and so did whoever
joined that thread. A dispatch begun after the termination did the same.

A dispatch now also ends, returning false, when its queue is
terminated: the termination wakes the port's waiters after draining it.
The flag is the queue's, so a dispatch on another queue that shares the
port waits on.

Co-Authored-By: Claude Fable 5.2 <[email protected]>
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 03:56:03 +00:00
github manually merged commit 36d6a79595 into gh-pr/239/base 2026-11-26 03:56:05 +00:00
Author
Owner

@⁠jeremygold02 on GitHub · 2026-11-24 05:03 UTC · original

Thanks @⁠Maraco91 for handling a dispatch that waits indefinitely on a manual queue. I tested the open PR at head 36d6a79595ff9c24930f7c881bd34d7fd56e425a with:

cargo test --locked -p victory-hle --lib terminate_ends_a_waiting_dispatch -- --test-threads=1

Result: 1 passed, 0 failed (86 filtered out). The regression test starts a dispatcher blocked on queue A, terminates a composite queue B that shares A’s manual ports and verifies A continues dispatching work, then terminates A and verifies its blocked dispatcher exits. It also checks a dispatch started after termination returns immediately. This is a synthetic HLE unit test; it does not exercise a real title or host thread workload.

Code: xasync.rs.

**@⁠jeremygold02** on GitHub · 2026-11-24 05:03 UTC · [original](https://github.com/Marvisak/VIctory/pull/239#issuecomment-7122590810) Thanks @⁠Maraco91 for handling a dispatch that waits indefinitely on a manual queue. I tested the open PR at head `36d6a79595ff9c24930f7c881bd34d7fd56e425a` with: ``` cargo test --locked -p victory-hle --lib terminate_ends_a_waiting_dispatch -- --test-threads=1 ``` Result: 1 passed, 0 failed (86 filtered out). The regression test starts a dispatcher blocked on queue A, terminates a composite queue B that shares A’s manual ports and verifies A continues dispatching work, then terminates A and verifies its blocked dispatcher exits. It also checks a dispatch started after termination returns immediately. This is a synthetic HLE unit test; it does not exercise a real title or host thread workload. Code: [xasync.rs](https://github.com/Marvisak/VIctory/blob/36d6a79595ff9c24930f7c881bd34d7fd56e425a/crates/victory-hle/src/modules/xasync.rs).
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!239
No description provided.