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

Merged
Maraco91 merged 1 commit from upstream-pr/239 into main 2026-11-24 07:57:52 +00:00
Maraco91 commented 2026-11-24 01:43:08 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by Maraco91 on 2026-11-24T01:43:08Z.


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

<!-- greyfinch-sync:Marvisak/VIctory:issue:6572684300 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/239). Originally posted by [Maraco91](https://github.com/Maraco91) on 2026-11-24T01:43:08Z. --- ## 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)
jeremygold02 commented 2026-11-24 06:55:56 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by jeremygold02 on 2026-11-24T05:03:55Z.


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.

<!-- greyfinch-sync:Marvisak/VIctory:comment:7122590810 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/239#issuecomment-7122590810). Originally posted by [jeremygold02](https://github.com/jeremygold02) on 2026-11-24T05:03:55Z. --- 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).
upstream-sync closed this pull request 2026-11-24 08:01:58 +00:00
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!239
No description provided.