HLE: cancel delayed task queue deliveries after termination #3182

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

@⁠fuzeofficlal on GitHub · 2026-11-26 11:21 UTC · original
fuzeofficlal:fix/hle-xtaskqueue-deliver-terminated → main · +126 −3 · 1 files · 1 commits · open

Summary

In crates/victory-hle/src/modules/xasync.rs, Queue::submit with delay_ms > 0 checked self.terminated only at submission time, then spawned a sleeping thread that removed the task from queue.delayed and called queue.deliver(port, task). Because Queue::deliver never checked self.terminated, a delayed callback that woke up right after XTaskQueueTerminate (or raced between delayed.remove(&id) and deliver) could still execute with canceled = false on ThreadPool/Immediate ports, re-enqueue onto a drained Manual/Serialized port, and invoke queue monitors after termination.

Fix

  • Pass &self.terminated to Port::submit and check terminated.load(Ordering::Acquire) while holding lock(&self.tasks) for Mode::Manual | Mode::Serialized, serializing against XTaskQueueTerminate's terminated.store(true, Ordering::Release) + port.tasks.drain(..) while preserving independent queue termination on shared composite ports (XTaskQueueCreateComposite).
  • In Queue::deliver, if self.terminated.load(Ordering::Acquire) or !Port::submit(...), run task.run(true) (canceled = true) and return without notifying monitors; also re-check self.terminated inside the monitor loop in case an Immediate callback or earlier monitor terminates the queue.

Verification

Added two deterministic unit tests in crates/victory-hle/src/modules/xasync.rs:

  • delivery_after_termination_cancels_without_notifying_monitors (covers all four port modes: Manual, Serialized, ThreadPool, Immediate)
  • termination_cancels_a_delayed_callback_during_delivery_handoff (deterministically exercises the delayed.remove(&id) -> deliver() handoff race)

Ran:

  • cargo test --locked -p victory-hle modules::xasync -- --test-threads=1: 15 passed, 0 failed
  • cargo clippy -p victory-hle --all-targets --locked -- -D warnings (default and --no-default-features): clean
  • cargo test -p xtask --locked policy::tests::repository_passes_policy: passed
