Skip to content

fix(gateway): add abort listener to clean up queued chat turn on client disconnect - #102935

Closed
wm0018 wants to merge 1 commit into
openclaw:mainfrom
wm0018:fix/chat-queued-turns-abort-listener-cleanup
Closed

wm0018 wants to merge 1 commit into
openclaw:mainfrom
wm0018:fix/chat-queued-turns-abort-listener-cleanup

Conversation

@wm0018

@wm0018 wm0018 commented Jul 9, 2026 •

Copy link
Copy Markdown
Contributor

What Problem This Solves

Fixes #102914 — registerQueuedChatTurn did not register an abort listener on the AbortSignal. If a client disconnected and the followup never reached the queue drainer, the entry would leak in chatQueuedTurns until gateway restart.

Why This Change Was Made

The drain path (drain.ts) normally calls completeQueuedChatTurn via the lifecycle callback when it detects an aborted signal. However, there is a window between registration and drain admission where a direct client-disconnect abort can fire without anyone checking the map. Registering a { once: true } abort listener on the controller ensures the entry is cleaned up regardless of the drain state. The listener guards against removing retired collect identities (abortable: false) so idempotency protection is preserved.

The pattern matches existing defensive abort-listener usage elsewhere in the codebase (command-queue.ts:350, process/exec.ts:614, channel-lifecycle.core.ts:116).

User Impact

Prevents a slow memory leak in the gateway when clients disconnect while queued chat turns are pending, without breaking idempotency for retired collect-mode entries.

Evidence

Real Gateway queue behavior proof

Run against the production src/gateway/chat-queued-turns.ts module:

REAL_GATEWAY_QUEUE_PROOF commit=... 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 (src/gateway/chat-queued-turns.proof.ts) exercises the production module:

  • Registers a queued turn → owner abort fires → entry removed
  • Stale controller abort does not remove a re-registered run id
  • Retired collect identity survives abort (idempotency guard), removed only by completion

Unit test results

40 tests pass across 4 gateway configs (gateway-core, gateway-server, gateway-client, gateway-methods), including the new tests:

 ✓ cleans up the map entry when the abort signal fires (client disconnect)
 ✓ keeps a retired collect identity after abort (idempotency guard)

@clawsweeper

clawsweeper Bot commented Jul 9, 2026 •

Copy link
Copy Markdown
Contributor

Codex review: needs real behavior proof before merge. Reviewed July 9, 2026, 11:26 AM ET / 15:26 UTC.

Summary
The branch adds a once-only abort listener in registerQueuedChatTurn to delete the queued-turn map entry on client disconnect and adds a regression test for direct controller abort cleanup.

PR surface: Source +10, Tests +15. Total +25 across 2 files.

Reproducibility: yes. Source inspection shows current main stores the queued entry and only rejects already-aborted signals; aborting the controller after registration has no current-main listener that deletes the map entry.

Review metrics: none identified.

Root-cause cluster
Relationship: fixed_by_candidate
Canonical: #102914
Summary: This PR is the candidate fix for the canonical queued chat-turn abort cleanup leak report.

Members:

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

Merge readiness
Overall: 🦪 silver shellfish
Proof: 🦪 silver shellfish
Patch quality: 🐚 platinum hermit
Result: blocked until real behavior proof from a real setup is added.

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

Rank-up moves:

  • [P1] Add real behavior proof, such as a redacted gateway run/log or terminal transcript showing a queued chat turn registered, the client-disconnect abort firing, and the queued map no longer retaining that run.

Proof guidance:

  • [P1] Needs real behavior proof before merge: The PR body provides code and unit-test snippets only; it does not show an after-fix live gateway run, log, terminal output, or transcript proving queued-turn cleanup in a real setup. After adding proof, update the PR body; ClawSweeper should re-review automatically. If it does not, the PR author or someone with repository write access can comment @clawsweeper re-review.

Risk before merge

  • [P1] The PR body provides unit-test evidence only; maintainers still need real gateway behavior proof before merging this external PR.

Maintainer options:

  1. Decide the mitigation before merge
    Land this focused helper-level cleanup after the contributor adds real gateway behavior proof and maintainers refresh exact-head CI; keep missing abort event listener on AbortSignal in registerQueuedChatTurn causes memory leak #102914 as the canonical issue until merge.
  2. Pause or close
    Do not merge this PR until maintainers decide whether the risk is worth taking.

Next step before merge

  • [P1] Contributor needs to add real gateway behavior proof before maintainers can merge; no automated code repair is indicated for the current diff.

