Improve RequestAborted token handling (#2977)

- Improve test reliability of tests verifying the RequestAborted token gets tripped
- Once the response body is completed, don't fire the token for that request even if it is accessed later on.
This commit is contained in:
Stephen Halter 2018-10-04 16:48:50 -07:00 committed by GitHub
parent b3f9f38ac6
commit f91ae20a3c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
9 changed files with 180 additions and 104 deletions

View File

@ -82,12 +82,12 @@ namespace Microsoft.AspNetCore.Server.Kestrel.Core.Internal
} }
finally finally
{ {
Log.ConnectionStop(connectionContext.ConnectionId);
KestrelEventSource.Log.ConnectionStop(connectionContext);
connection.Complete(); connection.Complete();
_serviceContext.ConnectionManager.RemoveConnection(id); _serviceContext.ConnectionManager.RemoveConnection(id);
Log.ConnectionStop(connectionContext.ConnectionId);
KestrelEventSource.Log.ConnectionStop(connectionContext);
} }
} }

View File

@ -39,12 +39,17 @@ namespace Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Http
private Stack<KeyValuePair<Func<object, Task>, object>> _onStarting; private Stack<KeyValuePair<Func<object, Task>, object>> _onStarting;
private Stack<KeyValuePair<Func<object, Task>, object>> _onCompleted; private Stack<KeyValuePair<Func<object, Task>, object>> _onCompleted;
private volatile int _ioCompleted; private object _abortLock = new object();
private volatile bool _requestAborted;
private bool _preventRequestAbortedCancellation;
private CancellationTokenSource _abortedCts; private CancellationTokenSource _abortedCts;
private CancellationToken? _manuallySetRequestAbortToken; private CancellationToken? _manuallySetRequestAbortToken;
protected RequestProcessingStatus _requestProcessingStatus; protected RequestProcessingStatus _requestProcessingStatus;
protected volatile bool _keepAlive; // volatile, see: https://msdn.microsoft.com/en-us/library/x13ttww7.aspx
// Keep-alive is default for HTTP/1.1 and HTTP/2; parsing and errors will change its value
// volatile, see: https://msdn.microsoft.com/en-us/library/x13ttww7.aspx
protected volatile bool _keepAlive = true;
protected bool _upgradeAvailable; protected bool _upgradeAvailable;
private bool _canHaveBody; private bool _canHaveBody;
private bool _autoChunk; private bool _autoChunk;
@ -235,16 +240,26 @@ namespace Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Http
{ {
return _manuallySetRequestAbortToken.Value; return _manuallySetRequestAbortToken.Value;
} }
// Otherwise, get the abort CTS. If we have one, which would mean that someone previously
// asked for the RequestAborted token, simply return its token. If we don't, lock (_abortLock)
// check to see whether we've already aborted, in which case just return an {
// already canceled token. Finally, force a source into existence if we still if (_preventRequestAbortedCancellation)
// don't have one, and return its token. {
var cts = _abortedCts; return new CancellationToken(false);
return }
cts != null ? cts.Token :
(_ioCompleted == 1) ? new CancellationToken(true) : if (_requestAborted)
RequestAbortedSource.Token; {
return new CancellationToken(true);
}
if (_abortedCts == null)
{
_abortedCts = new CancellationTokenSource();
}
return _abortedCts.Token;
}
} }
set set
{ {
@ -254,28 +269,6 @@ namespace Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Http
} }
} }
private CancellationTokenSource RequestAbortedSource
{
get
{
// Get the abort token, lazily-initializing it if necessary.
// Make sure it's canceled if an abort request already came in.
// EnsureInitialized can return null since _abortedCts is reset to null
// after it's already been initialized to a non-null value.
// If EnsureInitialized does return null, this property was accessed between
// requests so it's safe to return an ephemeral CancellationTokenSource.
var cts = LazyInitializer.EnsureInitialized(ref _abortedCts, () => new CancellationTokenSource())
?? new CancellationTokenSource();
if (_ioCompleted == 1)
{
cts.Cancel();
}
return cts;
}
}
public bool HasResponseStarted => _requestProcessingStatus == RequestProcessingStatus.ResponseStarted; public bool HasResponseStarted => _requestProcessingStatus == RequestProcessingStatus.ResponseStarted;
protected HttpRequestHeaders HttpRequestHeaders { get; } = new HttpRequestHeaders(); protected HttpRequestHeaders HttpRequestHeaders { get; } = new HttpRequestHeaders();
@ -354,9 +347,18 @@ namespace Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Http
Scheme = _scheme; Scheme = _scheme;
_manuallySetRequestAbortToken = null; _manuallySetRequestAbortToken = null;
_abortedCts = null; _preventRequestAbortedCancellation = false;
// Lock to prevent CancelRequestAbortedToken from attempting to cancel an disposed CTS.
lock (_abortLock)
{
if (!_requestAborted)
{
_abortedCts?.Dispose();
_abortedCts = null;
}
}
// Allow two bytes for \r\n after headers
_requestHeadersParsed = 0; _requestHeadersParsed = 0;
_responseBytesWritten = 0; _responseBytesWritten = 0;
@ -401,7 +403,8 @@ namespace Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Http
{ {
try try
{ {
RequestAbortedSource.Cancel(); _abortedCts.Cancel();
_abortedCts.Dispose();
_abortedCts = null; _abortedCts = null;
} }
catch (Exception ex) catch (Exception ex)
@ -412,15 +415,21 @@ namespace Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Http
protected void AbortRequest() protected void AbortRequest()
{ {
if (Interlocked.Exchange(ref _ioCompleted, 1) != 0) lock (_abortLock)
{ {
return; if (_requestAborted)
{
return;
}
_requestAborted = true;
} }
_keepAlive = false; if (_abortedCts != null)
{
// Potentially calling user code. CancelRequestAbortedToken logs any exceptions. // Potentially calling user code. CancelRequestAbortedToken logs any exceptions.
ServiceContext.Scheduler.Schedule(state => ((HttpProtocol)state).CancelRequestAbortedToken(), this); ServiceContext.Scheduler.Schedule(state => ((HttpProtocol)state).CancelRequestAbortedToken(), this);
}
} }
protected void PoisonRequestBodyStream(Exception abortReason) protected void PoisonRequestBodyStream(Exception abortReason)
@ -428,6 +437,22 @@ namespace Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Http
_streams?.Abort(abortReason); _streams?.Abort(abortReason);
} }
// Prevents the RequestAborted token from firing for the duration of the request.
private void PreventRequestAbortedCancellation()
{
lock (_abortLock)
{
if (_requestAborted)
{
return;
}
_preventRequestAbortedCancellation = true;
_abortedCts?.Dispose();
_abortedCts = null;
}
}
public void OnHeader(Span<byte> name, Span<byte> value) public void OnHeader(Span<byte> name, Span<byte> value)
{ {
_requestHeadersParsed++; _requestHeadersParsed++;
@ -487,9 +512,6 @@ namespace Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Http
private async Task ProcessRequests<TContext>(IHttpApplication<TContext> application) private async Task ProcessRequests<TContext>(IHttpApplication<TContext> application)
{ {
// Keep-alive is default for HTTP/1.1 and HTTP/2; parsing and errors will change its value
_keepAlive = true;
while (_keepAlive) while (_keepAlive)
{ {
BeginRequestProcessing(); BeginRequestProcessing();
@ -529,7 +551,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Http
// Run the application code for this request // Run the application code for this request
await application.ProcessRequestAsync(httpContext); await application.ProcessRequestAsync(httpContext);
if (_ioCompleted == 0) if (!_requestAborted)
{ {
VerifyResponseContentLength(); VerifyResponseContentLength();
} }
@ -565,7 +587,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Http
// 4XX responses are written by TryProduceInvalidRequestResponse during connection tear down. // 4XX responses are written by TryProduceInvalidRequestResponse during connection tear down.
if (_requestRejectedException == null) if (_requestRejectedException == null)
{ {
if (_ioCompleted == 0) if (!_requestAborted)
{ {
// Call ProduceEnd() before consuming the rest of the request body to prevent // Call ProduceEnd() before consuming the rest of the request body to prevent
// delaying clients waiting for the chunk terminator: // delaying clients waiting for the chunk terminator:
@ -597,7 +619,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Http
application.DisposeContext(httpContext, _applicationException); application.DisposeContext(httpContext, _applicationException);
// Even for non-keep-alive requests, try to consume the entire body to avoid RSTs. // Even for non-keep-alive requests, try to consume the entire body to avoid RSTs.
if (_ioCompleted == 0 && _requestRejectedException == null && !messageBody.IsEmpty) if (!_requestAborted && _requestRejectedException == null && !messageBody.IsEmpty)
{ {
await messageBody.ConsumeAsync(); await messageBody.ConsumeAsync();
} }
@ -877,7 +899,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Http
responseHeaders.ContentLength.HasValue && responseHeaders.ContentLength.HasValue &&
_responseBytesWritten == responseHeaders.ContentLength.Value) _responseBytesWritten == responseHeaders.ContentLength.Value)
{ {
_abortedCts = null; PreventRequestAbortedCancellation();
} }
} }
@ -995,8 +1017,8 @@ namespace Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Http
protected Task TryProduceInvalidRequestResponse() protected Task TryProduceInvalidRequestResponse()
{ {
// If _ioCompleted is set, the connection has already been closed. // If _requestAborted is set, the connection has already been closed.
if (_requestRejectedException != null && _ioCompleted == 0) if (_requestRejectedException != null && !_requestAborted)
{ {
return ProduceEnd(); return ProduceEnd();
} }
@ -1073,7 +1095,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Http
private async Task WriteSuffixAwaited() private async Task WriteSuffixAwaited()
{ {
// For the same reason we call CheckLastWrite() in Content-Length responses. // For the same reason we call CheckLastWrite() in Content-Length responses.
_abortedCts = null; PreventRequestAbortedCancellation();
await Output.WriteStreamSuffixAsync(); await Output.WriteStreamSuffixAsync();

View File

@ -640,17 +640,21 @@ namespace Microsoft.AspNetCore.Server.Kestrel.Core.Tests
{ {
_http1Connection.ResponseHeaders["Content-Length"] = "12"; _http1Connection.ResponseHeaders["Content-Length"] = "12";
// Need to compare WaitHandle ref since CancellationToken is struct var original = _http1Connection.RequestAborted;
var original = _http1Connection.RequestAborted.WaitHandle;
foreach (var ch in "hello, worl") foreach (var ch in "hello, worl")
{ {
await _http1Connection.WriteAsync(new ArraySegment<byte>(new[] { (byte)ch })); await _http1Connection.WriteAsync(new ArraySegment<byte>(new[] { (byte)ch }));
Assert.Same(original, _http1Connection.RequestAborted.WaitHandle); Assert.Equal(original, _http1Connection.RequestAborted);
} }
await _http1Connection.WriteAsync(new ArraySegment<byte>(new[] { (byte)'d' })); await _http1Connection.WriteAsync(new ArraySegment<byte>(new[] { (byte)'d' }));
Assert.NotSame(original, _http1Connection.RequestAborted.WaitHandle); Assert.NotEqual(original, _http1Connection.RequestAborted);
_http1Connection.Abort(new ConnectionAbortedException());
Assert.False(original.IsCancellationRequested);
Assert.False(_http1Connection.RequestAborted.IsCancellationRequested);
} }
[Fact] [Fact]
@ -658,17 +662,21 @@ namespace Microsoft.AspNetCore.Server.Kestrel.Core.Tests
{ {
_http1Connection.ResponseHeaders["Content-Length"] = "12"; _http1Connection.ResponseHeaders["Content-Length"] = "12";
// Need to compare WaitHandle ref since CancellationToken is struct var original = _http1Connection.RequestAborted;
var original = _http1Connection.RequestAborted.WaitHandle;
foreach (var ch in "hello, worl") foreach (var ch in "hello, worl")
{ {
await _http1Connection.WriteAsync(new ArraySegment<byte>(new[] { (byte)ch }), default(CancellationToken)); await _http1Connection.WriteAsync(new ArraySegment<byte>(new[] { (byte)ch }), default(CancellationToken));
Assert.Same(original, _http1Connection.RequestAborted.WaitHandle); Assert.Equal(original, _http1Connection.RequestAborted);
} }
await _http1Connection.WriteAsync(new ArraySegment<byte>(new[] { (byte)'d' }), default(CancellationToken)); await _http1Connection.WriteAsync(new ArraySegment<byte>(new[] { (byte)'d' }), default(CancellationToken));
Assert.NotSame(original, _http1Connection.RequestAborted.WaitHandle); Assert.NotEqual(original, _http1Connection.RequestAborted);
_http1Connection.Abort(new ConnectionAbortedException());
Assert.False(original.IsCancellationRequested);
Assert.False(_http1Connection.RequestAborted.IsCancellationRequested);
} }
[Fact] [Fact]
@ -676,36 +684,44 @@ namespace Microsoft.AspNetCore.Server.Kestrel.Core.Tests
{ {
_http1Connection.ResponseHeaders["Content-Length"] = "12"; _http1Connection.ResponseHeaders["Content-Length"] = "12";
// Need to compare WaitHandle ref since CancellationToken is struct var original = _http1Connection.RequestAborted;
var original = _http1Connection.RequestAborted.WaitHandle;
// Only first write can be WriteAsyncAwaited // Only first write can be WriteAsyncAwaited
var startingTask = _http1Connection.InitializeResponseAwaited(Task.CompletedTask, 1); var startingTask = _http1Connection.InitializeResponseAwaited(Task.CompletedTask, 1);
await _http1Connection.WriteAsyncAwaited(startingTask, new ArraySegment<byte>(new[] { (byte)'h' }), default(CancellationToken)); await _http1Connection.WriteAsyncAwaited(startingTask, new ArraySegment<byte>(new[] { (byte)'h' }), default(CancellationToken));
Assert.Same(original, _http1Connection.RequestAborted.WaitHandle); Assert.Equal(original, _http1Connection.RequestAborted);
foreach (var ch in "ello, worl") foreach (var ch in "ello, worl")
{ {
await _http1Connection.WriteAsync(new ArraySegment<byte>(new[] { (byte)ch }), default(CancellationToken)); await _http1Connection.WriteAsync(new ArraySegment<byte>(new[] { (byte)ch }), default(CancellationToken));
Assert.Same(original, _http1Connection.RequestAborted.WaitHandle); Assert.Equal(original, _http1Connection.RequestAborted);
} }
await _http1Connection.WriteAsync(new ArraySegment<byte>(new[] { (byte)'d' }), default(CancellationToken)); await _http1Connection.WriteAsync(new ArraySegment<byte>(new[] { (byte)'d' }), default(CancellationToken));
Assert.NotSame(original, _http1Connection.RequestAborted.WaitHandle); Assert.NotEqual(original, _http1Connection.RequestAborted);
_http1Connection.Abort(new ConnectionAbortedException());
Assert.False(original.IsCancellationRequested);
Assert.False(_http1Connection.RequestAborted.IsCancellationRequested);
} }
[Fact] [Fact]
public async Task RequestAbortedTokenIsResetBeforeLastWriteWithChunkedEncoding() public async Task RequestAbortedTokenIsResetBeforeLastWriteWithChunkedEncoding()
{ {
// Need to compare WaitHandle ref since CancellationToken is struct var original = _http1Connection.RequestAborted;
var original = _http1Connection.RequestAborted.WaitHandle;
_http1Connection.HttpVersion = "HTTP/1.1"; _http1Connection.HttpVersion = "HTTP/1.1";
await _http1Connection.WriteAsync(new ArraySegment<byte>(Encoding.ASCII.GetBytes("hello, world")), default(CancellationToken)); await _http1Connection.WriteAsync(new ArraySegment<byte>(Encoding.ASCII.GetBytes("hello, world")), default(CancellationToken));
Assert.Same(original, _http1Connection.RequestAborted.WaitHandle); Assert.Equal(original, _http1Connection.RequestAborted);
await _http1Connection.ProduceEndAsync(); await _http1Connection.ProduceEndAsync();
Assert.NotSame(original, _http1Connection.RequestAborted.WaitHandle); Assert.NotEqual(original, _http1Connection.RequestAborted);
_http1Connection.Abort(new ConnectionAbortedException());
Assert.False(original.IsCancellationRequested);
Assert.False(_http1Connection.RequestAborted.IsCancellationRequested);
} }
[Fact] [Fact]

View File

@ -201,7 +201,11 @@ namespace Microsoft.AspNetCore.Server.Kestrel.InMemory.FunctionalTests
private TestServer CreateServerWithMaxConnections(RequestDelegate app, ResourceCounter concurrentConnectionCounter) private TestServer CreateServerWithMaxConnections(RequestDelegate app, ResourceCounter concurrentConnectionCounter)
{ {
var serviceContext = new TestServiceContext(LoggerFactory); var serviceContext = new TestServiceContext(LoggerFactory)
{
ExpectedConnectionMiddlewareCount = 1
};
var listenOptions = new ListenOptions(new IPEndPoint(IPAddress.Loopback, 0)); var listenOptions = new ListenOptions(new IPEndPoint(IPAddress.Loopback, 0));
listenOptions.Use(next => listenOptions.Use(next =>
{ {

View File

@ -14,6 +14,7 @@ using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Server.Kestrel.Core; using Microsoft.AspNetCore.Server.Kestrel.Core;
using Microsoft.AspNetCore.Testing; using Microsoft.AspNetCore.Testing;
using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.DependencyInjection;
using Xunit;
namespace Microsoft.AspNetCore.Server.Kestrel.InMemory.FunctionalTests.TestTransport namespace Microsoft.AspNetCore.Server.Kestrel.InMemory.FunctionalTests.TestTransport
{ {
@ -66,7 +67,6 @@ namespace Microsoft.AspNetCore.Server.Kestrel.InMemory.FunctionalTests.TestTrans
_transportFactory = new InMemoryTransportFactory(); _transportFactory = new InMemoryTransportFactory();
HttpClientSlim = new InMemoryHttpClientSlim(this); HttpClientSlim = new InMemoryHttpClientSlim(this);
var hostBuilder = new WebHostBuilder() var hostBuilder = new WebHostBuilder()
.ConfigureServices(services => .ConfigureServices(services =>
{ {
@ -79,6 +79,12 @@ namespace Microsoft.AspNetCore.Server.Kestrel.InMemory.FunctionalTests.TestTrans
{ {
context.ServerOptions.ApplicationServices = sp; context.ServerOptions.ApplicationServices = sp;
configureKestrel(context.ServerOptions); configureKestrel(context.ServerOptions);
// Prevent ListenOptions reuse. This is easily done accidentally when trying to debug a test by running it
// in a loop, but will cause problems because only the app func from the first loop will ever be invoked.
Assert.All(context.ServerOptions.ListenOptions, lo =>
Assert.Equal(context.ExpectedConnectionMiddlewareCount, lo._middleware.Count));
return new KestrelServer(_transportFactory, context); return new KestrelServer(_transportFactory, context);
}); });
}); });

View File

@ -679,6 +679,10 @@ namespace Microsoft.AspNetCore.Server.Kestrel.FunctionalTests
readTcs.SetException(ex); readTcs.SetException(ex);
throw; throw;
} }
finally
{
await registrationTcs.Task.DefaultTimeout();
}
readTcs.SetException(new Exception("This shouldn't be reached.")); readTcs.SetException(new Exception("This shouldn't be reached."));
} }
@ -711,7 +715,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel.FunctionalTests
} }
} }
await Assert.ThrowsAsync<TaskCanceledException>(async () => await readTcs.Task); await Assert.ThrowsAsync<TaskCanceledException>(async () => await readTcs.Task).DefaultTimeout();
// The cancellation token for only the last request should be triggered. // The cancellation token for only the last request should be triggered.
var abortedRequestId = await registrationTcs.Task.DefaultTimeout(); var abortedRequestId = await registrationTcs.Task.DefaultTimeout();

View File

@ -211,6 +211,10 @@ namespace Microsoft.AspNetCore.Server.Kestrel.FunctionalTests
writeTcs.SetException(ex); writeTcs.SetException(ex);
throw; throw;
} }
finally
{
await requestAbortedWh.Task.DefaultTimeout();
}
writeTcs.SetException(new Exception("This shouldn't be reached.")); writeTcs.SetException(new Exception("This shouldn't be reached."));
}, new TestServiceContext(LoggerFactory), listenOptions)) }, new TestServiceContext(LoggerFactory), listenOptions))
@ -243,7 +247,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel.FunctionalTests
const int connectionPausedEventId = 4; const int connectionPausedEventId = 4;
const int maxRequestBufferSize = 4096; const int maxRequestBufferSize = 4096;
var requestAborted = false; var requestAborted = new TaskCompletionSource<object>(TaskCreationOptions.RunContinuationsAsynchronously);
var readCallbackUnwired = new TaskCompletionSource<object>(TaskCreationOptions.RunContinuationsAsynchronously); var readCallbackUnwired = new TaskCompletionSource<object>(TaskCreationOptions.RunContinuationsAsynchronously);
var clientClosedConnection = new TaskCompletionSource<object>(TaskCreationOptions.RunContinuationsAsynchronously); var clientClosedConnection = new TaskCompletionSource<object>(TaskCreationOptions.RunContinuationsAsynchronously);
var writeTcs = new TaskCompletionSource<object>(TaskCreationOptions.RunContinuationsAsynchronously); var writeTcs = new TaskCompletionSource<object>(TaskCreationOptions.RunContinuationsAsynchronously);
@ -291,10 +295,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel.FunctionalTests
using (var server = new TestServer(async context => using (var server = new TestServer(async context =>
{ {
context.RequestAborted.Register(() => context.RequestAborted.Register(() => requestAborted.SetResult(null));
{
requestAborted = true;
});
await clientClosedConnection.Task; await clientClosedConnection.Task;
@ -311,6 +312,10 @@ namespace Microsoft.AspNetCore.Server.Kestrel.FunctionalTests
writeTcs.SetException(ex); writeTcs.SetException(ex);
throw; throw;
} }
finally
{
await requestAborted.Task.DefaultTimeout();
}
writeTcs.SetException(new Exception("This shouldn't be reached.")); writeTcs.SetException(new Exception("This shouldn't be reached."));
}, testContext, listenOptions)) }, testContext, listenOptions))
@ -336,7 +341,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel.FunctionalTests
} }
mockKestrelTrace.Verify(t => t.ConnectionStop(It.IsAny<string>()), Times.Once()); mockKestrelTrace.Verify(t => t.ConnectionStop(It.IsAny<string>()), Times.Once());
Assert.True(requestAborted); Assert.True(requestAborted.Task.IsCompleted);
} }
[Theory] [Theory]
@ -345,22 +350,19 @@ namespace Microsoft.AspNetCore.Server.Kestrel.FunctionalTests
{ {
const int connectionResetEventId = 19; const int connectionResetEventId = 19;
const int connectionFinEventId = 6; const int connectionFinEventId = 6;
//const int connectionStopEventId = 2; const int connectionStopEventId = 2;
const int responseBodySegmentSize = 65536; const int responseBodySegmentSize = 65536;
const int responseBodySegmentCount = 100; const int responseBodySegmentCount = 100;
var requestAborted = new TaskCompletionSource<object>(TaskCreationOptions.RunContinuationsAsynchronously);
var appCompletedTcs = new TaskCompletionSource<object>(TaskCreationOptions.RunContinuationsAsynchronously); var appCompletedTcs = new TaskCompletionSource<object>(TaskCreationOptions.RunContinuationsAsynchronously);
var requestAborted = false;
var scratchBuffer = new byte[responseBodySegmentSize]; var scratchBuffer = new byte[responseBodySegmentSize];
using (var server = new TestServer(async context => using (var server = new TestServer(async context =>
{ {
context.RequestAborted.Register(() => context.RequestAborted.Register(() => requestAborted.SetResult(null));
{
requestAborted = true;
});
for (var i = 0; i < responseBodySegmentCount; i++) for (var i = 0; i < responseBodySegmentCount; i++)
{ {
@ -368,6 +370,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel.FunctionalTests
await Task.Delay(10); await Task.Delay(10);
} }
await requestAborted.Task.DefaultTimeout();
appCompletedTcs.SetResult(null); appCompletedTcs.SetResult(null);
}, new TestServiceContext(LoggerFactory), listenOptions)) }, new TestServiceContext(LoggerFactory), listenOptions))
{ {
@ -386,9 +389,9 @@ namespace Microsoft.AspNetCore.Server.Kestrel.FunctionalTests
connection.Reset(); connection.Reset();
} }
await appCompletedTcs.Task.DefaultTimeout(); await requestAborted.Task.DefaultTimeout();
// After the app is done with the write loop, the connection reset should be logged. // After the RequestAborted token is tripped, the connection reset should be logged.
// On Linux and macOS, the connection close is still sometimes observed as a FIN despite the LingerState. // On Linux and macOS, the connection close is still sometimes observed as a FIN despite the LingerState.
var presShutdownTransportLogs = TestSink.Writes.Where( var presShutdownTransportLogs = TestSink.Writes.Where(
w => w.LoggerName == "Microsoft.AspNetCore.Server.Kestrel.Transport.Libuv" || w => w.LoggerName == "Microsoft.AspNetCore.Server.Kestrel.Transport.Libuv" ||
@ -398,14 +401,13 @@ namespace Microsoft.AspNetCore.Server.Kestrel.FunctionalTests
(!TestPlatformHelper.IsWindows && w.EventId == connectionFinEventId)); (!TestPlatformHelper.IsWindows && w.EventId == connectionFinEventId));
Assert.NotEmpty(connectionResetLogs); Assert.NotEmpty(connectionResetLogs);
// On macOS, the default 5 shutdown timeout is insufficient for the write loop to complete, so give it extra time.
await appCompletedTcs.Task.DefaultTimeout();
} }
// TODO: Figure out what the following assertion is flaky. The server shouldn't shutdown before all var coreLogs = TestSink.Writes.Where(w => w.LoggerName == "Microsoft.AspNetCore.Server.Kestrel");
// the connections are closed, yet sometimes the connection stop log isn't observed here. Assert.Single(coreLogs.Where(w => w.EventId == connectionStopEventId));
//var coreLogs = TestSink.Writes.Where(w => w.LoggerName == "Microsoft.AspNetCore.Server.Kestrel");
//Assert.Single(coreLogs.Where(w => w.EventId == connectionStopEventId));
Assert.True(requestAborted, "RequestAborted token didn't fire.");
var transportLogs = TestSink.Writes.Where(w => w.LoggerName == "Microsoft.AspNetCore.Server.Kestrel" || var transportLogs = TestSink.Writes.Where(w => w.LoggerName == "Microsoft.AspNetCore.Server.Kestrel" ||
w.LoggerName == "Microsoft.AspNetCore.Server.Kestrel.Transport.Libuv" || w.LoggerName == "Microsoft.AspNetCore.Server.Kestrel.Transport.Libuv" ||
@ -512,6 +514,10 @@ namespace Microsoft.AspNetCore.Server.Kestrel.FunctionalTests
appFuncCompleted.SetResult(null); appFuncCompleted.SetResult(null);
throw; throw;
} }
finally
{
await requestAborted.Task.DefaultTimeout();
}
} }
using (var server = new TestServer(App, testContext, listenOptions)) using (var server = new TestServer(App, testContext, listenOptions))
@ -607,6 +613,10 @@ namespace Microsoft.AspNetCore.Server.Kestrel.FunctionalTests
appFuncCompleted.SetResult(null); appFuncCompleted.SetResult(null);
throw; throw;
} }
finally
{
await aborted.Task.DefaultTimeout();
}
}, testContext, listenOptions)) }, testContext, listenOptions))
{ {
using (var connection = server.CreateConnection()) using (var connection = server.CreateConnection())
@ -682,6 +692,10 @@ namespace Microsoft.AspNetCore.Server.Kestrel.FunctionalTests
copyToAsyncCts.SetException(ex); copyToAsyncCts.SetException(ex);
throw; throw;
} }
finally
{
await requestAborted.Task.DefaultTimeout();
}
copyToAsyncCts.SetException(new Exception("This shouldn't be reached.")); copyToAsyncCts.SetException(new Exception("This shouldn't be reached."));
} }
@ -777,12 +791,12 @@ namespace Microsoft.AspNetCore.Server.Kestrel.FunctionalTests
var targetBytesPerSecond = chunkSize / 4; var targetBytesPerSecond = chunkSize / 4;
await AssertStreamCompleted(connection.Stream, minTotalOutputSize, targetBytesPerSecond); await AssertStreamCompleted(connection.Stream, minTotalOutputSize, targetBytesPerSecond);
await appFuncCompleted.Task.DefaultTimeout(); await appFuncCompleted.Task.DefaultTimeout();
mockKestrelTrace.Verify(t => t.ResponseMinimumDataRateNotSatisfied(It.IsAny<string>(), It.IsAny<string>()), Times.Never());
mockKestrelTrace.Verify(t => t.ConnectionStop(It.IsAny<string>()), Times.Once());
Assert.False(requestAborted);
} }
} }
mockKestrelTrace.Verify(t => t.ResponseMinimumDataRateNotSatisfied(It.IsAny<string>(), It.IsAny<string>()), Times.Never());
mockKestrelTrace.Verify(t => t.ConnectionStop(It.IsAny<string>()), Times.Once());
Assert.False(requestAborted);
} }
[Fact] [Fact]
@ -852,12 +866,12 @@ namespace Microsoft.AspNetCore.Server.Kestrel.FunctionalTests
// Make sure consuming a single set of response headers exceeds the 2 second timeout. // Make sure consuming a single set of response headers exceeds the 2 second timeout.
var targetBytesPerSecond = responseSize / 4; var targetBytesPerSecond = responseSize / 4;
await AssertStreamCompleted(connection.Stream, minTotalOutputSize, targetBytesPerSecond); await AssertStreamCompleted(connection.Stream, minTotalOutputSize, targetBytesPerSecond);
mockKestrelTrace.Verify(t => t.ResponseMinimumDataRateNotSatisfied(It.IsAny<string>(), It.IsAny<string>()), Times.Never());
mockKestrelTrace.Verify(t => t.ConnectionStop(It.IsAny<string>()), Times.Once());
Assert.False(requestAborted);
} }
} }
mockKestrelTrace.Verify(t => t.ResponseMinimumDataRateNotSatisfied(It.IsAny<string>(), It.IsAny<string>()), Times.Never());
mockKestrelTrace.Verify(t => t.ConnectionStop(It.IsAny<string>()), Times.Once());
Assert.False(requestAborted);
} }
private async Task AssertStreamAborted(Stream stream, int totalBytes) private async Task AssertStreamAborted(Stream stream, int totalBytes)

