You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Propagate cancellation through Responses stream conversion - #1762
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.
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What
Forward cancellation of
chatCompletionsStreamto the upstream Responses reader, including the cancellation reason. Keep reader ownership in each conversion entry point and release it infinally; 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
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.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.