XAsync: cleanup waits for provider calls under way, Terminate waits and cancels only its own, pool and timer threads bounded (port of VIctory-7900XTX #244 + #466) #898

Manually merged
github merged 2 commits from github-bot/gh-898 into main 2026-11-29 08:22:14 +00:00 AGit
Owner

@⁠illuminatedfellow on GitHub · 2026-11-28 08:46 UTC · original
illuminatedfellow:port-228 → main · +688 −56 · 1 files · 2 commits · merged 2026-11-29 08:16 UTC

Ports the fixes for avatarmanz/VIctory-7900XTX#228 (XAsync races and thread use), as two commits:

  1. sharan104's avatarmanz/VIctory-7900XTX#244 (open there), cherry-picked with their authorship; the commit message is theirs, with its issue reference pointed at that repo.
    • A cancel or complete on one thread could release a call while its provider's DoWork or Cancel still ran on another, so the provider's cleanup freed its context under them. Each call now counts the provider operations under way, and cleanup runs after the last one returns.
    • XTaskQueueTerminate called its callback straight away and ignored wait; callbacks already running or queued in the pool still ran after it, and draining a shared port canceled other queues' callbacks. Terminate now cancels only its own queue's tasks and completes when its running callbacks have returned.
  2. avatarmanz/VIctory-7900XTX#466 on top (mine): idle pool workers end after 30 s; a burst starts workers up to twice the core count, past which one more starts only when no worker has taken a task for 50 ms (so a callback that waits on another still never starves); delayed callbacks wait on one timer thread instead of a thread each.

The cherry-pick applied cleanly on top of 4902d45 (pending_with completes the call it began); I read the place where the two meet: complete_call only releases a call that is still in flight, and #244's counting wraps the provider operations under it.

Checked on this fork (xasync.rs built on Linux with a stub for xgameruntime, since victory-hle needs Windows):

  • #244's four tests (cleanup_waits_for_work_under_way, terminate_completes_once_running_callbacks_return, terminate_leaves_other_queues_on_the_port_alone, terminate_waiting_from_its_own_callback_returns) fail on this main and pass here.
  • All 22 xasync tests pass; 60 runs in release and 12 under CPU load without a failure; clippy --all-targets -D warnings clean on Rust 1.97.
  • delayed_callbacks_do_not_start_a_thread_each shows the thread use: 200 delayed callbacks started 200 threads before.

Not built on Windows or with 1.98.1, and not run with the title.

**@⁠illuminatedfellow** on GitHub · 2026-11-28 08:46 UTC · [original](https://github.com/trustno1-coder/VIctory-NVIDIA/pull/898) `illuminatedfellow:port-228` → `main` · +688 −56 · 1 files · 2 commits · merged 2026-11-29 08:16 UTC Ports the fixes for avatarmanz/VIctory-7900XTX#228 (XAsync races and thread use), as two commits: 1. **sharan104's avatarmanz/VIctory-7900XTX#244** (open there), cherry-picked with their authorship; the commit message is theirs, with its issue reference pointed at that repo. - A cancel or complete on one thread could release a call while its provider's `DoWork` or `Cancel` still ran on another, so the provider's cleanup freed its context under them. Each call now counts the provider operations under way, and cleanup runs after the last one returns. - `XTaskQueueTerminate` called its callback straight away and ignored `wait`; callbacks already running or queued in the pool still ran after it, and draining a shared port canceled other queues' callbacks. Terminate now cancels only its own queue's tasks and completes when its running callbacks have returned. 2. **avatarmanz/VIctory-7900XTX#466** on top (mine): idle pool workers end after 30 s; a burst starts workers up to twice the core count, past which one more starts only when no worker has taken a task for 50 ms (so a callback that waits on another still never starves); delayed callbacks wait on one timer thread instead of a thread each. The cherry-pick applied cleanly on top of 4902d45 (`pending_with` completes the call it began); I read the place where the two meet: `complete_call` only releases a call that is still in flight, and #244's counting wraps the provider operations under it. **Checked on this fork** (`xasync.rs` built on Linux with a stub for `xgameruntime`, since `victory-hle` needs Windows): - #244's four tests (`cleanup_waits_for_work_under_way`, `terminate_completes_once_running_callbacks_return`, `terminate_leaves_other_queues_on_the_port_alone`, `terminate_waiting_from_its_own_callback_returns`) **fail on this main** and pass here. - All 22 xasync tests pass; 60 runs in release and 12 under CPU load without a failure; `clippy --all-targets -D warnings` clean on Rust 1.97. - `delayed_callbacks_do_not_start_a_thread_each` shows the thread use: 200 delayed callbacks started 200 threads before. Not built on Windows or with 1.98.1, and not run with the title.
A cancel or complete on one thread could release a call while its
provider's DoWork or Cancel still ran on another: the provider's cleanup,
which frees its context, ran under them. Each call now counts the provider
operations under way (begin, DoWork, cancel, get result). Release marks the
call and cleans up at once if none runs, otherwise the last one to return
does; no operation starts once the call is released, so cleanup runs once
and last.

XTaskQueueTerminate called its callback straight away and ignored `wait`.
Callbacks already running, or waiting in the shared thread pool, still ran
after it, and draining a port canceled the callbacks of every composite
queue sharing it. Tasks now carry the queue they were submitted to, which
counts them until they have run, live or canceled. Terminate sets its flag
under that count's lock (a delivery checks it there, so a task either
reaches its port before the drain or is refused), cancels only its own
queue's tasks from the ports, the pool and the delay list, and completes
when the count reaches zero: it waits there when `wait` is set, otherwise
the last callback to return calls the termination callback. A delayed
callback is counted from submission, so one whose delay ran out just
before termination is canceled before termination completes.

Waiting from one of the queue's own callbacks would wait for the caller,
where before `wait` was ignored. Each thread keeps a stack of the queues
whose callbacks it runs (an immediate port nests them); Terminate called
from inside one of its queue's callbacks does not wait, and the callback's
end completes the termination.

Tests: cleanup waits for DoWork under a concurrent cancel; Terminate, with
and without wait, completes only after a running callback returns; a
composite queue's termination leaves the other queue's callbacks queued;
a callback terminating its own queue with wait returns, and termination
completes after it.

Not addressed: worker threads are still uncapped, and complete_call still
runs an immediate cleanup under the call's lock.

Part of avatarmanz/VIctory-7900XTX#228

Co-Authored-By: Claude Opus 5.5 <[email protected]>
(cherry picked from commit 0572cc50bb217d43d23e0d0da388e2a70845a932)
On top of sharan104's fix for the two races (avatarmanz/VIctory-7900XTX#244, the commit before this). This is item 3:

- 3.1: a pool worker idle for 30 s now ends. Before, every worker a burst
  started stayed for the life of the process.
- 3.2: delayed callbacks (XTaskQueueSubmitDelayedCallback, XAsyncSchedule
  with a delay) wait on one timer thread and are delivered on the pool.
  Before, each one started its own thread that slept out the delay.
- 3.3: a burst starts workers up to twice the core count (at least 4).
  Past that, tasks wait for a worker, unless no worker has taken a task
  for 50 ms: then they may all be blocked on callbacks still queued, so
  the pool starts one more (and checks again 50 ms later). This is option
  (a) from that issue: a callback that waits on another still never starves.

Termination still takes a queue's callbacks back out of the pool: the
pool keeps them apart from its own jobs.

Tests: idle_pool_workers_end, a_burst_starts_no_more_workers_than_the_cap,
a_stalled_pool_grows_past_its_cap, delayed_callbacks_do_not_start_a_thread_each.
The last one fails with only the commit before (200 threads for 200 delayed callbacks).
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-28 14:29:46 +00:00
Author
Owner

@⁠illuminatedfellow on GitHub · 2026-11-28 17:28 UTC · original

The failing check is main's Clippy, not this change: it stops on victory-shader/src/emit/mod.rs:2768 (let_and_return) and victory-gpu/src/shadow.rs:1708 (manual_is_multiple_of), both already on main, which #86 fixes. This PR only touches victory-hle/src/modules/xasync.rs, which Clippy didn't reach.

**@⁠illuminatedfellow** on GitHub · 2026-11-28 17:28 UTC · [original](https://github.com/trustno1-coder/VIctory-NVIDIA/pull/898#issuecomment-7243040646) The failing check is `main`'s Clippy, not this change: it stops on `victory-shader/src/emit/mod.rs:2768` (`let_and_return`) and `victory-gpu/src/shadow.rs:1708` (`manual_is_multiple_of`), both already on `main`, which #86 fixes. This PR only touches `victory-hle/src/modules/xasync.rs`, which Clippy didn't reach.
Author
Owner

@⁠illuminatedfellow on GitHub · 2026-11-28 18:04 UTC · original

Heads-up: #1205 fixes another race in the same file and merges with this one without a conflict, but the two clash in complete_call: see my comment there. If #1205 goes in first, I'll update this branch with the one-line fix.

**@⁠illuminatedfellow** on GitHub · 2026-11-28 18:04 UTC · [original](https://github.com/trustno1-coder/VIctory-NVIDIA/pull/898#issuecomment-7243509073) Heads-up: #1205 fixes another race in the same file and merges with this one without a conflict, but the two clash in `complete_call`: see my comment there. If #1205 goes in first, I'll update this branch with the one-line fix.
github manually merged commit 834fed1f55 into main 2026-11-29 08:22:14 +00:00
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/trustno1-coder--VIctory-NVIDIA!898
No description provided.