Skip to content

Conversation

@wilsones-berkeley
Copy link
Contributor

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
- Add mocked events with proper headers and payload in order to test events that carry checksum calculation and make sure the whole processing works as expected.
@yenfryherrerafeliz
Copy link
Contributor

yenfryherrerafeliz commented Nov 28, 2025

Hi @wilsones-berkeley, thank you so much for this contribution. The fix you have proposed is totally valid!
I took the freedom of modifying the test case so we can test this change against events with the same structure we process them.

Copy link
Member

@stobrien89 stobrien89 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just one test change requested, and some new files needing a newline appended. Otherwise looks good!

Co-authored-by: Sean O'Brien <[email protected]>
Copy link
Member

@stobrien89 stobrien89 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Still seeing the newline warnings I commented on before. Otherwise, looks good- thanks for the contribution @wilsones-berkeley!

@yenfryherrerafeliz yenfryherrerafeliz force-pushed the fix-eventstream-partial-read branch from 38e5362 to a6839ac Compare December 1, 2025 17:58
@yenfryherrerafeliz
Copy link
Contributor

Running integ tets:

vendor/bin/behat --format=progress --tags=integ
...................................................................... 70
...................................................................... 140
...................................................................... 210
......................................

66 scenarios (66 passed)
248 steps (248 passed)
10m51.03s (49.07Mb)

Running smoke tests:

vendor/bin/behat --format=progress --suite=smoke --tags='~@noassumerole'
...................................................................... 70
...................................................................... 140
...................................................................... 210
............

111 scenarios (111 passed)
222 steps (222 passed)
0m35.39s (95.98Mb)

Add empty lines at the end of file.
@yenfryherrerafeliz yenfryherrerafeliz force-pushed the fix-eventstream-partial-read branch from a6839ac to 4150dd3 Compare December 1, 2025 18:27
@yenfryherrerafeliz yenfryherrerafeliz merged commit 8b85a28 into aws:master Dec 1, 2025
10 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants