Skip to content

fix: clean up queued chat turns on abort - #102995

Closed
MonkeyLeeT wants to merge 2 commits into
openclaw:mainfrom
MonkeyLeeT:codex/fix-102914-queued-chat-abort
Closed

MonkeyLeeT wants to merge 2 commits into
openclaw:mainfrom
MonkeyLeeT:codex/fix-102914-queued-chat-abort

Conversation

@MonkeyLeeT

@MonkeyLeeT MonkeyLeeT commented Jul 9, 2026 •

Copy link
Copy Markdown
Contributor

Closes #102914

AI-assisted: yes

What Problem This Solves

Fixes an issue where queued chat followup turns could remain in Gateway memory when the owning abort signal fired before queue completion. This affected the queued chat/followup lifecycle and could leave stale run identities in chatQueuedTurns until the turn was otherwise processed.

Why This Change Was Made

The Gateway now attaches a once-only abort listener when a queued chat turn is registered. The listener removes only the exact current queued entry that still owns cancellation, so stale listeners cannot remove a reused run id and retired collect-mode entries remain as idempotency guards until aggregate completion.

User Impact

Gateway queued chat turns clean up promptly after owner aborts without allowing duplicate dispatches for retired collect entries. This reduces stale in-memory queue state while preserving existing idempotency behavior.

Evidence

Real Gateway queue behavior proof from the production queue module, run against PR head 3aecbe6bf2e443db0cff0ab4df473293dbb3fe80:

REAL_GATEWAY_QUEUE_PROOF commit=3aecbe6bf2e443db0cff0ab4df473293dbb3fe80 surface=src/gateway/chat-queued-turns.ts
PASS queued_turn_registered {"runId":"proof-owner-abort","mapSize":1,"signalAborted":false}
PASS owner_abort_fired_and_entry_removed {"runId":"proof-owner-abort","mapSize":0,"signalAborted":true}
PASS stale_abort_did_not_remove_reused_run_id {"runId":"proof-reused","mapSize":1,"currentSessionId":"new-session"}
PASS current_reused_owner_abort_removed_entry {"runId":"proof-reused","mapSize":0}
PASS retired_collect_identity_survived_abort {"runId":"proof-retired","mapSize":1,"abortable":false}
PASS retired_collect_identity_removed_on_completion {"runId":"proof-retired","mapSize":0}
REAL_GATEWAY_QUEUE_PROOF result=pass

The proof script imported registerQueuedChatTurn, retireQueuedChatTurnCancellation, completeQueuedChatTurn, and getQueuedChatTurn from src/gateway/chat-queued-turns.ts, then exercised the runtime queue map directly. It shows the queued turn is registered, the owner abort signal fires, the entry is removed, stale aborts do not remove reused run ids, and retired collect identities survive abort until completion.

Additional validation:

  • git diff --check
  • node scripts/run-vitest.mjs src/gateway/chat-queued-turns.test.ts src/gateway/server.chat.gateway-server-chat-b.test.ts passed after final rebase: 6 files, 148 tests.
  • One local autoreview pass found a collect-retirement blocker; this PR includes the fix and a regression test for retired collect identities surviving abort until completion.

@openclaw-barnacle openclaw-barnacle Bot added gateway Gateway runtime size: S labels Jul 9, 2026
@clawsweeper

clawsweeper Bot commented Jul 9, 2026 •

Copy link
Copy Markdown
Contributor

Codex review: needs maintainer review before merge. Reviewed July 9, 2026, 1:54 PM ET / 17:54 UTC.

Summary
Adds a once-only abort listener to registerQueuedChatTurn and regression coverage for direct abort cleanup, reused run IDs, retired collect identities, and queued-followup behavior.

PR surface: Source +11, Tests +75. Total +86 across 3 files.

Reproducibility: yes. source-level: current main stores the queued entry after checking only whether the signal is already aborted, and the caller passes the active run controller. I did not execute a current-main failing test in this read-only review.

Review metrics: none identified.

Root-cause cluster
Relationship: fixed_by_candidate
Canonical: #102914
Summary: This PR is a candidate fix for the canonical queued chat-turn abort cleanup leak; the older open PR overlaps the same root cause but is narrower and has weaker safeguards/proof.

