Skip to content

[PR] fix: cancel discarded compress-loop response bodies - #6630

Merged
ranxianglei merged 1 commit into
ranxianglei:masterfrom
Afloat16:2026-11-13_loop-stream-cleanup
Nov 14, 2026
Merged

ranxianglei merged 1 commit into
ranxianglei:masterfrom
Afloat16:2026-11-13_loop-stream-cleanup

Conversation

@Afloat16

Copy link
Copy Markdown
Contributor

Fixes #6628

Problem

The compression loop owns every initial, retry, and continuation response, but currently only clears its fetch timers. Adapters release their readers when parsing exits, leaving an unread response body open.

This happens without a client disconnect: an upstream can send an in-band OpenAI error and keep its HTTP response open. The loop successfully retries the logical request, yet the discarded first response stays alive. Returning after a visible chunk also leaves the initial or continuation response open. Real loopback HTTP tests reproduce all three cases on the base.

Change

Cancel the current response body after each parse attempt exits, before any retry replaces it. Also cancel in the outer finally so consumer return and parser exceptions release the current response.

Cancellation is best effort and is not awaited: a raw-recording tee may still own its other branch, which must remain independently readable. Closed streams already have no unread work; the normal-completion control confirms that their underlying cancel callback is not invoked. Cleanup rejection does not replace the caller's result or the original parsing error.

The two ownership points add no per-event generator layer and leave adapter parsing, logical request bytes, retry eligibility, signed payloads, and tool execution unchanged.

Validation

  • 12 new cases: unmodified master e1f31ad gives 11 failures / 1 passing normal-completion control; the patch gives 12 passes.
  • All five wire adapters release unread bodies when the loop consumer returns, without retaining the reader lock or cancelling the underlying source twice.
  • Three real HTTP cases verify closure after an in-band error/retry, an initial-response return, and a return during an acp_status continuation. The retry still sends the same logical request and delivers its recovered answer.
  • A retained tee sibling stays readable and does not block loop cleanup; cancellation rejection and parser error identity are covered.
  • 73 targeted tests pass across cleanup, compression-loop, truncation, abort-lifetime, raw-disconnect, Anthropic signed-thinking/retry, and Gemini signature-replay suites.
  • Typecheck, production build, unused-export and test-import guards, and git diff --check pass.

The Codex prerequisite check finds the installed CLI and built artifact, but the configured real upstream at 127.0.0.1:8199 is unreachable. The gated live four-phase suite was not run. The HTTP tests above use a real local upstream with the production loop; full repository regression remains hosted CI's responsibility.

Prior work

All-state repository and author-history searches distinguish this ownership gap from #6051 (raw pipe paths) and #6571 (the inline fetch bridge). Neither changes runCompressLoop, and the new in-band-error reproduction does not require client cancellation. This PR changes only the loop's ownership of response bodies it has finished using.

@ranxianglei ranxianglei changed the title fix: cancel discarded compress-loop response bodies [PR] fix: cancel discarded compress-loop response bodies Nov 13, 2026
@ranxianglei
ranxianglei merged commit 66d5a20 into ranxianglei:master Nov 14, 2026
88 of 92 checks passed
@ranxianglei

Copy link
Copy Markdown
Owner

Superseded by #6677 (merged as 3858b6956, 2026-11-14).

Landed into master via the real merge commit 66d5a202544 (Merge Afloat16/2026-11-13_loop-stream-cleanup (PR #6630)), so your original commits are preserved verbatim in master history — only hashes of the batch wrapper changed, your authorship and commit messages are intact.

The fix for #6628 is now in master. Closing this PR as superseded — thanks for the careful, well-tested fix.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Compress loop leaves discarded upstream response bodies open

2 participants