hle(xasync): wait for provider calls on cleanup, scope queue termination, and bound worker pool with shared timer #162

Closed
github-bot wants to merge 1 commit from gh-pr/162/head into gh-pr/162/base
Owner

@⁠Androtim on GitHub · 2026-11-29 20:31 UTC · original
Androtim:feat/iter160-xasync-provider-cleanup-bounded-pool → main · +689 −57 · 1 files · 1 commits · closed without merging

## Summary

  • Wait for in-flight provider operations before cleanup (crates/victory-hle/src/modules/xasync.rs):
    • Add per-Call busy: Mutex<(usize, bool)> tracking (Call::run / Call::cleanup) so OP_CLEANUP is deferred until any concurrent OP_DO_WORK, OP_CANCEL, or OP_GET_RESULT returns, preventing context UAF under concurrent cancel/completion while preserving State::Completing / State::Done(HRESULT) publication before removing completed calls from CALLS.
  • Scoped queue termination & callback tracking:
    • Associate queued tasks with their originating Queue (weak reference) and track outstanding callbacks from submission until completion/cancellation; XTaskQueueTerminate cancels only that queue's own callbacks across ports, the pool, and delayed timers, respects wait, avoids self-deadlock when called from one of its own callbacks, and invokes the termination callback asynchronously when wait is false.
  • Bounded worker pool & shared delayed timer:
    • Exit idle pool workers after 30 s, replace per-delay sleeping threads with one shared timer thread, and cap worker bursts at 2 * cores (min 4) with a 50 ms stall detector so blocked callbacks can still grow the pool without starvation.

Test plan

  • cargo run -p xtask -- policy: passed
  • cargo test -p victory-hle xasync::: passed
  • Dual cargo clippy --workspace --all-targets (default and --features diagnostics) with -D warnings: passed
