xasync: cancel and queue-termination races (cleanup while DoWork runs, XTaskQueueTerminate ignores wait, threads never capped) #228

Open
opened 2026-11-26 12:04:49 +00:00 by github-bot · 2 comments
Owner

@⁠kekonn on GitHub · 2026-11-26 12:04 UTC · original

These are confirmed, but each fix is larger than a quick change. The narrow pending_with use-after-free is issue 1.

All in crates/victory-hle/src/modules/xasync.rs:

  • Cleanup while the provider runs. XAsyncCancel (726-734) checks done, calls OP_CANCEL, then complete_call(E_ABORT) → release → OP_CLEANUP, without waiting for a DoWork already running on the pool (~619-627) or a provider completing at the same moment. A title provider that frees its context in OP_CLEANUP (the usual GDK pattern) can have it freed under a running DoWork. Sketch (~35 lines, medium risk): add an in-flight count to Call, with enter()/leave() around DoWork and OP_CANCEL, and defer OP_CLEANUP until the count reaches 0. (The XAsyncRun variant, which reports E_ABORT while the title's work still runs, needs more than this.)
  • XTaskQueueTerminate. (393-421) ignores wait and fires the terminated callback straight away. Tasks already in the global POOL (~122), or taken from delayed (~240), still run after termination. drain(..) (~404) also cancels tasks of other composite queues that share the Arc<Port> (~325-332).
  • Threads never capped. A burst spawns about one worker per task (~168-169: workers just spawned aren't counted as idle), and they never exit. Each delayed callback gets its own OS thread holding an Arc<Queue> for the whole delay (~237). Every spawn runs the title's DllMains (victory-loader threads.rs ~252).
  • (low) Lock held across cleanup. complete_call holds call.done while release() runs OP_CLEANUP (673-689). A three-state done (PENDING, COMPLETING, DONE) would move cleanup outside the lock. A deadlock needs a title cleanup that blocks on a thread already inside cancel for the same call.

Upstream: no open PR touches xasync.rs.


Generated by Claude Code

**@⁠kekonn** on GitHub · 2026-11-26 12:04 UTC · [original](https://github.com/avatarmanz/VIctory-7900XTX/issues/228) These are confirmed, but each fix is larger than a quick change. The narrow `pending_with` use-after-free is issue 1. All in `crates/victory-hle/src/modules/xasync.rs`: - [ ] **Cleanup while the provider runs.** `XAsyncCancel` (726-734) checks `done`, calls `OP_CANCEL`, then `complete_call(E_ABORT)` → `release` → `OP_CLEANUP`, without waiting for a `DoWork` already running on the pool (~619-627) or a provider completing at the same moment. A title provider that frees its context in `OP_CLEANUP` (the usual GDK pattern) can have it freed under a running `DoWork`. Sketch (~35 lines, medium risk): add an in-flight count to `Call`, with `enter()`/`leave()` around `DoWork` and `OP_CANCEL`, and defer `OP_CLEANUP` until the count reaches 0. (The `XAsyncRun` variant, which reports `E_ABORT` while the title's work still runs, needs more than this.) - [ ] **XTaskQueueTerminate.** (393-421) ignores `wait` and fires the terminated callback straight away. Tasks already in the global `POOL` (~122), or taken from `delayed` (~240), still run after termination. `drain(..)` (~404) also cancels tasks of *other* composite queues that share the `Arc<Port>` (~325-332). - [ ] **Threads never capped.** A burst spawns about one worker per task (~168-169: workers just spawned aren't counted as idle), and they never exit. Each delayed callback gets its own OS thread holding an `Arc<Queue>` for the whole delay (~237). Every spawn runs the title's DllMains (`victory-loader` `threads.rs` ~252). - [ ] *(low)* **Lock held across cleanup.** `complete_call` holds `call.done` while `release()` runs `OP_CLEANUP` (673-689). A three-state `done` (PENDING, COMPLETING, DONE) would move cleanup outside the lock. A deadlock needs a title cleanup that blocks on a thread already inside cancel for the same call. **Upstream:** no open PR touches `xasync.rs`. --- _Generated by [Claude Code](https://claude.ai/code)_
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 12:04:49 +00:00
Author
Owner

@⁠illuminatedfellow on GitHub · 2026-11-26 16:41 UTC · original

Status of the four items, and a proposal for the one that needs a policy decision.

Covered: items 1 (cleanup under a running DoWork) and 2 (XTaskQueueTerminate) are in #244. Its commit message says what it leaves: "worker threads are still uncapped, and complete_call still runs an immediate cleanup under the call's lock" — i.e. items 3 and 4. Both live in code #244 rewrites, so anything for them should build on #244.

Item 3 is three separate things:

  1. Idle workers never exit. Mechanical: wait on ready with a timeout (say 30 s) and exit if still idle and more than a small floor (say 4) are left. Every exit also runs the title's DLL_THREAD_DETACH, so keeping a floor matters.

  2. One OS thread per delayed callback. Mechanical: one timer thread with a BinaryHeap of (deadline, id, Arc<Queue>, port) and wait_timeout until the earliest deadline. #244's semantics carry over unchanged: the task stays in queue.delayed and counted in running from submission, the timer only does what each sleeping thread does now (remove(&id) then deliver), so termination still cancels it.

  3. A burst starts ~one worker per task. This one is not a counting slip: counting workers that are starting changes nothing (tasks > idle + starting still spawns one per queued task when submissions outpace startup). One-thread-per-waiting-task is what guarantees "a callback that blocks on another never starves it". Capping needs a policy, e.g.:

    • (a) cap + starvation escape: spawn freely up to N (e.g. 2× cores), past N only when no task has started for ~50 ms while tasks wait — the classic thread-pool injection rule; or
    • (b) hard cap with a logged warning — simpler, but a title whose callbacks wait on each other can deadlock past N.

    (a) keeps the no-starvation property; (b) does not. Which would you accept?

Item 4 (cleanup under the call's lock) interacts with #244's new release counting, so it's best done after #244 lands.

Happy to send 3.1 + 3.2 as a PR stacked on #244 (with tests of the pool and timer on their own), and 3.3 once a policy is picked.

(Written with Claude Opus 5.5.)

**@⁠illuminatedfellow** on GitHub · 2026-11-26 16:41 UTC · [original](https://github.com/avatarmanz/VIctory-7900XTX/issues/228#issuecomment-7197976998) Status of the four items, and a proposal for the one that needs a policy decision. **Covered:** items 1 (cleanup under a running DoWork) and 2 (`XTaskQueueTerminate`) are in #244. Its commit message says what it leaves: *"worker threads are still uncapped, and complete_call still runs an immediate cleanup under the call's lock"* — i.e. items 3 and 4. Both live in code #244 rewrites, so anything for them should build on #244. **Item 3 is three separate things:** 1. *Idle workers never exit.* Mechanical: wait on `ready` with a timeout (say 30 s) and exit if still idle and more than a small floor (say 4) are left. Every exit also runs the title's `DLL_THREAD_DETACH`, so keeping a floor matters. 2. *One OS thread per delayed callback.* Mechanical: one timer thread with a `BinaryHeap` of `(deadline, id, Arc<Queue>, port)` and `wait_timeout` until the earliest deadline. #244's semantics carry over unchanged: the task stays in `queue.delayed` and counted in `running` from submission, the timer only does what each sleeping thread does now (`remove(&id)` then `deliver`), so termination still cancels it. 3. *A burst starts ~one worker per task.* This one is not a counting slip: counting workers that are starting changes nothing (`tasks > idle + starting` still spawns one per queued task when submissions outpace startup). One-thread-per-waiting-task is what guarantees *"a callback that blocks on another never starves it"*. Capping needs a policy, e.g.: - **(a) cap + starvation escape:** spawn freely up to N (e.g. 2× cores), past N only when no task has started for ~50 ms while tasks wait — the classic thread-pool injection rule; or - **(b) hard cap** with a logged warning — simpler, but a title whose callbacks wait on each other can deadlock past N. (a) keeps the no-starvation property; (b) does not. Which would you accept? **Item 4** (cleanup under the call's lock) interacts with #244's new release counting, so it's best done after #244 lands. Happy to send 3.1 + 3.2 as a PR stacked on #244 (with tests of the pool and timer on their own), and 3.3 once a policy is picked. (Written with Claude Opus 5.5.)
Author
Owner

@⁠kekonn on GitHub · 2026-11-26 20:38 UTC · original

Go ahead. I just recorded these because I spent a 5 hour limit worth of Opus on discovering all these.

**@⁠kekonn** on GitHub · 2026-11-26 20:38 UTC · [original](https://github.com/avatarmanz/VIctory-7900XTX/issues/228#issuecomment-7202927619) Go ahead. I just recorded these because I spent a 5 hour limit worth of Opus on discovering all these.
This discussion has been locked. Commenting is limited to contributors.
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/avatarmanz--VIctory-7900XTX#228
No description provided.