XTaskQueue: terminating a queue cancels its delayed callbacks too #132

Manually merged
github merged 1 commit from gh-pr/132/head into gh-pr/132/base 2026-11-26 03:51:30 +00:00
Owner

@⁠Dufftwt on GitHub · 2026-11-24 00:49 UTC · original
Dufftwt:fix/terminate-cancels-delayed-callbacks → main · +54 −9 · 1 files · 1 commits · merged 2026-11-25 00:07 UTC

What's wrong

XTaskQueueSubmitDelayedCallback lets a title queue a callback to run after a delay. In xasync.rs, the delayed callback waits on a thread of its own and is handed to the queue when the delay runs out.

XTaskQueueTerminate is supposed to cancel everything still pending on the queue: each pending callback runs once with canceled = true, and only then does the termination callback fire. It only cancels what is already sitting in the queue's ports. A delayed callback still waiting out its delay is missed. When its delay runs out, it is delivered and runs as normal, live work:

submit callback, delay 50 ms
terminate queue          -> termination callback runs ("queue is gone")
... 50 ms later          -> the callback runs with canceled = false

A title typically frees whatever its callbacks use in the termination callback, or right after it. So that late callback can run against freed memory or a shut-down subsystem. At best it does work for a queue that no longer exists.

What the fix does

  • The queue keeps its delayed callbacks in a small list (delayed, in submission order) until their delay runs out.
  • XTaskQueueTerminate cancels those along with the queued ones, before it calls the termination callback.
  • When a delay runs out, the waiting thread takes its callback out of the list. If termination already took it, there is nothing left to deliver.
  • A submit that races XTaskQueueTerminate re-checks terminated while it holds the list's lock. A callback is therefore always either canceled with the others or refused with E_ABORT, never lost or run late.

How it behaves after the fix

  • Terminating a queue cancels pending delayed callbacks right away (canceled = true), before the termination callback, and they never run again afterwards.
  • Delayed callbacks on queues that are not terminated behave exactly as before.

Tests

The new test terminate_cancels_delayed_callbacks in xasync.rs submits a callback with a 50 ms delay and terminates the queue. It checks that the callback was canceled before the termination callback ran. It then waits 200 ms and checks that the callback did not run again.

I ran cargo test --workspace --no-fail-fast on GitHub's windows-latest with Rust 1.98.1 from rust-toolchain.toml:

  • This PR: the new test passes; victory-hle has 82 passed and 0 failed.
  • The new test alone on main: it fails. The callback was never canceled, and only the termination callback ran (left: ["terminated"], right: ["canceled", "terminated"]).
  • In both runs the only other failure is victory-gpu's root::tests::host_serialized_blobs_create_root_signatures. It fails the same way on the main this branch starts from, and #36, now merged, fixes it.

Not tested: a run of the game itself.

Related: #114 and #122 also change xasync.rs, in other functions. They don't depend on each other. #114 adds its test at the end of the same test module as this one, so whichever lands second may need a one-line rebase there.