Members:

Proposal only: this assessment does not dispatch repair, suppress jobs, mutate sibling items, close, or merge anything.

Merge readiness
Overall: 🐚 platinum hermit
Proof: 🐚 platinum hermit
Patch quality: 🦞 diamond lobster
Result: ready for maintainer review.

Overall follows the weaker of proof and patch quality, so missing proof can cap an otherwise strong patch.

Rank-up moves:

  • none.

Risk before merge

  • [P1] An older overlapping PR remains open for the same canonical issue; maintainers should choose one canonical fix so the narrower branch does not land later with weaker cleanup semantics.

Maintainer options:

  1. Decide the mitigation before merge
    Land this guarded helper-level cleanup as the canonical fix for missing abort event listener on AbortSignal in registerQueuedChatTurn causes memory leak #102914, then close or retire fix(gateway): add abort listener to clean up queued chat turn on client disconnect #102935 as superseded.
  2. Pause or close
    Do not merge this PR until maintainers decide whether the risk is worth taking.

Next step before merge

  • No automated repair is needed; maintainers should review this stronger candidate, choose it as the canonical fix or not, and retire the overlapping older PR accordingly.

Maintainer decision needed

  • Question: Should maintainers land this guarded PR as the canonical queued chat-turn abort cleanup fix and retire fix(gateway): add abort listener to clean up queued chat turn on client disconnect #102935?
  • Rationale: Two open PRs target the same leak, and this one appears technically stronger, but choosing which contributor branch to merge and how to retire the overlap is a maintainer review action.
  • Likely owner: steipete — This handle is tied to the queued lifecycle introduction and merge history for the affected Gateway path.
  • Options:
    • Land this guarded PR (recommended): Merge this PR after normal maintainer review, then close the older overlapping PR as superseded by the guarded implementation.
    • Land the older PR instead: Use the narrower older PR only if maintainers prefer the smaller patch and are comfortable handling reused-id and retired-collect safeguards separately.
    • Request a different boundary: Ask for changes if maintainers do not want registration-time cleanup to own this lifecycle invariant.

Security
Cleared: The diff changes Gateway in-memory cleanup logic and tests only; it does not alter dependencies, workflows, secrets, permissions, lockfiles, or external code execution.

Review details

Best possible solution:

Land this guarded helper-level cleanup as the canonical fix for #102914, then close or retire #102935 as superseded.

Do we have a high-confidence way to reproduce the issue?

Yes, source-level: current main stores the queued entry after checking only whether the signal is already aborted, and the caller passes the active run controller. I did not execute a current-main failing test in this read-only review.

Is this the best way to solve the issue?

Yes. Registering cleanup in registerQueuedChatTurn is the narrow owner-boundary fix because all queued entries enter through that helper, and this PR preserves reused-id and retired-collect invariants that the older overlapping PR does not cover.

AGENTS.md: found and applied where relevant.

Codex review notes: model internal, reasoning high; reviewed against 9f248969f6c8.

Label changes

Label justifications:

  • P2: The PR addresses a normal-priority Gateway in-memory resource leak with narrow runtime scope and no evidence of data loss, security bypass, crash loop, or unusable core runtime.
  • rating: 🐚 platinum hermit: Overall readiness is 🐚 platinum hermit; proof is 🐚 platinum hermit and patch quality is 🦞 diamond lobster.
  • status: 👀 ready for maintainer look: ClawSweeper has no concrete contributor-facing blocker left for this PR. Sufficient (terminal): The PR body includes terminal proof from the production queue module at the reviewed head showing registration, owner abort cleanup, reused-run-id protection, and retired-collect preservation.
  • proof: sufficient: Contributor real behavior proof is sufficient. The PR body includes terminal proof from the production queue module at the reviewed head showing registration, owner abort cleanup, reused-run-id protection, and retired-collect preservation.
Evidence reviewed

PR surface:

Source +11, Tests +75. Total +86 across 3 files.

View PR surface stats
Area Files Added Removed Net
Source 1 11 0 +11
Tests 2 75 0 +75
Docs 0 0 0 0
Config 0 0 0 0
Generated 0 0 0 0
Other 0 0 0 0
Total 3 86 0 +86

