Repository navigation
fix: swallow reader.cancel rejection when stream source already errored #627
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
mattapperson
wants to merge
2
commits into
main
Choose a base branch
from
claude/fix-stream-cancel-abort-error
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| --- | ||
| "@openrouter/ai-sdk-provider": patch | ||
| --- | ||
|
|
||
| Fix unhandled `AbortError` rejection when a streaming response is cancelled after its source has already errored (e.g. an aborted fetch) | ||
|
|
||
| Also stop reporting a spurious `Controller is already closed` error through the stream error handler when a stream is cancelled while a read is in flight. |
150 changes: 150 additions & 0 deletions
150
e2e/issues/issue-615-625-stream-cancel-after-abort.test.ts
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,150 @@ | ||
| /** | ||
| * Regression test for GitHub issues #615 and #625 | ||
| * https://github.com/OpenRouterTeam/ai-sdk-provider/issues/615 | ||
| * https://github.com/OpenRouterTeam/ai-sdk-provider/issues/625 | ||
| * | ||
| * Reported error: Unhandled AbortError when a cancelled stream has already | ||
| * errored ("Stopping a chat can crash the process with an unhandled | ||
| * `AbortError`"). | ||
| * Affected: 3.1.0, doStream on chat models | ||
| * | ||
| * This test verifies that cancelling a doStream() result after the HTTP | ||
| * response body has errored with an AbortError does not produce an | ||
| * unhandledRejection, for both chat and completion models and for a range of | ||
| * buffered-but-unread SSE chunk depths. Fully offline: a custom `fetch` | ||
| * returns a Response whose body is controlled by the test. | ||
| */ | ||
| import type { | ||
| LanguageModelV4Prompt, | ||
| LanguageModelV4StreamPart, | ||
| } from '@ai-sdk/provider'; | ||
|
|
||
| import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'; | ||
| import { createOpenRouter } from '@/src'; | ||
|
|
||
| vi.mock('@/src/version', () => ({ | ||
| VERSION: '0.0.0-test', | ||
| })); | ||
|
|
||
| const TEST_PROMPT: LanguageModelV4Prompt = [ | ||
| { role: 'user', content: [{ type: 'text', text: 'Hello' }] }, | ||
| ]; | ||
|
|
||
| const encoder = new TextEncoder(); | ||
|
|
||
| function chatChunk(i: number): string { | ||
| return `data: ${JSON.stringify({ | ||
| id: 'test-1', | ||
| object: 'chat.completion.chunk', | ||
| created: 1234567890, | ||
| model: 'openai/gpt-4o-mini', | ||
| choices: [ | ||
| { index: 0, delta: { content: `token-${i}` }, finish_reason: null }, | ||
| ], | ||
| })}\n\n`; | ||
| } | ||
|
|
||
| function completionChunk(i: number): string { | ||
| return `data: ${JSON.stringify({ | ||
| id: 'test-1', | ||
| object: 'text_completion', | ||
| created: 1234567890, | ||
| model: 'openai/gpt-4o-mini', | ||
| choices: [{ index: 0, text: `token-${i}`, finish_reason: null }], | ||
| })}\n\n`; | ||
| } | ||
|
|
||
| function createControlledFetch() { | ||
| let bodyController!: ReadableStreamDefaultController<Uint8Array>; | ||
| const body = new ReadableStream<Uint8Array>({ | ||
| start(controller) { | ||
| bodyController = controller; | ||
| }, | ||
| }); | ||
| const fetch = vi.fn( | ||
| async () => | ||
| new Response(body, { | ||
| status: 200, | ||
| headers: { 'content-type': 'text/event-stream' }, | ||
| }), | ||
| ); | ||
| return { | ||
| fetch: fetch as unknown as typeof globalThis.fetch, | ||
| get body() { | ||
| return bodyController; | ||
| }, | ||
| }; | ||
| } | ||
|
|
||
| const tick = () => new Promise((resolve) => setTimeout(resolve, 0)); | ||
|
|
||
| describe('Issue #615/#625: cancelling a stream whose source already errored', () => { | ||
| let unhandled: unknown[]; | ||
| const onUnhandled = (reason: unknown) => { | ||
| unhandled.push(reason); | ||
| }; | ||
|
|
||
| beforeEach(() => { | ||
| unhandled = []; | ||
| process.on('unhandledRejection', onUnhandled); | ||
| }); | ||
|
|
||
| afterEach(() => { | ||
| process.off('unhandledRejection', onUnhandled); | ||
| }); | ||
|
|
||
| const cases = [ | ||
| { | ||
| name: 'chat', | ||
| chunk: chatChunk, | ||
| start: (provider: ReturnType<typeof createOpenRouter>) => | ||
| provider.chat('openai/gpt-4o-mini').doStream({ prompt: TEST_PROMPT }), | ||
| }, | ||
| { | ||
| name: 'completion', | ||
| chunk: completionChunk, | ||
| start: (provider: ReturnType<typeof createOpenRouter>) => | ||
| provider | ||
| .completion('openai/gpt-4o-mini') | ||
| .doStream({ prompt: TEST_PROMPT }), | ||
| }, | ||
| ] as const; | ||
|
|
||
| for (const { name, chunk, start } of cases) { | ||
| for (let queued = 0; queued <= 6; queued++) { | ||
| it(`${name}: no unhandledRejection with ${queued} unread chunk(s) queued`, async () => { | ||
| const controlled = createControlledFetch(); | ||
| const provider = createOpenRouter({ | ||
| apiKey: 'test-api-key', | ||
| fetch: controlled.fetch, | ||
| }); | ||
|
|
||
| const { stream } = await start(provider); | ||
| const typedStream: ReadableStream<LanguageModelV4StreamPart> = stream; | ||
|
|
||
| for (let i = 0; i < queued; i++) { | ||
| controlled.body.enqueue(encoder.encode(chunk(i))); | ||
| } | ||
| // Let the pipeline pull the queued chunks into its internal buffers. | ||
| await tick(); | ||
| await tick(); | ||
|
|
||
| const abortError = new DOMException( | ||
| 'This operation was aborted', | ||
| 'AbortError', | ||
| ); | ||
| controlled.body.error(abortError); | ||
| // Let the error propagate through the pipe chain so the source the | ||
| // provider wraps is already errored when the consumer cancels. | ||
| await tick(); | ||
| await tick(); | ||
| await typedStream.cancel(abortError); | ||
|
|
||
| // Give Node a chance to report any unhandled rejection. | ||
| await new Promise((resolve) => setTimeout(resolve, 20)); | ||
|
|
||
| expect(unhandled).toEqual([]); | ||
| }); | ||
| } | ||
| } | ||
| }); |
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
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
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[nit] do we want the issue numbers still in the comment here?