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

Open
sharan104 wants to merge 1 commit from upstream-pr/244 into main
sharan104 commented 2026-11-26 12:32:26 +00:00 (Migrated from github.com)

Imported from avatarmanz/VIctory-7900XTX. Originally posted by sharan104 on 2026-11-26T12:32:26Z.


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

<!-- greyfinch-sync:avatarmanz/VIctory-7900XTX:issue:6626714662 --> Imported from [avatarmanz/VIctory-7900XTX](https://github.com/avatarmanz/VIctory-7900XTX/pull/244). Originally posted by [sharan104](https://github.com/sharan104) on 2026-11-26T12:32:26Z. --- 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)
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/244:upstream-pr/244'
git switch 'upstream-pr/244'

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/244'
git switch 'upstream-pr/244'
git rebase 'main'
git switch 'main'
git merge --ff-only 'upstream-pr/244'
git switch 'upstream-pr/244'
git rebase 'main'
git switch 'main'
git merge --no-ff 'upstream-pr/244'
git switch 'main'
git merge --squash 'upstream-pr/244'
git switch 'main'
git merge --ff-only 'upstream-pr/244'
git switch 'main'
git merge 'upstream-pr/244'
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
avatarmanz/VIctory-7900XTX!244
No description provided.