Security
Cleared: The diff only changes TypeScript gateway cleanup logic and a colocated test; it does not alter dependencies, secrets, permissions, workflows, lockfiles, or external code execution.

Review details

Best possible solution:

Land this focused helper-level cleanup after the contributor adds real gateway behavior proof and maintainers refresh exact-head CI; keep #102914 as the canonical issue until merge.

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

Yes. Source inspection shows current main stores the queued entry and only rejects already-aborted signals; aborting the controller after registration has no current-main listener that deletes the map entry.

Is this the best way to solve the issue?

Yes. Adding cleanup at registerQueuedChatTurn is the narrowest owner-boundary fix because that helper owns admission into chatQueuedTurns, while existing lifecycle completion paths already handle drain-side cleanup.

AGENTS.md: found and applied where relevant.

Codex review notes: model internal, reasoning high; reviewed against 436ed5f3085b.

Label changes

Label changes:

  • add P2: The PR addresses a normal-priority gateway runtime resource leak with a narrow fix and no evidence of data loss, security bypass, crash loop, or unusable runtime.
  • add rating: 🦪 silver shellfish: Overall readiness is 🦪 silver shellfish; proof is 🦪 silver shellfish and patch quality is 🐚 platinum hermit.
  • add status: 📣 needs proof: The PR needs real behavior proof before ClawSweeper can clear the contributor ask. Needs real behavior proof before merge: The PR body provides code and unit-test snippets only; it does not show an after-fix live gateway run, log, terminal output, or transcript proving queued-turn cleanup in a real setup. After adding proof, update the PR body; ClawSweeper should re-review automatically. If it does not, the PR author or someone with repository write access can comment @clawsweeper re-review.

Label justifications:

  • P2: The PR addresses a normal-priority gateway runtime resource leak with a narrow fix and no evidence of data loss, security bypass, crash loop, or unusable runtime.
  • rating: 🦪 silver shellfish: Overall readiness is 🦪 silver shellfish; proof is 🦪 silver shellfish and patch quality is 🐚 platinum hermit.
  • status: 📣 needs proof: The PR needs real behavior proof before ClawSweeper can clear the contributor ask. Needs real behavior proof before merge: The PR body provides code and unit-test snippets only; it does not show an after-fix live gateway run, log, terminal output, or transcript proving queued-turn cleanup in a real setup. After adding proof, update the PR body; ClawSweeper should re-review automatically. If it does not, the PR author or someone with repository write access can comment @clawsweeper re-review.
Evidence reviewed

PR surface:

Source +10, Tests +15. Total +25 across 2 files.

View PR surface stats
Area Files Added Removed Net
Source 1 10 0 +10
Tests 1 15 0 +15
Docs 0 0 0 0
Config 0 0 0 0
Generated 0 0 0 0
Other 0 0 0 0
Total 2 25 0 +25

What I checked:

Likely related people:

  • steipete: GitHub PR and commit metadata tie the current queued-turn lifecycle, helper, caller, queue drain, and tests to fix(tui): queue prompts while the agent is busy #100123. (role: introduced queued prompt lifecycle; confidence: high; commits: 1b8d83767445; files: src/gateway/chat-queued-turns.ts, src/gateway/server-methods/chat.ts, src/auto-reply/reply/queue/types.ts)
  • vincentkoc: Recent GitHub path history shows a merged cleanup touching src/gateway/chat-queued-turns.ts, but only for dead helper removal rather than the abort lifecycle behavior. (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.

@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
@wm0018
wm0018 force-pushed the fix/chat-queued-turns-abort-listener-cleanup branch from 4d085bf to b3efa7b Compare July 10, 2026 00:26
@wm0018

wm0018 commented Jul 10, 2026

Copy link
Copy Markdown
Contributor Author

@clawsweeper re-review

@clawsweeper

clawsweeper Bot commented Jul 10, 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.

@steipete

Copy link
Copy Markdown
Contributor

Superseded by #103194, landed as 0d2db46956d.

The landed owner-layer fix includes the abort listener plus exact controller/entry guards for completion, collect retirement, and synchronous replacement during single/bulk abort cleanup. Those identity checks are required to prevent a delayed callback from deleting a newer queued turn that reused the same run ID.

Exact-head hosted CI, 160 focused Gateway tests, live TUI cancellation/collect scenarios, and post-merge current-main gates are green. Closing this narrower duplicate in favor of the canonical implementation.

@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

gateway Gateway runtime P2 Normal backlog priority with limited blast radius. rating: 🦪 silver shellfish Thin PR readiness signal; proof, validation, or implementation needs work. size: XS status: 📣 needs proof The PR needs real behavior proof before ClawSweeper can clear the contributor ask.

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