What I checked:

  • Repository policy read: Read the full root AGENTS.md and the scoped Gateway AGENTS.md; Gateway lifecycle and focused-test guidance shaped the review. (AGENTS.md:1, 9f248969f6c8)
  • Current main lacks future-abort cleanup: Current main rejects already-aborted controllers and stores the queued entry, but returns without registering a listener for a later owner abort. (src/gateway/chat-queued-turns.ts:41, 9f248969f6c8)
  • Caller passes the active run controller: chat.send passes activeRunAbort.controller into registerQueuedChatTurn and wires retirement/completion through the queued lifecycle, making the helper the narrow cleanup boundary. (src/gateway/server-methods/chat.ts:4831, 9f248969f6c8)
  • Queue lifecycle has separate retired/completed states: The followup lifecycle uses enqueued, cancellation-retired, and completed guards, so abort cleanup must not remove retired collect identities before aggregate completion. (src/auto-reply/reply/queue/types.ts:176, 9f248969f6c8)
  • PR adds guarded abort cleanup: The PR head adds a once-only abort listener that deletes only when the map still points at the same entry and the entry has not been retired from cancellation ownership. (src/gateway/chat-queued-turns.ts:66, 3aecbe6bf2e4)
  • PR tests cover risky invariants: The PR adds tests for direct controller abort cleanup, stale listeners after run-id reuse, retired collect identities surviving abort, and the queued-followup path. (src/gateway/chat-queued-turns.test.ts:37, 3aecbe6bf2e4)

Likely related people:

  • steipete: GitHub PR and commit metadata tie the queued prompt lifecycle, helper, caller wiring, and merge of the original feature to this handle. (role: introduced queued lifecycle behavior; confidence: high; commits: 1b8d83767445, fdd19e3017ea; files: src/gateway/chat-queued-turns.ts, src/gateway/server-methods/chat.ts, src/auto-reply/reply/queue/types.ts)
  • Sebastien Tardif: The merged queued prompt lifecycle commit lists this person as a co-author on the feature that introduced the helper and queue lifecycle wiring. (role: co-author on queued lifecycle work; confidence: medium; commits: 1b8d83767445; files: src/gateway/chat-queued-turns.ts, src/gateway/server-methods/chat.ts, src/auto-reply/reply/queue/types.ts)
  • vincentkoc: Path history shows recent merged cleanup touching src/gateway/chat-queued-turns.ts, though not the abort-listener behavior itself. (role: recent adjacent contributor; confidence: medium; commits: 17777b3a9ff1; files: src/gateway/chat-queued-turns.ts)
What the crustacean ranks mean
  • 🦀 challenger crab: rare, exceptional readiness with strong proof, clean implementation, and convincing validation.
  • 🦞 diamond lobster: very strong readiness with only minor maintainer review expected.
  • 🐚 platinum hermit: good normal PR, likely mergeable with ordinary maintainer review.
  • 🦐 gold shrimp: useful signal, but proof or patch confidence is still limited.
  • 🦪 silver shellfish: thin signal; proof, validation, or implementation needs work.
  • 🧂 unranked krab: not merge-ready because proof is missing/unusable or there are serious correctness or safety concerns.
  • 🌊 off-meta tidepool: rating does not apply to this item.

Shiny media proof means a screenshot, video, or linked artifact directly shows the changed behavior. Runtime, network, CSP, and security claims still need visible diagnostics.

How this review workflow works
  • ClawSweeper keeps one durable marker-backed review comment per issue or PR.
  • Re-runs edit this comment so the latest verdict, findings, and automation markers stay together instead of adding duplicate bot comments.
  • A fresh review can be triggered by eligible @clawsweeper re-review comments, exact-item GitHub events, scheduled/background review runs, or manual workflow dispatch.
  • PR/issue authors and users with repository write access can comment @clawsweeper re-review or @clawsweeper re-run on an open PR or issue to request a fresh review only.
  • Maintainers can also comment @clawsweeper review to request a fresh review only.
  • Fresh-review commands do not start repair, autofix, rebase, CI repair, or automerge.
  • Maintainer-only repair and merge flows require explicit commands such as @clawsweeper autofix, @clawsweeper automerge, @clawsweeper fix ci, or @clawsweeper address review.
  • Maintainers can comment @clawsweeper explain to ask for more context, or @clawsweeper stop to stop active automation.
