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

Open
AscilCH wants to merge 1 commit from upstream-pr/584 into main
AscilCH commented 2026-11-25 02:35:32 +00:00 (Migrated from github.com)

Imported from fxceless/victory-fork. Originally posted by AscilCH on 2026-11-25T02:35:32Z.


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.)

<!-- greyfinch-sync:fxceless/victory-fork:issue:6595879398 --> Imported from [fxceless/victory-fork](https://github.com/fxceless/victory-fork/pull/584). Originally posted by [AscilCH](https://github.com/AscilCH) on 2026-11-25T02:35:32Z. --- ## 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.)*
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/584:upstream-pr/584'
git switch 'upstream-pr/584'

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/584'
git switch 'upstream-pr/584'
git rebase 'main'
git switch 'main'
git merge --ff-only 'upstream-pr/584'
git switch 'upstream-pr/584'
git rebase 'main'
git switch 'main'
git merge --no-ff 'upstream-pr/584'
git switch 'main'
git merge --squash 'upstream-pr/584'
git switch 'main'
git merge --ff-only 'upstream-pr/584'
git switch 'main'
git merge 'upstream-pr/584'
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
fxceless/victory-fork!584
No description provided.