**@⁠Androtim** on GitHub · 2026-11-29 20:31 UTC · [original](https://github.com/Androtim/MiniVI/pull/162) `Androtim:feat/iter160-xasync-provider-cleanup-bounded-pool` → `main` · +689 −57 · 1 files · 1 commits · closed without merging ## Summary - **Wait for in-flight provider operations before cleanup (`crates/victory-hle/src/modules/xasync.rs`)**: - Add per-`Call` `busy: Mutex<(usize, bool)>` tracking (`Call::run` / `Call::cleanup`) so `OP_CLEANUP` is deferred until any concurrent `OP_DO_WORK`, `OP_CANCEL`, or `OP_GET_RESULT` returns, preventing context UAF under concurrent cancel/completion while preserving `State::Completing` / `State::Done(HRESULT)` publication before removing completed calls from `CALLS`. - **Scoped queue termination & callback tracking**: - Associate queued tasks with their originating `Queue` (weak reference) and track outstanding callbacks from submission until completion/cancellation; `XTaskQueueTerminate` cancels only that queue's own callbacks across ports, the pool, and delayed timers, respects `wait`, avoids self-deadlock when called from one of its own callbacks, and invokes the termination callback asynchronously when `wait` is false. - **Bounded worker pool & shared delayed timer**: - Exit idle pool workers after 30 s, replace per-delay sleeping threads with one shared timer thread, and cap worker bursts at `2 * cores` (min 4) with a 50 ms stall detector so blocked callbacks can still grow the pool without starvation. ## Test plan - `cargo run -p xtask -- policy`: passed - `cargo test -p victory-hle xasync::`: passed - Dual `cargo clippy --workspace --all-targets` (default and `--features diagnostics`) with `-D warnings`: passed
github-bot locked as GitHub sync and limited conversation to collaborators 2026-12-01 20:14:25 +00:00
github-bot closed this pull request 2026-12-01 20:14:25 +00:00
Author
Owner

@⁠Androtim on GitHub · 2026-11-29 21:04 UTC · original · edited 2026-11-29 22:05 UTC

Automated triage for the maintainers (not an approval).

Verdict: needs-work · CI (check): passing

Review Summary

The provider cleanup synchronization (Call::run / Call::cleanup), scoped queue termination (take_tasks / Pool::take_tasks), bounded worker pool with stall detection (Pool), and shared Timer thread in crates/victory-hle/src/modules/xasync.rs are well-structured and avoid lock-order inversions (queue.delayed -> queue.running -> port.tasks / POOL.state -> TIMER.state).

However, there are two concrete bugs in the new asynchronous XTaskQueueTerminate completion path:

  1. Async termination callback is silently dropped if the queue handle is closed before running callbacks finish:

    • Task::queue is a Weak<Queue> (to avoid cycles with undrained Manual ports), and Queue::running stores Option<(usize, usize)> without holding a strong Arc<Queue>.
    • If a title calls XTaskQueueTerminate(q, false, ctx, Some(cb)) while callbacks are still running (running.0 > 0) and then immediately calls XTaskQueueCloseHandle(q) (standard GDK async teardown), Arc::decrement_strong_count drops Queue before the in-flight callbacks return. When those callbacks finish, Task::run gets None from self.queue.upgrade(), Queue::ran never runs, and cb is never invoked.
    • Because XTaskQueueTerminate has already drained all queued/delayed tasks before storing running.1, retaining a strong Arc<Queue> alongside the pending termination callback in running.1 (released when Queue::ran takes it at running.0 == 0) keeps Queue alive without leaking.
  2. Repeated XTaskQueueTerminate overwrites a pending termination callback:

    • In XTaskQueueTerminate, if running.0 > 0 { running.1 = callback.map(...); } unconditionally overwrites running.1. Calling XTaskQueueTerminate(q, false, null, None) (or with a second callback) while callbacks are still draining clobbers and drops a previously registered termination callback.

To change the verdict:

  • Keep Queue alive (e.g., store Arc<Queue> with the pending termination callback in Queue::running) until Queue::ran invokes the callback, and add a unit test covering XTaskQueueTerminate(q, false, ctx, Some(cb)) followed immediately by XTaskQueueCloseHandle(q) while a callback is in flight.
  • Avoid clobbering an already-queued termination callback on repeated XTaskQueueTerminate calls (e.g., store pending termination callbacks in a Vec or preserve existing callbacks when callback is None).

Generated with Antigravity CLI (agy).

**@⁠Androtim** on GitHub · 2026-11-29 21:04 UTC · [original](https://github.com/Androtim/MiniVI/pull/162#issuecomment-7262579863) · edited 2026-11-29 22:05 UTC <!-- minivi-triage v1 head=ceb12a3f3392422037bd5707c9f6819e62b28bf0 verdict=needs-work ci=passing at=2026-11-29T22:06:16Z --> Automated triage for the maintainers (not an approval). **Verdict:** `needs-work` · **CI (`check`):** `passing` ### Review Summary The provider cleanup synchronization (`Call::run` / `Call::cleanup`), scoped queue termination (`take_tasks` / `Pool::take_tasks`), bounded worker pool with stall detection (`Pool`), and shared `Timer` thread in `crates/victory-hle/src/modules/xasync.rs` are well-structured and avoid lock-order inversions (`queue.delayed` -> `queue.running` -> `port.tasks` / `POOL.state` -> `TIMER.state`). However, there are two concrete bugs in the new asynchronous `XTaskQueueTerminate` completion path: 1. **Async termination callback is silently dropped if the queue handle is closed before running callbacks finish**: - `Task::queue` is a `Weak<Queue>` (to avoid cycles with undrained `Manual` ports), and `Queue::running` stores `Option<(usize, usize)>` without holding a strong `Arc<Queue>`. - If a title calls `XTaskQueueTerminate(q, false, ctx, Some(cb))` while callbacks are still running (`running.0 > 0`) and then immediately calls `XTaskQueueCloseHandle(q)` (standard GDK async teardown), `Arc::decrement_strong_count` drops `Queue` before the in-flight callbacks return. When those callbacks finish, `Task::run` gets `None` from `self.queue.upgrade()`, `Queue::ran` never runs, and `cb` is never invoked. - Because `XTaskQueueTerminate` has already drained all queued/delayed tasks before storing `running.1`, retaining a strong `Arc<Queue>` alongside the pending termination callback in `running.1` (released when `Queue::ran` takes it at `running.0 == 0`) keeps `Queue` alive without leaking. 2. **Repeated `XTaskQueueTerminate` overwrites a pending termination callback**: - In `XTaskQueueTerminate`, `if running.0 > 0 { running.1 = callback.map(...); }` unconditionally overwrites `running.1`. Calling `XTaskQueueTerminate(q, false, null, None)` (or with a second callback) while callbacks are still draining clobbers and drops a previously registered termination callback. ### To change the verdict: - Keep `Queue` alive (e.g., store `Arc<Queue>` with the pending termination callback in `Queue::running`) until `Queue::ran` invokes the callback, and add a unit test covering `XTaskQueueTerminate(q, false, ctx, Some(cb))` followed immediately by `XTaskQueueCloseHandle(q)` while a callback is in flight. - Avoid clobbering an already-queued termination callback on repeated `XTaskQueueTerminate` calls (e.g., store pending termination callbacks in a `Vec` or preserve existing callbacks when `callback` is `None`). --- _Generated with Antigravity CLI (`agy`)._
Author
Owner

@⁠Androtim on GitHub · 2026-12-01 01:38 UTC · original

Closing stale/conflicted backlog PR per #276. Superseded by the active root-architecture replacement pillars with isolated plain --release A/B verification.

**@⁠Androtim** on GitHub · 2026-12-01 01:38 UTC · [original](https://github.com/Androtim/MiniVI/pull/162#issuecomment-7297731970) Closing stale/conflicted backlog PR per #276. Superseded by the active root-architecture replacement pillars with isolated plain --release A/B verification.

Pull request closed

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/Androtim--MiniVI!162
No description provided.