Skip to content

bugfix: Fix eventstream partial read #3165

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
wants to merge 3 commits into
base: master
Choose a base branch
from

Conversation

wilsones-berkeley
Copy link

Issue #, if available
#3110

Description of changes:
This pull request fixes a bug in NonSeekableStreamDecodingEventStreamIterator where readAndHashBytes() assumed that the underlying stream would always return the full number of requested bytes in a single read() call.

In cases where the stream returns fewer bytes (e.g., partial reads from network streams or large payloads), this could result in truncated payloads and CRC mismatches.

The fix updates readAndHashBytes() to:

  • Continue reading from the stream until the expected number of bytes have been read or the stream reaches EOF.
  • Prevent infinite loops on unexpected empty reads.

A new unit test testReadAndHashBytesHandlesPartialReads has been added to verify this behavior by simulating a non-seekable stream that returns partial data in each read() call.

All tests pass with make test.

By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.

…eamIterator

Previously, readAndHashBytes() assumed that a single call to $stream->read($num) would return the full payload length. However, PSR-7 allows partial reads, and this behavior occurs in practice with non-seekable streams (e.g., cURL).

This caused CRC mismatches and truncated payloads in streamed responses, especially for large final chunks, such as those returned by Bedrock's RetrieveAndGenerateStream API.

This change updates readAndHashBytes() to read in a loop until all expected bytes are received or EOF is reached, ensuring proper CRC calculation and full payload integrity.
@wilsones-berkeley wilsones-berkeley changed the title Fix eventstream partial read bugfix: Fix eventstream partial read Aug 12, 2025
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