Skip to content
This repository was archived by the owner on Dec 18, 2018. It is now read-only.

Commit d879518

Browse files
authored
Always complete RequestBodyPipe.Reader (#1893)
* Disable test that leaks blocks in debug builds * Move some test helpers
1 parent 31e5dfd commit d879518

File tree

6 files changed

+18
-19
lines changed

6 files changed

+18
-19
lines changed

src/Microsoft.AspNetCore.Server.Kestrel.Core/Internal/Http/FrameOfT.cs

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -163,13 +163,9 @@ public override async Task ProcessRequestsAsync()
163163
TimeoutControl.SetTimeout(Constants.RequestBodyDrainTimeout.Ticks, TimeoutAction.SendTimeoutResponse);
164164
await messageBody.ConsumeAsync();
165165
TimeoutControl.CancelTimeout();
166-
167-
// At this point both the request body pipe reader and writer should be completed.
168-
RequestBodyPipe.Reset();
169166
}
170167
else
171168
{
172-
RequestBodyPipe.Reader.Complete();
173169
messageBody.Cancel();
174170
Input.CancelPendingRead();
175171
}
@@ -201,6 +197,14 @@ public override async Task ProcessRequestsAsync()
201197
// StopStreams should be called before the end of the "if (!_requestProcessingStopping)" block
202198
// to ensure InitializeStreams has been called.
203199
StopStreams();
200+
201+
if (HasStartedConsumingRequestBody)
202+
{
203+
RequestBodyPipe.Reader.Complete();
204+
205+
// At this point both the request body pipe reader and writer should be completed.
206+
RequestBodyPipe.Reset();
207+
}
204208
}
205209
}
206210

src/Microsoft.AspNetCore.Server.Kestrel.Core/Internal/Http/MessageBody.cs

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -204,19 +204,12 @@ public void Cancel()
204204
{
205205
TryInit();
206206

207-
try
207+
ReadResult result;
208+
do
208209
{
209-
ReadResult result;
210-
do
211-
{
212-
result = await _context.RequestBodyPipe.Reader.ReadAsync();
213-
_context.RequestBodyPipe.Reader.Advance(result.Buffer.End);
214-
} while (!result.IsCompleted);
215-
}
216-
finally
217-
{
218-
_context.RequestBodyPipe.Reader.Complete();
219-
}
210+
result = await _context.RequestBodyPipe.Reader.ReadAsync();
211+
_context.RequestBodyPipe.Reader.Advance(result.Buffer.End);
212+
} while (!result.IsCompleted);
220213
}
221214

222215
protected void Copy(ReadableBuffer readableBuffer, WritableBuffer writableBuffer)

test/Microsoft.AspNetCore.Server.Kestrel.Core.Tests/MessageBodyTests.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -349,7 +349,7 @@ public async Task CopyToAsyncDoesNotCompletePipeReader()
349349
}
350350

351351
[Fact]
352-
public async Task ConsumeAsyncCompletesPipeReader()
352+
public async Task ConsumeAsyncConsumesAllRemainingInput()
353353
{
354354
using (var input = new TestInput())
355355
{
@@ -359,7 +359,7 @@ public async Task ConsumeAsyncCompletesPipeReader()
359359

360360
await body.ConsumeAsync();
361361

362-
await Assert.ThrowsAsync<InvalidOperationException>(async () => await body.ReadAsync(new ArraySegment<byte>(new byte[1])));
362+
Assert.Equal(0, await body.ReadAsync(new ArraySegment<byte>(new byte[1])));
363363
}
364364
}
365365

test/Microsoft.AspNetCore.Server.Kestrel.FunctionalTests/FrameConnectionManagerTests.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@ namespace Microsoft.AspNetCore.Server.Kestrel.FunctionalTests
1616
{
1717
public class FrameConnectionManagerTests
1818
{
19-
19+
// This test causes MemoryPoolBlocks to be finalized which in turn causes an assert failure in debug builds.
20+
#if !DEBUG
2021
[ConditionalFact]
2122
[NoDebuggerCondition]
2223
public async Task CriticalErrorLoggedIfApplicationDoesntComplete()
@@ -65,6 +66,7 @@ public async Task CriticalErrorLoggedIfApplicationDoesntComplete()
6566
Assert.True(logWaitAttempts < 10);
6667
}
6768
}
69+
#endif
6870

6971
private class NoDebuggerConditionAttribute : Attribute, ITestCondition
7072
{

0 commit comments

Comments
 (0)