XTaskQueue: terminating a composite spares the base queue's tasks #382

Closed
JordanFableFur wants to merge 2 commits from upstream-pr/382 into main
JordanFableFur commented 2026-11-25 00:12:44 +00:00 (Migrated from github.com)

Imported from fxceless/victory-fork. Originally posted by JordanFableFur on 2026-11-25T00:12:44Z.


What

A port shared by two queues (a composite queue's ports are the base queue's own) drained everything when one of them terminated: the base queue's pending callbacks ran with canceled=true, and a dispatch of the base queue had nothing left to run. The queues using a port have independent lifetimes (XTaskQueue.h: terminating one must not cancel what belongs to another).

Each task now carries the submitting queue's identity. XTaskQueueTerminate only cancels that queue's own tasks; the others keep their FIFO place and run uncanceled. Cancellation callbacks now run outside the port locks (they may submit again or take locks). Tasks still delayed (their own thread sleeping before delivery) are issue #198's case and are not covered here; this fixes what is already in the shared port.

Fixes #243.

Testing

A regression creates a manual base queue and a composite over its ports, submits one immediate callback on each, terminates the composite, then dispatches the base queue: the composite's callback was cancelled at once, the base's stays queued and runs with canceled=false on the base queue. The existing terminate test is unchanged and passes.

Validated on Windows CI (fork-run of the repository workflow): clippy both builds and the full test suite pass: https://github.com/JordanFableFur/victory-fork/actions/runs/45513602357

AI use: written with Codex and Claude Code (GPT-5.2 Codex); reviewed and posted with my approval. No game files were used.

