XAsync: cleanup waits for provider calls under way; XTaskQueueTerminate honours wait and cancels only its own (part of #228) #244

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

@⁠sharan104 on GitHub · 2026-11-26 12:32 UTC · original
sharan104:fix/xasync-cancel-races → main · +366 −26 · 1 files · 1 commits · open

Part of #228. It fixes the two race items in the issue and leaves the two design items open (listed below).

It builds on main and doesn't overlap #229's #214 fix (db23fd1, pending_with). git merge-tree combines the two without conflicts.

Cleanup while DoWork / Cancel runs. A cancel or complete on one thread could release a call while its provider's DoWork or Cancel was still running on another. The provider's cleanup, which frees its context, then ran underneath them. Now:

  • Each call counts the provider operations under way (begin, DoWork, cancel, get result).
  • release marks the call. If nothing is running, it cleans up at once; otherwise the last operation to return runs the cleanup.
  • No operation starts after release, so cleanup runs once, and last.

XTaskQueueTerminate ignored wait. It called the termination callback straight away. Callbacks already running, or waiting in the shared pool, still ran after it, and draining a port cancelled the callbacks of every composite queue sharing that port. Now:

  • Each task carries the queue it was submitted to, and that queue counts it until it has run, whether live or cancelled.
  • Terminate sets its flag under that count's lock, and deliveries check the flag there. So a task either reaches its port before the drain, or is refused.
  • Terminate cancels only its own queue's tasks: from the ports, the pool and the delay list.
  • It completes when the count reaches zero. With wait it blocks until then; without it, 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 still cancelled before termination completes.
  • Guard: wait from inside one of the same queue's own callbacks doesn't block. Blocking there would deadlock, where main simply ignored wait. A per-thread stack of running queues detects the case, and it handles nested Immediate-port callbacks.

Tests (deterministic: channels, no sleeps):

  • cleanup_waits_for_work_under_way
  • terminate_completes_once_running_callbacks_return (with and without wait)
  • terminate_leaves_other_queues_on_the_port_alone
  • terminate_waiting_from_its_own_callback_returns (with the guard switched off it fails after 10 s, instead of hanging)

Results:

  • cargo test -p victory-hle --locked: 159 passed, 0 failed, 2 ignored.
  • The xasync tests passed 20 times out of 20 in parallel, and also pass with --all-features --test-threads=1.
  • cargo clippy -p victory-hle --all-targets --locked --no-deps -- -D warnings is clean, with and without --all-features. Workspace Clippy on main also fails on watch.rs:3205, which #241 fixes.

Left open in #228:

  • Worker threads are still uncapped. That's a design change, and a cap could bring back the pool starvation the pool was built to avoid.
  • complete_call still runs an immediate cleanup under the call's lock. Moving it out means cleanup could run after the title has freed the block.

For review:

  • Lock order: the queue's running-count lock is taken before the port and pool locks.
  • When cleanup is deferred, it runs after the call has ended, so the title may already have reused the block. A provider's cleanup should only free its context.

🤖 Generated with Claude Code

**@⁠sharan104** on GitHub · 2026-11-26 12:32 UTC · [original](https://github.com/avatarmanz/VIctory-7900XTX/pull/244) `sharan104:fix/xasync-cancel-races` → `main` · +366 −26 · 1 files · 1 commits · open Part of #228. It fixes the two race items in the issue and leaves the two design items open (listed below). It builds on main and doesn't overlap #229's #214 fix (`db23fd1`, `pending_with`). `git merge-tree` combines the two without conflicts. **Cleanup while `DoWork` / `Cancel` runs.** A cancel or complete on one thread could release a call while its provider's `DoWork` or `Cancel` was still running on another. The provider's cleanup, which frees its context, then ran underneath them. Now: - Each call counts the provider operations under way (begin, DoWork, cancel, get result). - `release` marks the call. If nothing is running, it cleans up at once; otherwise the last operation to return runs the cleanup. - No operation starts after release, so cleanup runs once, and last. **`XTaskQueueTerminate` ignored `wait`.** It called the termination callback straight away. Callbacks already running, or waiting in the shared pool, still ran after it, and draining a port cancelled the callbacks of every composite queue sharing that port. Now: - Each task carries the queue it was submitted to, and that queue counts it until it has run, whether live or cancelled. - Terminate sets its flag under that count's lock, and deliveries check the flag there. So a task either reaches its port before the drain, or is refused. - Terminate cancels only its own queue's tasks: from the ports, the pool and the delay list. - It completes when the count reaches zero. With `wait` it blocks until then; without it, 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 still cancelled before termination completes. - **Guard:** `wait` from inside one of the same queue's own callbacks doesn't block. Blocking there would deadlock, where main simply ignored `wait`. A per-thread stack of running queues detects the case, and it handles nested Immediate-port callbacks. **Tests** (deterministic: channels, no sleeps): - `cleanup_waits_for_work_under_way` - `terminate_completes_once_running_callbacks_return` (with and without `wait`) - `terminate_leaves_other_queues_on_the_port_alone` - `terminate_waiting_from_its_own_callback_returns` (with the guard switched off it fails after 10 s, instead of hanging) **Results:** - `cargo test -p victory-hle --locked`: 159 passed, 0 failed, 2 ignored. - The xasync tests passed 20 times out of 20 in parallel, and also pass with `--all-features --test-threads=1`. - `cargo clippy -p victory-hle --all-targets --locked --no-deps -- -D warnings` is clean, with and without `--all-features`. Workspace Clippy on main also fails on `watch.rs:3205`, which #241 fixes. **Left open in #228:** - Worker threads are still uncapped. That's a design change, and a cap could bring back the pool starvation the pool was built to avoid. - `complete_call` still runs an immediate cleanup under the call's lock. Moving it out means cleanup could run after the title has freed the block. **For review:** - Lock order: the queue's running-count lock is taken before the port and pool locks. - When cleanup is deferred, it runs after the call has ended, so the title may already have reused the block. A provider's cleanup should only free its context. 🤖 Generated with [Claude Code](https://claude.com/claude-code)
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 #228

Co-Authored-By: Claude Opus 5.5 <[email protected]>
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 12:36:17 +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/244/head:github-bot/gh-244'
git switch 'github-bot/gh-244'

Merge

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