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

Merged
Dufftwt merged 1 commit from upstream-pr/132 into main 2026-11-25 00:07:55 +00:00
Dufftwt commented 2026-11-24 00:49:03 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by Dufftwt on 2026-11-24T00:49:03Z.


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.

<!-- greyfinch-sync:Marvisak/VIctory:issue:6572034155 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/132). Originally posted by [Dufftwt](https://github.com/Dufftwt) on 2026-11-24T00:49:03Z. --- ## 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.
jeremygold02 commented 2026-11-24 06:55:47 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by jeremygold02 on 2026-11-24T04:31:08Z.


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.

<!-- greyfinch-sync:Marvisak/VIctory:comment:7122092176 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/132#issuecomment-7122092176). Originally posted by [jeremygold02](https://github.com/jeremygold02) on 2026-11-24T04:31:08Z. --- 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.
upstream-sync closed this pull request 2026-11-25 01:02:45 +00:00
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!132
No description provided.