Skip to content
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

[Json] Avoid writing to PipeWriter if IAsyncEnumerable throws before first item #113503

Merged
merged 2 commits into from
Mar 19, 2025

Conversation

BrennanConroy
Copy link
Member

@BrennanConroy BrennanConroy commented Mar 14, 2025

@@ -179,6 +178,7 @@ rootValue is not null &&
}
else
{
writer.Flush();
Copy link
Member

@eiriktsarpalis eiriktsarpalis Mar 14, 2025

Choose a reason for hiding this comment

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

Is this fixing an unrelated bug related to SuppressFlush handling?

Copy link
Member Author

Choose a reason for hiding this comment

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

Nope, writer.Flush() calls Advance on the underlying PipeWriter which is what we're trying to avoid here. So without the move, [ would be written to the PipeWriter still before observing the exception from the IAsyncEnumerable.

@@ -230,6 +230,8 @@ rootValue is not null &&
}
catch
{
// Reset the writer in exception cases as we don't want the writer.Dispose() call in the finally block to flush any pending bytes.
Copy link
Member

Choose a reason for hiding this comment

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

If we only do this to prevent the writer.Dispose() from flushing, why not just collocate the two calls?

@@ -354,7 +354,7 @@ public async Task RegressionTest_ExceptionOnFirstMoveNextShouldNotFlushBuffer()

// Regression test for https://github.com/dotnet/aspnetcore/issues/36977
using var stream = new MemoryStream();
await Assert.ThrowsAsync<NotImplementedException>(async () => await StreamingSerializer.SerializeWrapper(stream, new AsyncEnumerableDto<int> { Data = GetFailingAsyncEnumerable() }));
await Assert.ThrowsAsync<NotImplementedException>(async () => await StreamingSerializer.SerializeWrapper(stream, GetFailingAsyncEnumerable()));
Copy link
Member

Choose a reason for hiding this comment

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

I don't recall what this regression test is checking, but why does this now need to be changed?

Copy link
Member Author

Choose a reason for hiding this comment

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

The regression, at least from ASP.NET Core side, was a pure IAsyncEnumerable<T>, so wrapping it in a DTO seemed odd. It also was initially failing in the Pipe case as the "Data:" bit was being flushed, but it seems I can reset the test now that I've moved the writer.Flush() call into the SuppressFlush check if we'd like.

@BrennanConroy
Copy link
Member Author

Updated

Copy link
Member

@eiriktsarpalis eiriktsarpalis left a comment

Choose a reason for hiding this comment

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

Thanks

@eiriktsarpalis eiriktsarpalis merged commit 322735d into dotnet:main Mar 19, 2025
79 of 84 checks passed
@BrennanConroy
Copy link
Member Author

/backport to release/9.0-staging

Copy link
Contributor

Started backporting to release/9.0-staging: https://github.com/dotnet/runtime/actions/runs/13952382257

@eiriktsarpalis
Copy link
Member

Was this this a regression from .NET 8?

@BrennanConroy
Copy link
Member Author

The PipeWriter overload is new in 9.0

@eiriktsarpalis
Copy link
Member

Then I'm not sure if it meets the bar for servicing.

@BrennanConroy
Copy link
Member Author

While technically not a regression from STJ perspective since it is a new API, it is a regression from ASP.NET Core perspective since we changed to use the new API in 9.0.

@eiriktsarpalis
Copy link
Member

Would it be possible to update the customer impact section in the backporting PR specifically describing how it impacts ASP.NET Core users?

FYI @ericstj @artl93

@BrennanConroy
Copy link
Member Author

Did I not already do that?

@eiriktsarpalis
Copy link
Member

The reproduction is an STJ snippet. Given that we're contemplating a backport on the grounds of an ASP.NET regression I would suggest putting all emphasis on that.

@BrennanConroy
Copy link
Member Author

Sure, updated with more emphasis.

@eiriktsarpalis
Copy link
Member

Thanks

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.

Exceptions when returning IAsyncEnumerable create invalid ProblemDetails JSON response
2 participants