-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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
bugfix: Fix eventstream partial read #3165
Conversation
…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.
- 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.
|
Hi @wilsones-berkeley, thank you so much for this contribution. The fix you have proposed is totally valid! |
stobrien89
left a comment
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.
Just one test change requested, and some new files needing a newline appended. Otherwise looks good!
tests/Api/Parser/NonSeekableStreamDecodingEventStreamIteratorTest.php
Outdated
Show resolved
Hide resolved
Co-authored-by: Sean O'Brien <[email protected]>
stobrien89
left a comment
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.
Still seeing the newline warnings I commented on before. Otherwise, looks good- thanks for the contribution @wilsones-berkeley!
38e5362 to
a6839ac
Compare
|
Running integ tets: Running smoke tests: |
Add empty lines at the end of file.
a6839ac to
4150dd3
Compare
Issue #, if available
#3110
Description of changes:
This pull request fixes a bug in
NonSeekableStreamDecodingEventStreamIteratorwherereadAndHashBytes()assumed that the underlying stream would always return the full number of requested bytes in a singleread() 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:A new unit test
testReadAndHashBytesHandlesPartialReadshas been added to verify this behavior by simulating a non-seekable stream that returns partial data in eachread()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.