<!-- greyfinch-sync:fxceless/victory-fork:issue:6594105933 --> Imported from [fxceless/victory-fork](https://github.com/fxceless/victory-fork/pull/382). Originally posted by [JordanFableFur](https://github.com/JordanFableFur) on 2026-11-25T00:12:44Z. --- ## What A port shared by two queues (a composite queue's ports are the base queue's own) drained everything when one of them terminated: the base queue's pending callbacks ran with `canceled=true`, and a dispatch of the base queue had nothing left to run. The queues using a port have independent lifetimes (XTaskQueue.h: terminating one must not cancel what belongs to another). Each task now carries the submitting queue's identity. `XTaskQueueTerminate` only cancels that queue's own tasks; the others keep their FIFO place and run uncanceled. Cancellation callbacks now run outside the port locks (they may submit again or take locks). Tasks still delayed (their own thread sleeping before delivery) are issue #198's case and are not covered here; this fixes what is already in the shared port. Fixes #243. ## Testing A regression creates a manual base queue and a composite over its ports, submits one immediate callback on each, terminates the composite, then dispatches the base queue: the composite's callback was cancelled at once, the base's stays queued and runs with `canceled=false` on the base queue. The existing terminate test is unchanged and passes. Validated on Windows CI (fork-run of the repository workflow): clippy both builds and the full test suite pass: https://github.com/JordanFableFur/victory-fork/actions/runs/45513602357 **AI use:** written with Codex and Claude Code (GPT-5.2 Codex); reviewed and posted with my approval. No game files were used.
chatgpt-codex-connector[bot commented 2026-11-25 01:11:35 +00:00 (Migrated from github.com)

Imported from fxceless/victory-fork. Originally posted by chatgpt-codex-connector[bot] on 2026-11-25T00:17:45Z.

Review: COMMENTED


💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 9ce1c19544

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

<!-- greyfinch-sync:fxceless/victory-fork:review:6049577588 --> Imported from [fxceless/victory-fork](https://github.com/fxceless/victory-fork/pull/382#pullrequestreview-6049577588). Originally posted by [chatgpt-codex-connector[bot]](https://github.com/chatgpt-codex-connector%5Bbot%5D) on 2026-11-25T00:17:45Z. **Review:** COMMENTED --- <!-- codex-review-receipt:v1 --> ### 💡 Codex Review Here are some automated review suggestions for this pull request. **Reviewed commit:** `9ce1c19544` <details> <summary>ℹ️ About Codex in GitHub</summary> <br/> [Your team has set up Codex to review pull requests in this repo](https://chatgpt.com/codex/cloud/settings/general). Reviews are triggered when you - Open a pull request for review - Mark a draft as ready - Comment "@codex review". If Codex has suggestions, it will comment; otherwise it will react with 👍. Codex can also answer questions or update the PR. Try commenting "@codex address that feedback". </details>
chatgpt-codex-connector[bot commented 2026-11-25 01:11:57 +00:00 (Migrated from github.com)

Imported from fxceless/victory-fork. Originally posted by chatgpt-codex-connector[bot] on 2026-11-25T00:12:52Z.


Codex Review Summary: ✅ Completed

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-25T00:17:47.211997Z 9ce1c19 PR opened
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

<!-- greyfinch-sync:fxceless/victory-fork:comment:7148270672 --> Imported from [fxceless/victory-fork](https://github.com/fxceless/victory-fork/pull/382#issuecomment-7148270672). Originally posted by [chatgpt-codex-connector[bot]](https://github.com/chatgpt-codex-connector%5Bbot%5D) on 2026-11-25T00:12:52Z. --- <!-- codex-pull-request-review-summary --> <!-- codex-review-state:v1 {"code_review":{"commit_sha":"9ce1c19544823529011da52587c805f7328e5d0e","has_any_findings":true,"has_unresolved_findings":true,"status":"completed"},"security_review":{"commit_sha":null,"has_any_findings":null,"has_unresolved_findings":null,"status":null}} --> <details> <summary><strong>Codex Review Summary</strong>: ✅ Completed</summary> This comment shows the latest Codex review activity on this pull request. | Review | Status | Commit | Review trigger | | --- | --- | --- | --- | | 📝 **Code Review** | ✅ **Completed** <relative-time datetime="2026-11-25T00:17:47.211997Z">2026-11-25T00:17:47.211997Z</relative-time> | `9ce1c19` | PR opened | <details> <summary>ℹ️ About Codex in GitHub</summary> <br/> [Your team has set up Codex to review pull requests in this repo](https://chatgpt.com/codex/cloud/settings/general). Reviews are triggered when you - Open a pull request for review - Mark a draft as ready - Comment "@codex review" or "@codex security review". Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings. </details> </details>
JordanFableFur commented 2026-11-25 01:12:01 +00:00 (Migrated from github.com)

Imported from fxceless/victory-fork. Originally posted by JordanFableFur on 2026-11-25T00:32:58Z.


Addressed in 75258c0: the partition happens under each port's mutex — this queue's tasks come out, the others keep their FIFO place in the deque — and the cancellation callbacks run afterwards, outside the lock. A nested terminate (or a concurrent one) now sees the other queues' tasks in place, and a callback's submits queue behind them.

<!-- greyfinch-sync:fxceless/victory-fork:comment:7148623463 --> Imported from [fxceless/victory-fork](https://github.com/fxceless/victory-fork/pull/382#issuecomment-7148623463). Originally posted by [JordanFableFur](https://github.com/JordanFableFur) on 2026-11-25T00:32:58Z. --- Addressed in 75258c0: the partition happens under each port's mutex — this queue's tasks come out, the others keep their FIFO place in the deque — and the cancellation callbacks run afterwards, outside the lock. A nested terminate (or a concurrent one) now sees the other queues' tasks in place, and a callback's submits queue behind them.
chatgpt-codex-connector[bot commented 2026-11-25 01:12:09 +00:00 (Migrated from github.com)

Imported from fxceless/victory-fork. Originally posted by chatgpt-codex-connector[bot] on 2026-11-25T00:17:45Z.

Code review location: crates/victory-hle/src/modules/xasync.rs; line 421

@@ -381,16 +398,29 @@ pub fn XTaskQueueTerminate(
         return E_INVALIDARG;
     };
     queue.terminated.store(true, Ordering::Release);
+    let owner = Arc::as_ptr(&queue) as usize;
+    let mut others = Vec::new();
     for port in &queue.ports {
+        // This queue's tasks out, the others in order.
         let pending: Vec<Task> = lock(&port.tasks).drain(..).collect();
         // Dispatches waiting on this queue end: nothing more will come.
         // (Those of other queues on the port see theirs is open and wait
         // on.) After the lock above, so none misses both flag and wake.
         port.ready.notify_all();
         for task in pending {
-            task.run(true);
+            if task.owner == Some(owner) {
+                task.run(true);
+            } else {
+                others.push((Arc::clone(port), task));
+            }
         }
     }
+    // Cancellation callbacks may submit again or take locks; these run
+    // outside the port locks.
+    for (port, task) in others {
+        lock(&port.tasks).push_back(task);

P1 Badge Keep other queues' tasks queued during cancellation callbacks

When a composite's cancellation callback terminates the base queue, an earlier base task is already hidden in others. The nested XTaskQueueTerminate(base, true, ...) therefore returns without canceling that task, and this loop subsequently restores it; a later base dispatch invokes it with canceled=false, potentially after termination cleanup has released its context. Concurrent terminations have the same race. Even a cancellation callback that merely submits new base work causes that work to overtake the older buffered tasks. Partition each port's deque under its mutex, leaving non-owned tasks queued before invoking the extracted cancellation callbacks.

Useful? React with 👍 / 👎.

<!-- greyfinch-sync:fxceless/victory-fork:review-comment:4688432859 --> Imported from [fxceless/victory-fork](https://github.com/fxceless/victory-fork/pull/382#discussion_r4688432859). Originally posted by [chatgpt-codex-connector[bot]](https://github.com/chatgpt-codex-connector%5Bbot%5D) on 2026-11-25T00:17:45Z. **Code review location:** `crates/victory-hle/src/modules/xasync.rs`; line 421 ~~~~diff @@ -381,16 +398,29 @@ pub fn XTaskQueueTerminate( return E_INVALIDARG; }; queue.terminated.store(true, Ordering::Release); + let owner = Arc::as_ptr(&queue) as usize; + let mut others = Vec::new(); for port in &queue.ports { + // This queue's tasks out, the others in order. let pending: Vec<Task> = lock(&port.tasks).drain(..).collect(); // Dispatches waiting on this queue end: nothing more will come. // (Those of other queues on the port see theirs is open and wait // on.) After the lock above, so none misses both flag and wake. port.ready.notify_all(); for task in pending { - task.run(true); + if task.owner == Some(owner) { + task.run(true); + } else { + others.push((Arc::clone(port), task)); + } } } + // Cancellation callbacks may submit again or take locks; these run + // outside the port locks. + for (port, task) in others { + lock(&port.tasks).push_back(task); ~~~~ --- **<sub><sub>![P1 Badge](https://img.shields.io/badge/P1-orange?style=flat)</sub></sub> Keep other queues' tasks queued during cancellation callbacks** When a composite's cancellation callback terminates the base queue, an earlier base task is already hidden in `others`. The nested `XTaskQueueTerminate(base, true, ...)` therefore returns without canceling that task, and this loop subsequently restores it; a later base dispatch invokes it with `canceled=false`, potentially after termination cleanup has released its context. Concurrent terminations have the same race. Even a cancellation callback that merely submits new base work causes that work to overtake the older buffered tasks. Partition each port's deque under its mutex, leaving non-owned tasks queued before invoking the extracted cancellation callbacks. Useful? React with 👍 / 👎. <!-- codex-code-review-finding:v1 -->
upstream-sync closed this pull request 2026-11-25 02:09:28 +00:00
kaayzouee commented 2026-11-25 02:10:06 +00:00 (Migrated from github.com)

Imported from fxceless/victory-fork. Originally posted by kaayzouee on 2026-11-25T01:19:03Z.


Duplicate of #296?

<!-- greyfinch-sync:fxceless/victory-fork:comment:7149423760 --> Imported from [fxceless/victory-fork](https://github.com/fxceless/victory-fork/pull/382#issuecomment-7149423760). Originally posted by [kaayzouee](https://github.com/kaayzouee) on 2026-11-25T01:19:03Z. --- Duplicate of #296?
JordanFableFur commented 2026-11-25 02:10:11 +00:00 (Migrated from github.com)

Imported from fxceless/victory-fork. Originally posted by JordanFableFur on 2026-11-25T01:46:20Z.


Closing — #296 fixes the same issue (#243) and was opened first, including the partition under the port lock so a cancellation callback cannot miss the other queues' tasks. Thanks for flagging.

<!-- greyfinch-sync:fxceless/victory-fork:comment:7149937816 --> Imported from [fxceless/victory-fork](https://github.com/fxceless/victory-fork/pull/382#issuecomment-7149937816). Originally posted by [JordanFableFur](https://github.com/JordanFableFur) on 2026-11-25T01:46:20Z. --- Closing — #296 fixes the same issue (#243) and was opened first, including the partition under the port lock so a cancellation callback cannot miss the other queues' tasks. Thanks for flagging.

Pull request closed

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
fxceless/victory-fork!382
No description provided.