Repository navigation
Fix response streaming dropping unflushed BodyWriter bytes - #2609
Merged
Merged
Conversation
When response streaming is enabled, StreamingResponseBodyFeature.Writer binds its inner PipeWriter to the pre-start buffer on first use. The inner writer was only rebound to the Lambda response stream when the application itself flushed or completed the writer. If the response was started any other way (CompleteAsync at the end of the request, an explicit Response.StartAsync, or SendFileAsync), bytes the application had advanced but not flushed were later flushed into the discarded pre-start buffer and never reached the client. StartAsync now flushes any pending writer bytes into the pre-start buffer and detaches the inner writer before copying the buffer to the Lambda stream, so subsequent writes bind to the live stream.
dscpinheiro
approved these changes
Nov 13, 2026
afroz429
approved these changes
Nov 13, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Issue #, if available:
Description of changes:
When response streaming is enabled (
EnableResponseStreaming), bytes an application writes throughHttpResponse.BodyWriterwithGetSpan/GetMemory+Advance, without flushing, can be silently dropped. The response then arrives with an empty (or truncated) body and no error.Cause.
StreamingResponseBodyFeature.Writeris aStartOnFlushPipeWriterwhose innerPipeWriteris created lazily against the feature's currentStream, which before the response starts is the pre-startMemoryStream. The wrapper only dropped that inner writer on its own unstartedFlushAsync/CompleteAsync/Completepaths. When the response was started any other way (CompleteAsyncat the end of the request, an explicitResponse.StartAsync(), orSendFileAsync),StartAsynccopied the still-empty pre-start buffer to the Lambda stream, and the later flush wrote the pending bytes into the discarded pre-start buffer.Fix.
StartAsyncnow asks the writer to flush any pending bytes into the pre-start buffer and detach its inner writer before the buffer is copied to the Lambda stream, so pending bytes are sent in order and later writes bind to the live stream regardless of how the response is started.Tests. Added four tests to
StreamingResponseBodyFeatureTests: unflushedAdvancefollowed byCompleteAsync, by an explicitStartAsync+ flush, bySendFileAsync, and interleaved writes before and after start. All four fail onmasterand pass with this change. The fullAmazon.Lambda.AspNetCoreServer.Testsuite passes on net8.0 and net10.0 (229/229).Found by an automated concurrency analysis run (Unveil) and reproduced end to end through
APIGatewayHttpApiV2ProxyFunction.FunctionHandlerAsyncwith response streaming enabled.By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.