**@⁠Dufftwt** on GitHub · 2026-11-24 00:49 UTC · [original](https://github.com/Marvisak/VIctory/pull/132) `Dufftwt:fix/terminate-cancels-delayed-callbacks` → `main` · +54 −9 · 1 files · 1 commits · merged 2026-11-25 00:07 UTC ## What's wrong `XTaskQueueSubmitDelayedCallback` lets a title queue a callback to run after a delay. In `xasync.rs`, the delayed callback waits on a thread of its own and is handed to the queue when the delay runs out. `XTaskQueueTerminate` is supposed to cancel **everything still pending** on the queue: each pending callback runs once with `canceled = true`, and only then does the termination callback fire. It only cancels what is already sitting in the queue's ports. A delayed callback still waiting out its delay is missed. When its delay runs out, it is delivered and runs as normal, live work: ``` submit callback, delay 50 ms terminate queue -> termination callback runs ("queue is gone") ... 50 ms later -> the callback runs with canceled = false ``` A title typically frees whatever its callbacks use in the termination callback, or right after it. So that late callback can run against freed memory or a shut-down subsystem. At best it does work for a queue that no longer exists. ## What the fix does - The queue keeps its delayed callbacks in a small list (`delayed`, in submission order) until their delay runs out. - `XTaskQueueTerminate` cancels those along with the queued ones, before it calls the termination callback. - When a delay runs out, the waiting thread takes its callback out of the list. If termination already took it, there is nothing left to deliver. - A submit that races `XTaskQueueTerminate` re-checks `terminated` while it holds the list's lock. A callback is therefore always either canceled with the others or refused with `E_ABORT`, never lost or run late. ## How it behaves after the fix - Terminating a queue cancels pending delayed callbacks right away (`canceled = true`), before the termination callback, and they never run again afterwards. - Delayed callbacks on queues that are not terminated behave exactly as before. ## Tests The new test `terminate_cancels_delayed_callbacks` in `xasync.rs` submits a callback with a 50 ms delay and terminates the queue. It checks that the callback was canceled *before* the termination callback ran. It then waits 200 ms and checks that the callback did not run again. I ran `cargo test --workspace --no-fail-fast` on GitHub's `windows-latest` with Rust 1.98.1 from `rust-toolchain.toml`: - **This PR:** the new test passes; `victory-hle` has 82 passed and 0 failed. - **The new test alone on `main`:** it fails. The callback was never canceled, and only the termination callback ran (`left: ["terminated"]`, `right: ["canceled", "terminated"]`). - In both runs the only other failure is `victory-gpu`'s `root::tests::host_serialized_blobs_create_root_signatures`. It fails the same way on the `main` this branch starts from, and #36, now merged, fixes it. **Not tested:** a run of the game itself. **Related:** #114 and #122 also change `xasync.rs`, in other functions. They don't depend on each other. #114 adds its test at the end of the same test module as this one, so whichever lands second may need a one-line rebase there.
A callback submitted with a delay waited on a thread of its own and was
delivered when the delay ran out, whether or not the queue had been
terminated meanwhile. XTaskQueueTerminate only canceled what was already
in the ports, so a pending delayed callback was not canceled: it ran
later as live work (canceled = false), after the termination callback,
by which time a title may have freed what the callback uses.

The queue now keeps its delayed callbacks in a list until their delay
runs out. Terminate cancels them along with the queued ones, before it
calls the termination callback, and a delay that runs out after that
finds nothing to deliver. A submit racing Terminate checks `terminated`
under the list's lock, so the callback is either canceled or refused.
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 03:51:27 +00:00
github manually merged commit b9ff219826 into gh-pr/132/base 2026-11-26 03:51:30 +00:00
Author
Owner

@⁠jeremygold02 on GitHub · 2026-11-24 04:31 UTC · original

Tested head b9ff219826e61ba6306f6a1378b613319f70da47 with cargo test -p victory-hle --lib xasync -- --test-threads=1: 6 passed, 0 failed (76 unrelated library tests filtered out). The relevant tests modules::xasync::tests::terminate_cancels_delayed_callbacks and terminate_cancels_what_is_queued pass; they cover delayed and already-queued callbacks when a queue is terminated (xasync.rs). This is synthetic unit coverage, not a title runtime test.

**@⁠jeremygold02** on GitHub · 2026-11-24 04:31 UTC · [original](https://github.com/Marvisak/VIctory/pull/132#issuecomment-7122092176) Tested head `b9ff219826e61ba6306f6a1378b613319f70da47` with `cargo test -p victory-hle --lib xasync -- --test-threads=1`: **6 passed, 0 failed** (76 unrelated library tests filtered out). The relevant tests `modules::xasync::tests::terminate_cancels_delayed_callbacks` and `terminate_cancels_what_is_queued` pass; they cover delayed and already-queued callbacks when a queue is terminated ([`xasync.rs`](https://github.com/Marvisak/VIctory/blob/b9ff219826e61ba6306f6a1378b613319f70da47/crates/victory-hle/src/modules/xasync.rs)). This is synthetic unit coverage, not a title runtime test.
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!132
No description provided.