Review history (3 earlier review cycles)
  • reviewed 2026-07-09T16:36:33.962Z sha 3aecbe6 :: needs real behavior proof before merge. :: none
  • reviewed 2026-07-09T17:32:13.294Z sha 3aecbe6 :: needs maintainer review before merge. :: none
  • reviewed 2026-07-09T17:47:09.722Z sha 3aecbe6 :: needs maintainer review before merge. :: none

@clawsweeper clawsweeper Bot added rating: 🦪 silver shellfish Thin PR readiness signal; proof, validation, or implementation needs work. status: 📣 needs proof The PR needs real behavior proof before ClawSweeper can clear the contributor ask. P2 Normal backlog priority with limited blast radius. labels Jul 9, 2026
@MonkeyLeeT

Copy link
Copy Markdown
Contributor Author

Added real Gateway queue behavior proof to the PR body.

Key output now included there:

PASS queued_turn_registered {"runId":"proof-owner-abort","mapSize":1,"signalAborted":false}
PASS owner_abort_fired_and_entry_removed {"runId":"proof-owner-abort","mapSize":0,"signalAborted":true}
PASS stale_abort_did_not_remove_reused_run_id {"runId":"proof-reused","mapSize":1,"currentSessionId":"new-session"}
PASS current_reused_owner_abort_removed_entry {"runId":"proof-reused","mapSize":0}
PASS retired_collect_identity_survived_abort {"runId":"proof-retired","mapSize":1,"abortable":false}
PASS retired_collect_identity_removed_on_completion {"runId":"proof-retired","mapSize":0}
REAL_GATEWAY_QUEUE_PROOF result=pass

This was run against PR head 3aecbe6bf2e443db0cff0ab4df473293dbb3fe80 by importing the production Gateway queue module and exercising the runtime queue map directly. It shows queued registration, owner abort firing, entry cleanup, reused-run-id protection, and collect-retirement preservation until completion.

@clawsweeper re-review

@clawsweeper

clawsweeper Bot commented Jul 9, 2026 •

Copy link
Copy Markdown
Contributor

🦞🧹
ClawSweeper re-review requested.

I asked ClawSweeper to review this item again.
Action: item re-review queued (workflow sweep.yml, event repository_dispatch).
Result: the existing ClawSweeper review comment will be edited in place when the review finishes.

@clawsweeper clawsweeper Bot added proof: sufficient ClawSweeper judged the real behavior proof convincing. rating: 🐚 platinum hermit Good normal PR readiness with ordinary maintainer review expected. status: 👀 ready for maintainer look ClawSweeper has no concrete contributor-facing blocker left for this PR. and removed rating: 🦪 silver shellfish Thin PR readiness signal; proof, validation, or implementation needs work. status: 📣 needs proof The PR needs real behavior proof before ClawSweeper can clear the contributor ask. labels Jul 9, 2026
@MonkeyLeeT
MonkeyLeeT marked this pull request as ready for review July 9, 2026 17:39
@steipete steipete self-assigned this Jul 10, 2026
@steipete
steipete requested review from a team as code owners July 10, 2026 00:47
@openclaw-barnacle openclaw-barnacle Bot added docs Improvements or additions to documentation channel: discord Channel integration: discord channel: mattermost Channel integration: mattermost channel: nextcloud-talk Channel integration: nextcloud-talk channel: telegram Channel integration: telegram channel: tlon Channel integration: tlon channel: voice-call Channel integration: voice-call app: android App: android app: ios App: ios app: macos App: macos app: web-ui App: web-ui labels Jul 10, 2026
@github-actions

Copy link
Copy Markdown
Contributor

Dependency Guard

This PR changes dependency-related files. Maintainers should confirm these changes are intentional.

Changed files:

  • extensions/codex/npm-shrinkwrap.json
  • extensions/codex/package.json
  • extensions/meta/npm-shrinkwrap.json
  • extensions/meta/package.json
  • pnpm-lock.yaml

