[release/9.0-staging] [Json] Avoid writing to PipeWriter if IAsyncEnumerable throws before first item #113699
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 #113503 to release/9.0-staging
/cc @BrennanConroy
Customer Impact
dotnet/aspnetcore#60911
The below code shows the problem, where there is serialized data in the
PipeWriter
even though there weren't any items returned by theIAsyncEnumerable<>
. This differs in behavior from theStream
overload where there isn't any data in the stream.This is an issue in apps like ASP.NET Core because the data written goes out in the Http response and if the user has error handling (like returning problem details) then the output is invalid due to the data written by the serializer.
Simplified ASP.NET Core app showing issue:
Output of an http request to
/weatherforecast
:Note the
[
in the beginning.Another example is a pure console app using the new overload:
Regression
It's technically not a regression for console apps moving from 8.0 -> 9.0, but it is a regression in behavior if you go from ASP.NET Core 8.0 -> 9.0 or use the new PipeWriter overloads in Json in your console apps.
Testing
[How was the fix verified? How was the issue missed previously? What tests were added?]
There was already a regression test for this case for the
Stream
overload. When adding thePipeWriter
overload we missed addingPipeWriter
to the suite of tests that the regression test was part of. Fixing that testing oversight showed that the issue would have been caught before and shows that the fix resolves the issue.Risk
[High/Medium/Low. Justify the indication by mentioning how risks were measured and addressed.]
Low
The testing in Json is extensive, we just missed adding a suite of tests for the new (in 9.0)
PipeWriter
overload.