diff --git a/src/Servers/Kestrel/Core/src/Internal/Http2/Http2Stream.cs b/src/Servers/Kestrel/Core/src/Internal/Http2/Http2Stream.cs index e2a5b3b83d..5b4c6f6005 100644 --- a/src/Servers/Kestrel/Core/src/Internal/Http2/Http2Stream.cs +++ b/src/Servers/Kestrel/Core/src/Internal/Http2/Http2Stream.cs @@ -368,11 +368,16 @@ namespace Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Http2 { RequestBodyPipe.Writer.Write(segment.Span); } - var flushTask = RequestBodyPipe.Writer.FlushAsync(); - // It shouldn't be possible for the RequestBodyPipe to fill up an return an incomplete task if - // _inputFlowControl.Advance() didn't throw. - Debug.Assert(flushTask.IsCompleted); + // If the stream is completed go ahead and call RequestBodyPipe.Writer.Complete(). + // Data will still be available to the reader. + if (!endStream) + { + var flushTask = RequestBodyPipe.Writer.FlushAsync(); + // It shouldn't be possible for the RequestBodyPipe to fill up an return an incomplete task if + // _inputFlowControl.Advance() didn't throw. + Debug.Assert(flushTask.IsCompleted); + } } } } diff --git a/src/Servers/Kestrel/test/InMemory.FunctionalTests/Http2/Http2StreamTests.cs b/src/Servers/Kestrel/test/InMemory.FunctionalTests/Http2/Http2StreamTests.cs index 2011a0b6f1..6c656e1a2a 100644 --- a/src/Servers/Kestrel/test/InMemory.FunctionalTests/Http2/Http2StreamTests.cs +++ b/src/Servers/Kestrel/test/InMemory.FunctionalTests/Http2/Http2StreamTests.cs @@ -1023,8 +1023,8 @@ namespace Microsoft.AspNetCore.Server.Kestrel.Core.Tests await InitializeConnectionAsync(async context => { var readResult = await context.Request.BodyPipe.ReadAsync(); - Assert.Equal(12, readResult.Buffer.Length); Assert.True(readResult.IsCompleted); + Assert.Equal(12, readResult.Buffer.Length); context.Request.BodyPipe.AdvanceTo(readResult.Buffer.End); readResult = await context.Request.BodyPipe.ReadAsync();