Maintainer follow-up:

  • Review whether the dependency changes are intentional.
  • Inspect resolved package deltas when lockfile, shrinkwrap, or workspace dependency policy changes are present.
  • Treat package-lock.json and npm-shrinkwrap.json diffs as security-review surfaces.
  • Run pnpm deps:changes:report -- --base-ref origin/main --markdown /tmp/dependency-changes.md --json /tmp/dependency-changes.json locally for detailed release-style evidence.

@github-actions

Copy link
Copy Markdown
Contributor

Dependency graph changes are blocked

OpenClaw does not accept dependency graph changes through PRs unless a repository admin or security explicitly authorizes the current head SHA. Dependency updates are generated internally by maintainers so external PRs cannot change the resolved graph.

Detected dependency graph changes:

  • extensions/codex/npm-shrinkwrap.json changed.
  • extensions/meta/npm-shrinkwrap.json changed.
  • pnpm-lock.yaml changed.
  • extensions/codex/package.json changed dependencies.
  • extensions/meta/package.json changed devDependencies, name, version.

Auto-scrub was not attempted because this PR changes package manifest dependency graph fields:

  • extensions/codex/package.json changed dependencies.
  • extensions/meta/package.json changed devDependencies, name, version.

Dependency graph changes must be reviewed by security or handled by maintainers internally. Please remove lockfile changes manually if they are not needed.

To remove lockfile changes, restore them from the target branch:

git fetch origin
git checkout 'origin/main' -- 'extensions/codex/npm-shrinkwrap.json' 'extensions/meta/npm-shrinkwrap.json' 'pnpm-lock.yaml'
git commit -m 'chore: remove dependency lockfile change'
git push

If this PR intentionally needs a dependency graph change, ask a repository admin or member of @openclaw/openclaw-secops to comment:

/allow-dependencies-change

The action will approve the current head SHA (127bc6ae02c7a487430ffda5996cf8bff6bd8d71) when it reruns. A later push requires a fresh approval.

@steipete

Copy link
Copy Markdown
Contributor

Thanks @MonkeyLeeT for the diagnosis and initial regression work. The repaired, clean-history version landed in #103194 as 0d2db46956d, with your co-authorship preserved.

The landed rewrite also binds completion, collect retirement, and abort cleanup to the exact queued-turn controller/entry, so delayed callbacks cannot delete a replacement that reused the same run ID. The fork branch's ancestry became unsuitable for a safe merge during maintainer synchronization, so the final patch was rebuilt on openclaw/openclaw instead of landing the contaminated stack.

Proof: exact-head hosted CI 29061297665 green; 160 focused Gateway tests and path-scoped gates green on Testbox; three live TUI/Gateway cancellation/collect scenarios green; post-merge current-main focused tests and gates green.

Closing as superseded by the credited landed rewrite. Thank you.

@steipete steipete closed this Jul 10, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

agents Agent runtime and tooling app: android App: android app: ios App: ios app: macos App: macos app: web-ui App: web-ui channel: discord Channel integration: discord channel: mattermost Channel integration: mattermost channel: nextcloud-talk Channel integration: nextcloud-talk channel: qqbot channel: sms Channel integration: sms channel: telegram Channel integration: telegram channel: tlon Channel integration: tlon channel: twitch Channel integration: twitch channel: voice-call Channel integration: voice-call cli CLI command changes commands Command implementations dependencies-changed PR changes dependency-related files docker Docker and sandbox tooling docs Improvements or additions to documentation extensions: anthropic extensions: anthropic-vertex extensions: codex extensions: codex-supervisor Extension: codex-supervisor extensions: copilot extensions: device-pair extensions: meta Extension: meta extensions: minimax extensions: nvidia extensions: openrouter extensions: qa-lab gateway Gateway runtime P2 Normal backlog priority with limited blast radius. plugin: file-transfer proof: sufficient ClawSweeper judged the real behavior proof convincing. rating: 🐚 platinum hermit Good normal PR readiness with ordinary maintainer review expected. scripts Repository scripts size: XL status: 👀 ready for maintainer look ClawSweeper has no concrete contributor-facing blocker left for this PR.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

missing abort event listener on AbortSignal in registerQueuedChatTurn causes memory leak

2 participants