Skip to content

Propagate cancellation through Responses stream conversion - #1762

Open
VIOLETee0 wants to merge 1 commit into
CodebuffAI:mainfrom
VIOLETee0:fix/responses-stream-cancellation
Open

VIOLETee0 wants to merge 1 commit into
CodebuffAI:mainfrom
VIOLETee0:fix/responses-stream-cancellation

Conversation

@VIOLETee0

Copy link
Copy Markdown

What

Forward cancellation of chatCompletionsStream to the upstream Responses reader, including the cancellation reason. Keep reader ownership in each conversion entry point and release it in finally; suppress further output after cancellation.

Why

The conversion starts reading immediately, but its output stream has no cancellation handler. If a consumer cancels while the next upstream read is pending, that read stays pending and the upstream body remains locked. This breaks cancellation propagation at the Responses-to-Chat-Completions boundary.

How verified

  • Added one regression using real Web Streams, with a causal handshake for the pending read and no sleeps or module mocks. It checks cancellation once with the identical reason, reader release, and the closed output.
  • On unchanged main 0239a27: 49 passed, 1 failed (the new regression). With the fix: 50 passed, 0 failed, 378 assertions across the Responses, ChatGPT model, ChatGPT, and BYOK provider compatibility files.
  • SDK typecheck, frozen-lockfile install, SDK build including declarations, Windows Freebuff binary build, version, and binary smoke passed with Bun 1.3.14.
  • Prettier passed. ESLint reported 0 errors and 2 local TypeScript resolver warnings in the new test.
  • A local HTTP SSE probe on native Node 24.20.0 confirmed connection closure and reader release after cancellation, plus normal completion.

Scope and tradeoffs

This changes the conversion boundary only. It does not add retries, alter SSE framing, or change authentication. In a separate Windows Bun 1.3.14 control against a native Node server, neither raw fetch-body cancellation nor converted-body cancellation immediately closed the socket; both released their reader. This patch restores cancellation propagation and does not claim to fix that transport behavior. Linux/macOS, live providers, and the full monorepo suite were not run.

@codebuff-team

Copy link
Copy Markdown
Contributor

Good find and good execution. The core bug is real: chatCompletionsStream's start() reads body internally via readServerSentEvents, but the returned ReadableStream had no cancel handler, so cancelling the consumer-facing stream never propagated to the upstream reader — the pending reader.read() stayed in flight and the upstream body stayed locked.

The fix is minimal and sits at the right layer: readServerSentEvents now takes an already-acquired reader instead of a body, and each entry point (chatCompletionsStream, chatCompletionsJson) owns reader creation/release itself. chatCompletionsStream adds a cancel(reason) that forwards to reader.cancel(reason) while still mid-read, guards further events.read() calls with a cancelled flag, and skips the [DONE]/controller.close() path once cancelled. The reading flag correctly avoids calling reader.cancel() after the reader has already been released in the finally block — that ordering is right per the Streams spec (reader release is idempotent, so the redundant reader.releaseLock() in finally after reader.cancel() is harmless).

The new test is exactly the kind this repo seems to want: real Web Streams, a causal handshake via the pull() counter instead of a sleep, and assertions on cancellation reason identity, reader release, and output closure — not an AI-shaped happy-path check. It reproduces the bug on main (1 failing) and passes with the fix.

One thing worth adding: chatCompletionsJson's reader-ownership refactor has no dedicated regression test, even though it's touched by the same diff. Given its happy path presumably passes already, a cancellation/early-termination test there (if applicable) would round this out, but it's not blocking. Scope is tight, no forbidden paths touched.

@codebuff-team codebuff-team added bot:triaged Classified by the community triage bot pr:port-candidate Worth porting into the private source tree labels Nov 29, 2026

This branch has not been deployed

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

Labels

bot:triaged Classified by the community triage bot pr:port-candidate Worth porting into the private source tree

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants