diff --git a/src/Microsoft.AspNetCore.Server.Kestrel.Core/Internal/Http/FrameOfT.cs b/src/Microsoft.AspNetCore.Server.Kestrel.Core/Internal/Http/FrameOfT.cs index 8eddab20d..094cb3ca1 100644 --- a/src/Microsoft.AspNetCore.Server.Kestrel.Core/Internal/Http/FrameOfT.cs +++ b/src/Microsoft.AspNetCore.Server.Kestrel.Core/Internal/Http/FrameOfT.cs @@ -163,13 +163,9 @@ public override async Task ProcessRequestsAsync() TimeoutControl.SetTimeout(Constants.RequestBodyDrainTimeout.Ticks, TimeoutAction.SendTimeoutResponse); await messageBody.ConsumeAsync(); TimeoutControl.CancelTimeout(); - - // At this point both the request body pipe reader and writer should be completed. - RequestBodyPipe.Reset(); } else { - RequestBodyPipe.Reader.Complete(); messageBody.Cancel(); Input.CancelPendingRead(); } @@ -201,6 +197,14 @@ public override async Task ProcessRequestsAsync() // StopStreams should be called before the end of the "if (!_requestProcessingStopping)" block // to ensure InitializeStreams has been called. StopStreams(); + + if (HasStartedConsumingRequestBody) + { + RequestBodyPipe.Reader.Complete(); + + // At this point both the request body pipe reader and writer should be completed. + RequestBodyPipe.Reset(); + } } } diff --git a/src/Microsoft.AspNetCore.Server.Kestrel.Core/Internal/Http/MessageBody.cs b/src/Microsoft.AspNetCore.Server.Kestrel.Core/Internal/Http/MessageBody.cs index f964f5500..eb987d959 100644 --- a/src/Microsoft.AspNetCore.Server.Kestrel.Core/Internal/Http/MessageBody.cs +++ b/src/Microsoft.AspNetCore.Server.Kestrel.Core/Internal/Http/MessageBody.cs @@ -204,19 +204,12 @@ public void Cancel() { TryInit(); - try + ReadResult result; + do { - ReadResult result; - do - { - result = await _context.RequestBodyPipe.Reader.ReadAsync(); - _context.RequestBodyPipe.Reader.Advance(result.Buffer.End); - } while (!result.IsCompleted); - } - finally - { - _context.RequestBodyPipe.Reader.Complete(); - } + result = await _context.RequestBodyPipe.Reader.ReadAsync(); + _context.RequestBodyPipe.Reader.Advance(result.Buffer.End); + } while (!result.IsCompleted); } protected void Copy(ReadableBuffer readableBuffer, WritableBuffer writableBuffer) diff --git a/test/Microsoft.AspNetCore.Server.Kestrel.Core.Tests/MessageBodyTests.cs b/test/Microsoft.AspNetCore.Server.Kestrel.Core.Tests/MessageBodyTests.cs index 7a5f9caa7..2c8b3b7f8 100644 --- a/test/Microsoft.AspNetCore.Server.Kestrel.Core.Tests/MessageBodyTests.cs +++ b/test/Microsoft.AspNetCore.Server.Kestrel.Core.Tests/MessageBodyTests.cs @@ -349,7 +349,7 @@ public async Task CopyToAsyncDoesNotCompletePipeReader() } [Fact] - public async Task ConsumeAsyncCompletesPipeReader() + public async Task ConsumeAsyncConsumesAllRemainingInput() { using (var input = new TestInput()) { @@ -359,7 +359,7 @@ public async Task ConsumeAsyncCompletesPipeReader() await body.ConsumeAsync(); - await Assert.ThrowsAsync(async () => await body.ReadAsync(new ArraySegment(new byte[1]))); + Assert.Equal(0, await body.ReadAsync(new ArraySegment(new byte[1]))); } } diff --git a/test/Microsoft.AspNetCore.Server.Kestrel.FunctionalTests/FrameConnectionManagerTests.cs b/test/Microsoft.AspNetCore.Server.Kestrel.FunctionalTests/FrameConnectionManagerTests.cs index 0e71450a5..3ecd57588 100644 --- a/test/Microsoft.AspNetCore.Server.Kestrel.FunctionalTests/FrameConnectionManagerTests.cs +++ b/test/Microsoft.AspNetCore.Server.Kestrel.FunctionalTests/FrameConnectionManagerTests.cs @@ -16,7 +16,8 @@ namespace Microsoft.AspNetCore.Server.Kestrel.FunctionalTests { public class FrameConnectionManagerTests { - +// This test causes MemoryPoolBlocks to be finalized which in turn causes an assert failure in debug builds. +#if !DEBUG [ConditionalFact] [NoDebuggerCondition] public async Task CriticalErrorLoggedIfApplicationDoesntComplete() @@ -65,6 +66,7 @@ public async Task CriticalErrorLoggedIfApplicationDoesntComplete() Assert.True(logWaitAttempts < 10); } } +#endif private class NoDebuggerConditionAttribute : Attribute, ITestCondition { diff --git a/test/Microsoft.AspNetCore.Server.Kestrel.FunctionalTests/IWebHostPortExtensions.cs b/test/Microsoft.AspNetCore.Server.Kestrel.FunctionalTests/TestHelpers/IWebHostPortExtensions.cs similarity index 100% rename from test/Microsoft.AspNetCore.Server.Kestrel.FunctionalTests/IWebHostPortExtensions.cs rename to test/Microsoft.AspNetCore.Server.Kestrel.FunctionalTests/TestHelpers/IWebHostPortExtensions.cs diff --git a/test/Microsoft.AspNetCore.Server.Kestrel.FunctionalTests/TestServer.cs b/test/Microsoft.AspNetCore.Server.Kestrel.FunctionalTests/TestHelpers/TestServer.cs similarity index 100% rename from test/Microsoft.AspNetCore.Server.Kestrel.FunctionalTests/TestServer.cs rename to test/Microsoft.AspNetCore.Server.Kestrel.FunctionalTests/TestHelpers/TestServer.cs