View File

@ -77,6 +77,8 @@ namespace Microsoft.AspNetCore.Testing
public Func<MemoryPool<byte>> MemoryPoolFactory { get; set; } = KestrelMemoryPool.Create; public Func<MemoryPool<byte>> MemoryPoolFactory { get; set; } = KestrelMemoryPool.Create;
public int ExpectedConnectionMiddlewareCount { get; set; }
public string DateHeaderValue => DateHeaderValueManager.GetDateHeaderValues().String; public string DateHeaderValue => DateHeaderValueManager.GetDateHeaderValues().String;
} }
} }

View File

@ -17,6 +17,7 @@ using Microsoft.AspNetCore.Testing;
using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options; using Microsoft.Extensions.Options;
using Xunit;
namespace Microsoft.AspNetCore.Server.Kestrel.FunctionalTests namespace Microsoft.AspNetCore.Server.Kestrel.FunctionalTests
{ {
@ -48,6 +49,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel.FunctionalTests
: this(app, context, options => options.ListenOptions.Add(listenOptions), configureServices) : this(app, context, options => options.ListenOptions.Add(listenOptions), configureServices)
{ {
} }
public TestServer(RequestDelegate app, TestServiceContext context, Action<KestrelServerOptions> configureKestrel) public TestServer(RequestDelegate app, TestServiceContext context, Action<KestrelServerOptions> configureKestrel)
: this(app, context, configureKestrel, _ => { }) : this(app, context, configureKestrel, _ => { })
{ {
@ -77,6 +79,12 @@ namespace Microsoft.AspNetCore.Server.Kestrel.FunctionalTests
{ {
c.Configure(context.ServerOptions); c.Configure(context.ServerOptions);
} }
// Prevent ListenOptions reuse. This is easily done accidentally when trying to debug a test by running it
// in a loop, but will cause problems because only the app func from the first loop will ever be invoked.
Assert.All(context.ServerOptions.ListenOptions, lo =>
Assert.Equal(context.ExpectedConnectionMiddlewareCount, lo._middleware.Count));
return new KestrelServer(sp.GetRequiredService<ITransportFactory>(), context); return new KestrelServer(sp.GetRequiredService<ITransportFactory>(), context);
}); });
configureServices(services); configureServices(services);