HLE: cancel delayed task queue deliveries after termination #3180

Open
fuzeofficlal wants to merge 1 commit from upstream-pr/3182 into main
fuzeofficlal commented 2026-11-26 11:21:31 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by fuzeofficlal on 2026-11-26T11:21:31Z.


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
<!-- greyfinch-sync:Marvisak/VIctory:issue:6625562184 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/3182). Originally posted by [fuzeofficlal](https://github.com/fuzeofficlal) on 2026-11-26T11:21:31Z. --- ### 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
cloph-dsp commented 2026-11-26 12:06:01 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by cloph-dsp on 2026-11-26T11:59:03Z.


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.

<!-- greyfinch-sync:Marvisak/VIctory:comment:7191415558 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/3182#issuecomment-7191415558). Originally posted by [cloph-dsp](https://github.com/cloph-dsp) on 2026-11-26T11:59:03Z. --- 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.
Dukestar69 commented 2026-11-26 14:01:31 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by Dukestar69 on 2026-11-26T13:10:36Z.

Review: APPROVED


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).

<!-- greyfinch-sync:Marvisak/VIctory:review:6074693951 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/3182#pullrequestreview-6074693951). Originally posted by [Dukestar69](https://github.com/Dukestar69) on 2026-11-26T13:10:36Z. **Review:** APPROVED --- 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).
futurepro62 commented 2026-11-27 22:13:24 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by futurepro62 on 2026-11-27T22:13:24Z.


NVIDIA full-suite run, to go with the reports above by Dukestar69, cloph-dsp, since none of them names a GPU run. Tested exact head 0bdf7ce (on its base 63bcca4).

Tested

  • NVIDIA GeForce RTX 5070 Ti 16 GB (Blackwell, 32-lane waves only, mesh shaders), driver 621.40, Ryzen 7 9800X3D, Windows 11 Home 26300, Rust 1.98.1 MSVC (pinned), dxil.dll from Windows SDK 10.0.26100 (the loader's default), debug.
  • cargo build --workspace --locked, then cargo test --workspace --locked --no-fail-fast -- --test-threads=4 --skip emit::memory_tests::heap_direct_texture_reads_mark_their_heap_index --skip descriptor::host::tests::dynamic_slots_are_held_until_released --skip memory::tests::lists_share_a_table_and_copies_keep_their_views --skip emit::stride_tests::a_heap_buffer_read_by_element_takes_the_views_stride --skip emit::stride_tests::a_heap_buffer_read_where_no_buffer_view_is_reads_zeros --skip emit::synthetic_tests::gathers_at_a_level_match_the_hardware: 868 passed, 0 failed, 10 ignored.
  • The new tests ran and passed: delivery_after_termination_cancels_without_notifying_monitors, termination_cancels_a_delayed_callback_during_delivery_handoff.
  • --skip leaves out tests that fail on main 63bcca4 on this card too, not because of this PR: heap_direct_texture_reads_mark_their_heap_index ends the victory-shader test process with 0xc0000005 (#2571), so without the skip the rest of that crate never reports; dynamic_slots_are_held_until_released, lists_share_a_table_and_copies_keep_their_views and the two stride_tests hit the 1,000,000-view heap cap of NVIDIA drivers (#1835, #2317); gathers_at_a_level_match_the_hardware fails on lane 0 (#1744).

Not tested

  • AMD (the tests take the default adapter), a release build, the title (test run only).

AI use

  • Claude Code (Claude Opus 5.5) ran the tests and wrote this report. I checked it before posting.
<!-- greyfinch-sync:Marvisak/VIctory:comment:7229135693 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/3182#issuecomment-7229135693). Originally posted by [futurepro62](https://github.com/futurepro62) on 2026-11-27T22:13:24Z. --- NVIDIA full-suite run, to go with the reports above by Dukestar69, cloph-dsp, since none of them names a GPU run. Tested exact head `0bdf7ce` (on its base `63bcca4`). **Tested** - NVIDIA GeForce RTX 5070 Ti 16 GB (Blackwell, 32-lane waves only, mesh shaders), driver 621.40, Ryzen 7 9800X3D, Windows 11 Home 26300, Rust 1.98.1 MSVC (pinned), `dxil.dll` from Windows SDK 10.0.26100 (the loader's default), debug. - `cargo build --workspace --locked`, then `cargo test --workspace --locked --no-fail-fast -- --test-threads=4 --skip emit::memory_tests::heap_direct_texture_reads_mark_their_heap_index --skip descriptor::host::tests::dynamic_slots_are_held_until_released --skip memory::tests::lists_share_a_table_and_copies_keep_their_views --skip emit::stride_tests::a_heap_buffer_read_by_element_takes_the_views_stride --skip emit::stride_tests::a_heap_buffer_read_where_no_buffer_view_is_reads_zeros --skip emit::synthetic_tests::gathers_at_a_level_match_the_hardware`: **868 passed, 0 failed, 10 ignored**. - The new tests ran and passed: `delivery_after_termination_cancels_without_notifying_monitors`, `termination_cancels_a_delayed_callback_during_delivery_handoff`. - `--skip` leaves out tests that fail on main `63bcca4` on this card too, not because of this PR: `heap_direct_texture_reads_mark_their_heap_index` ends the `victory-shader` test process with `0xc0000005` (#2571), so without the skip the rest of that crate never reports; `dynamic_slots_are_held_until_released`, `lists_share_a_table_and_copies_keep_their_views` and the two `stride_tests` hit the 1,000,000-view heap cap of NVIDIA drivers (#1835, #2317); `gathers_at_a_level_match_the_hardware` fails on lane 0 (#1744). **Not tested** - AMD (the tests take the default adapter), a release build, the title (test run only). **AI use** - Claude Code (Claude Opus 5.5) ran the tests and wrote this report. I checked it before posting.
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/3182:upstream-pr/3182'
git switch 'upstream-pr/3182'

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/3182'
git switch 'upstream-pr/3182'
git rebase 'main'
git switch 'main'
git merge --ff-only 'upstream-pr/3182'
git switch 'upstream-pr/3182'
git rebase 'main'
git switch 'main'
git merge --no-ff 'upstream-pr/3182'
git switch 'main'
git merge --squash 'upstream-pr/3182'
git switch 'main'
git merge --ff-only 'upstream-pr/3182'
git switch 'main'
git merge 'upstream-pr/3182'
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
Marvisak/VIctory!3180
No description provided.