HLE/xasync: wait for running tasks, cancel delayed timers, and scope composite queue termination #584

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

@⁠AscilCH on GitHub · 2026-11-25 02:35 UTC · original
AscilCH:fix/hle-xtaskqueue-terminate-wait-timers-and-composite → main · +338 −41 · 1 files · 1 commits · open

Problem

Three bugs in XTaskQueue termination and delayed-callback handling (crates/victory-hle/src/modules/xasync.rs):

  1. Post-termination delayed-callback UAF: Queue::submit with delay_ms > 0 spawned a thread running thread::sleep(delay) followed by unconditional queue.deliver(port, task) without checking terminated. XTaskQueueTerminate returned immediately while delayed timers remained armed; when they fired after the title freed callback/monitor contexts, they invoked title callbacks and queue monitors on freed memory.
  2. Ignored wait parameter in XTaskQueueTerminate: _wait: bool was ignored, so XTaskQueueTerminate(q, true, ...) invoked terminated_callback and returned while ThreadPool / Serialized / Manual callbacks for q were still executing (UAF when the title destroys contexts right after XTaskQueueTerminate(q, true, ...) returns).
  3. Cross-queue cancellation on composite ports: XTaskQueueTerminate ran lock(&port.tasks).drain(..) on each port without checking task ownership, canceling tasks belonging to sibling open queues sharing a port created via XTaskQueueGetPort + XTaskQueueCreateComposite.

Fix

  • Tag queued tasks with their owning queue (Weak<Queue> + queue_id) and drain only matching queue_id entries from ports and POOL in XTaskQueueTerminate.
  • Track pending delayed callbacks in QueueState::delayed and sleep on a per-queue Condvar, so termination wakes timer threads immediately and cancels all pending delayed tasks synchronously before returning.
  • Maintain a per-queue running: AtomicUsize counter (incremented while holding the port/pool lock, decremented in RunningTask::drop with a done Condvar) so wait == true blocks until all in-flight callbacks finish before calling terminated_callback.
  • Add synthetic unit test terminate_cancels_delayed_waits_for_running_and_preserves_composite_siblings.

Verification

  • cargo test -p victory-hle --locked xasync::tests: 8 passed, 0 failed
  • cargo xtask policy: 169 files ok
  • cargo clippy -p victory-hle --no-deps --all-targets --locked -- -D warnings: clean
  • cargo clippy -p victory-hle --all-targets --all-features --locked -- -D warnings: clean

(AI-assisted; synthetic unit tests only, no game data.)

**@⁠AscilCH** on GitHub · 2026-11-25 02:35 UTC · [original](https://github.com/fxceless/victory-fork/pull/584) `AscilCH:fix/hle-xtaskqueue-terminate-wait-timers-and-composite` → `main` · +338 −41 · 1 files · 1 commits · open ## Problem Three bugs in `XTaskQueue` termination and delayed-callback handling (`crates/victory-hle/src/modules/xasync.rs`): 1. **Post-termination delayed-callback UAF**: `Queue::submit` with `delay_ms > 0` spawned a thread running `thread::sleep(delay)` followed by unconditional `queue.deliver(port, task)` without checking `terminated`. `XTaskQueueTerminate` returned immediately while delayed timers remained armed; when they fired after the title freed callback/monitor contexts, they invoked title callbacks and queue monitors on freed memory. 2. **Ignored `wait` parameter in `XTaskQueueTerminate`**: `_wait: bool` was ignored, so `XTaskQueueTerminate(q, true, ...)` invoked `terminated_callback` and returned while `ThreadPool` / `Serialized` / `Manual` callbacks for `q` were still executing (UAF when the title destroys contexts right after `XTaskQueueTerminate(q, true, ...)` returns). 3. **Cross-queue cancellation on composite ports**: `XTaskQueueTerminate` ran `lock(&port.tasks).drain(..)` on each port without checking task ownership, canceling tasks belonging to sibling open queues sharing a port created via `XTaskQueueGetPort` + `XTaskQueueCreateComposite`. ## Fix - Tag queued tasks with their owning queue (`Weak<Queue>` + `queue_id`) and drain only matching `queue_id` entries from ports and `POOL` in `XTaskQueueTerminate`. - Track pending delayed callbacks in `QueueState::delayed` and sleep on a per-queue `Condvar`, so termination wakes timer threads immediately and cancels all pending delayed tasks synchronously before returning. - Maintain a per-queue `running: AtomicUsize` counter (incremented while holding the port/pool lock, decremented in `RunningTask::drop` with a `done` `Condvar`) so `wait == true` blocks until all in-flight callbacks finish before calling `terminated_callback`. - Add synthetic unit test `terminate_cancels_delayed_waits_for_running_and_preserves_composite_siblings`. ## Verification - `cargo test -p victory-hle --locked xasync::tests`: 8 passed, 0 failed - `cargo xtask policy`: 169 files ok - `cargo clippy -p victory-hle --no-deps --all-targets --locked -- -D warnings`: clean - `cargo clippy -p victory-hle --all-targets --all-features --locked -- -D warnings`: clean *(AI-assisted; synthetic unit tests only, no game data.)*
- Tag queued tasks with their owning queue (`Weak<Queue>` + `queue_id`) so
  `XTaskQueueTerminate` drains and cancels only tasks belonging to the
  terminated queue on shared composite ports (and in the thread pool), leaving
  sibling queues' queued work intact.
- Track pending delayed callbacks under `Queue::state` and wait on a `Condvar`
  instead of `thread::sleep`, so `XTaskQueueTerminate` wakes sleeping timer
  threads immediately, cancels every pending delayed callback before returning,
  and guarantees no delayed callback can deliver or invoke monitors after
  termination.
- Track in-flight callbacks per queue via `Queue::running` (incremented before
  releasing the port/pool lock and decremented in `RunningTask::drop`) so
  `XTaskQueueTerminate(_, true, ..)` blocks until all running callbacks of
  that queue finish before invoking `terminated_callback` and returning.
- Add synthetic unit test
  `terminate_cancels_delayed_waits_for_running_and_preserves_composite_siblings`.
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 03:46:50 +00:00
This pull request can be merged automatically.
This branch is out-of-date with the base branch
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/584/head:github-bot/gh-584'
git switch 'github-bot/gh-584'

Merge

Merge the changes and update on Forgejo.
git switch 'main'
git merge --no-ff 'github-bot/gh-584'
git switch 'github-bot/gh-584'
git rebase 'main'
git switch 'main'
git merge --ff-only 'github-bot/gh-584'
git switch 'github-bot/gh-584'
git rebase 'main'
git switch 'main'
git merge --no-ff 'github-bot/gh-584'
git switch 'main'
git merge --squash 'github-bot/gh-584'
git switch 'main'
git merge --ff-only 'github-bot/gh-584'
git switch 'main'
git merge 'github-bot/gh-584'
git push origin 'main'
This discussion has been locked. Commenting is limited to contributors.
No description provided.