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

Closed
github-bot wants to merge 2 commits from gh-pr/382/head into gh-pr/382/base
Owner

@⁠JordanFableFur on GitHub · 2026-11-25 00:12 UTC · original
JordanFableFur:fix/composite-terminate-keeps-base-tasks → main · +99 −9 · 1 files · 2 commits · closed without merging

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.

**@⁠JordanFableFur** on GitHub · 2026-11-25 00:12 UTC · [original](https://github.com/fxceless/victory-fork/pull/382) `JordanFableFur:fix/composite-terminate-keeps-base-tasks` → `main` · +99 −9 · 1 files · 2 commits · closed without merging ## 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.
A port shared by two queues (a composite's ports are the base queue's)
drained everything when one of them terminated: the base queue's
callbacks saw canceled too, and the base queue's dispatch had nothing to
run. The queues using a port have independent lifetimes (XTaskQueue.h).

Each task now carries the submitting queue's identity. Terminate only
cancels that queue's own tasks; the others keep their FIFO place and
run uncanceled, with the cancellation callbacks run outside the port
locks. A regression terminates a composite with a task from each queue
on the port and dispatches the base queue: the composite's callback was
cancelled at once, the base's runs uncanceled on the base queue.

AI use: written with Codex and Claude Code (GPT-5.2 Codex); reviewed and
posted with my approval. No game files were used.
Review of the first fix: the other queues' tasks sat in a local list
while this queue's cancellation callbacks ran, so a callback that
terminated another queue on the port passed their tasks by (a later
dispatch would run them uncanceled past the cleanup), and one that
submitted new work overtook them.

The partition now happens under the port's mutex: this queue's tasks
come out, the others keep their FIFO place in the deque, and only then
do the cancellation callbacks run outside the lock.
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 03:42:08 +00:00
github-bot closed this pull request 2026-11-26 03:42:08 +00:00
Author
Owner

@⁠chatgpt-codex-connector[bot] on GitHub · 2026-11-25 00:12 UTC · original · edited 2026-11-25 00:17 UTC

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.

**@⁠chatgpt-codex-connector[bot]** on GitHub · 2026-11-25 00:12 UTC · [original](https://github.com/fxceless/victory-fork/pull/382#issuecomment-7148270672) · edited 2026-11-25 00:17 UTC <!-- 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>
Author
Owner

@⁠JordanFableFur on GitHub · 2026-11-25 00:32 UTC · original

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.

**@⁠JordanFableFur** on GitHub · 2026-11-25 00:32 UTC · [original](https://github.com/fxceless/victory-fork/pull/382#issuecomment-7148623463) 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.
Author
Owner

@⁠kaayzouee on GitHub · 2026-11-25 01:19 UTC · original

Duplicate of #296?

**@⁠kaayzouee** on GitHub · 2026-11-25 01:19 UTC · [original](https://github.com/fxceless/victory-fork/pull/382#issuecomment-7149423760) Duplicate of #296?
Author
Owner

@⁠JordanFableFur on GitHub · 2026-11-25 01:46 UTC · original

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.

**@⁠JordanFableFur** on GitHub · 2026-11-25 01:46 UTC · [original](https://github.com/fxceless/victory-fork/pull/382#issuecomment-7149937816) 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.
Author
Owner

@⁠chatgpt-codex-connector[bot] on GitHub · 2026-11-25 00:17 UTC · original
On crates/victory-hle/src/modules/xasync.rs line 421 (outdated):

@@ -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 👍 / 👎.

**@⁠chatgpt-codex-connector[bot]** on GitHub · 2026-11-25 00:17 UTC · [original](https://github.com/fxceless/victory-fork/pull/382#discussion_r4688432859) On `crates/victory-hle/src/modules/xasync.rs` line 421 (outdated): ```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 -->

Pull request closed

This discussion has been locked. Commenting is limited to contributors.
No description provided.