Skip to content

Conversation

@teunlao
Copy link
Contributor

@teunlao teunlao commented Oct 27, 2025

Background

convertAsyncIteratorToReadableStream didn't notify its source when a consumer called cancel(). This left async generators running even after the stream was cancelled.

This affects any code that adapts convertAsyncIteratorToReadableStream, e.g. LlamaIndex adapter

Manual Verification

Checked a real streaming flow (OpenAI via LlamaIndex): read a few chunks, then cancel. Next read returns done=true and no further chunks are produced.

Summary

  • Fixed convertAsyncIteratorToReadableStream to properly call iterator.return() when the stream is cancelled
  • Added unit tests

Checklist

  • Tests have been added / updated (for bug fixes / features)
  • A patch changeset for relevant packages has been added
  • I have reviewed this pull request (self-review)

@lgrammel lgrammel changed the title Make ReadableStream.cancel properly finalize async iterators fix(provider-utils): make ReadableStream.cancel() properly finalize async iterators Oct 29, 2025
@lgrammel lgrammel merged commit 016b111 into vercel:main Oct 29, 2025
22 of 23 checks passed
vercel-ai-sdk bot pushed a commit that referenced this pull request Oct 29, 2025
…sync iterators (#9834)

## Background

`convertAsyncIteratorToReadableStream` didn't notify its source when a
consumer called `cancel()`. This left async generators running even
after the stream was cancelled.

This affects any code that adapts
`convertAsyncIteratorToReadableStream`, e.g. LlamaIndex adapter

## Manual Verification
Checked a real streaming flow (OpenAI via LlamaIndex): read a few
chunks, then cancel. Next read returns done=true and no further chunks
are produced.

## Summary

- Fixed `convertAsyncIteratorToReadableStream` to properly call
`iterator.return()` when the stream is cancelled
- Added unit tests
@vercel-ai-sdk vercel-ai-sdk bot removed the backport label Oct 29, 2025
@vercel-ai-sdk
Copy link
Contributor

vercel-ai-sdk bot commented Oct 29, 2025

✅ Backport PR created: #9881

vercel-ai-sdk bot added a commit that referenced this pull request Oct 29, 2025
…finalize async iterators (#9881)

This is an automated backport of #9834 to the release-v5.0 branch.

Co-authored-by: Dmitriy Osipov <75555523+teunlao@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants