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

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

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.

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