[release/6.0] Do not flush serializer buffers before first IAsyncEnumerable element is fetched #59865
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.
Backport of #59808 to release/6.0
/cc @eiriktsarpalis
Customer Impact
The current STJ implementation of
IAsyncEnumerable<T>
serialization will attempt to flush the serializer's internal buffer to the underlying stream every time anIAsyncEnumerator.MoveNextAsync()
call yields a running task. This is by design behavior, intended to echo the chunking strategy employed by the IAsyncEnumerable source.Note however that many
IAsyncEnumerable<T>
implementations that wrap over network connections will delay establishing a connection until the firstIAsyncEnumerator.MoveNextAsync()
has been made. Thus in scenaria where establishing a connection has failed, the serializer will have already flushed data to the underlying stream, in a some cases preventing users from performing necessary error recovery. This is a customer reported issue.This PR changes the buffer flushing logic so that the first
MoveNextAsync()
does not prompt flushing of serializer buffers.Testing
Unit testing has been added that validates the new behavior.
Risk
Low. Applies minor interventions to product code to modify the buffer flushing logic.
cc @pranavkm @davidfowl