**@⁠fuzeofficlal** on GitHub · 2026-11-26 11:21 UTC · [original](https://github.com/Marvisak/VIctory/pull/3182) `fuzeofficlal:fix/hle-xtaskqueue-deliver-terminated` → `main` · +126 −3 · 1 files · 1 commits · open ### Summary In `crates/victory-hle/src/modules/xasync.rs`, `Queue::submit` with `delay_ms > 0` checked `self.terminated` only at submission time, then spawned a sleeping thread that removed the task from `queue.delayed` and called `queue.deliver(port, task)`. Because `Queue::deliver` never checked `self.terminated`, a delayed callback that woke up right after `XTaskQueueTerminate` (or raced between `delayed.remove(&id)` and `deliver`) could still execute with `canceled = false` on `ThreadPool`/`Immediate` ports, re-enqueue onto a drained `Manual`/`Serialized` port, and invoke queue monitors after termination. ### Fix - Pass `&self.terminated` to `Port::submit` and check `terminated.load(Ordering::Acquire)` while holding `lock(&self.tasks)` for `Mode::Manual | Mode::Serialized`, serializing against `XTaskQueueTerminate`'s `terminated.store(true, Ordering::Release)` + `port.tasks.drain(..)` while preserving independent queue termination on shared composite ports (`XTaskQueueCreateComposite`). - In `Queue::deliver`, if `self.terminated.load(Ordering::Acquire)` or `!Port::submit(...)`, run `task.run(true)` (`canceled = true`) and return without notifying monitors; also re-check `self.terminated` inside the monitor loop in case an `Immediate` callback or earlier monitor terminates the queue. ### Verification Added two deterministic unit tests in `crates/victory-hle/src/modules/xasync.rs`: - `delivery_after_termination_cancels_without_notifying_monitors` (covers all four port modes: `Manual`, `Serialized`, `ThreadPool`, `Immediate`) - `termination_cancels_a_delayed_callback_during_delivery_handoff` (deterministically exercises the `delayed.remove(&id)` -> `deliver()` handoff race) Ran: - `cargo test --locked -p victory-hle modules::xasync -- --test-threads=1`: 15 passed, 0 failed - `cargo clippy -p victory-hle --all-targets --locked -- -D warnings` (default and `--no-default-features`): clean - `cargo test -p xtask --locked policy::tests::repository_passes_policy`: passed
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 11:33:03 +00:00
Author
Owner

@⁠cloph-dsp on GitHub · 2026-11-26 11:59 UTC · original

Independent local verification of head 0bdf7ce on Windows, Rust 1.100.0/MSVC:

  • cargo test --locked -p victory-hle --lib modules::xasync::tests::delivery_after_termination_cancels_without_notifying_monitors -- --exact — 1 passed, 0 failed.
  • cargo test --locked -p victory-hle --lib modules::xasync::tests::termination_cancels_a_delayed_callback_during_delivery_handoff -- --exact — 1 passed, 0 failed.
  • git diff --check origin/main HEAD — clean.

Worktree-local CARGO_TARGET_DIR used; synthetic Windows-thread scenarios only, no game assets or DRM material. AI-assisted (Hy4 flash). GPU/driver N/A for these CPU-only tests.

**@⁠cloph-dsp** on GitHub · 2026-11-26 11:59 UTC · [original](https://github.com/Marvisak/VIctory/pull/3182#issuecomment-7191415558) Independent local verification of head `0bdf7ce` on Windows, Rust 1.100.0/MSVC: - `cargo test --locked -p victory-hle --lib modules::xasync::tests::delivery_after_termination_cancels_without_notifying_monitors -- --exact` — 1 passed, 0 failed. - `cargo test --locked -p victory-hle --lib modules::xasync::tests::termination_cancels_a_delayed_callback_during_delivery_handoff -- --exact` — 1 passed, 0 failed. - `git diff --check origin/main HEAD` — clean. Worktree-local `CARGO_TARGET_DIR` used; synthetic Windows-thread scenarios only, no game assets or DRM material. AI-assisted (Hy4 flash). GPU/driver N/A for these CPU-only tests.
Author
Owner

@⁠Dukestar69 on GitHub · 2026-11-26 13:10 UTC · original · approved these changes

Verified against origin/main (63bcca4):

  • cargo test -p victory-hle --lib modules::xasync:: -- --test-threads=1: 15/15 passed (including delivery_after_termination_cancels_without_notifying_monitors and termination_cancels_a_delayed_callback_during_delivery_handoff).
  • cargo clippy -p victory-hle --all-targets -- -D warnings: clean.
  • cargo xtask policy: 191 files ok.

Cleanly cancels delayed task-queue deliveries racing with termination without notifying monitors.

Synthetic tests only (no game files used). AI-assisted (Gemini 4 Neon).

**@⁠Dukestar69** on GitHub · 2026-11-26 13:10 UTC · [original](https://github.com/Marvisak/VIctory/pull/3182#pullrequestreview-6074693951) · **approved these changes** Verified against `origin/main` (`63bcca4`): - `cargo test -p victory-hle --lib modules::xasync:: -- --test-threads=1`: 15/15 passed (including `delivery_after_termination_cancels_without_notifying_monitors` and `termination_cancels_a_delayed_callback_during_delivery_handoff`). - `cargo clippy -p victory-hle --all-targets -- -D warnings`: clean. - `cargo xtask policy`: 191 files ok. Cleanly cancels delayed task-queue deliveries racing with termination without notifying monitors. Synthetic tests only (no game files used). AI-assisted (Gemini 4 Neon).
This pull request can be merged automatically.
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/3182/head:github-bot/gh-3182'
git switch 'github-bot/gh-3182'

Merge

Merge the changes and update on Forgejo.
git switch 'main'
git merge --no-ff 'github-bot/gh-3182'
git switch 'github-bot/gh-3182'
git rebase 'main'
git switch 'main'
git merge --ff-only 'github-bot/gh-3182'
git switch 'github-bot/gh-3182'
git rebase 'main'
git switch 'main'
git merge --no-ff 'github-bot/gh-3182'
git switch 'main'
git merge --squash 'github-bot/gh-3182'
git switch 'main'
git merge --ff-only 'github-bot/gh-3182'
git switch 'main'
git merge 'github-bot/gh-3182'
git push origin 'main'
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/Marvisak--VIctory!3182
No description provided.