Inline second statemahine into loop and us EC.Restore to reset context (#23175)

This commit is contained in:
Ben Adams 2020-08-17 02:06:25 +01:00 committed by GitHub
parent de726ccc11
commit 03b75e354d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 90 additions and 93 deletions

View File

@ -607,6 +607,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Http
private async Task ProcessRequests<TContext>(IHttpApplication<TContext> application) private async Task ProcessRequests<TContext>(IHttpApplication<TContext> application)
{ {
var cleanContext = ExecutionContext.Capture();
while (_keepAlive) while (_keepAlive)
{ {
BeginRequestProcessing(); BeginRequestProcessing();
@ -637,26 +638,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Http
InitializeBodyControl(messageBody); InitializeBodyControl(messageBody);
// We run user controlled request processing in a seperate async method
// so any changes made to ExecutionContext are undone when it returns and
// each request starts with a fresh ExecutionContext state.
await ProcessRequest(application);
// Even for non-keep-alive requests, try to consume the entire body to avoid RSTs.
if (!_connectionAborted && _requestRejectedException == null && !messageBody.IsEmpty)
{
await messageBody.ConsumeAsync();
}
if (HasStartedConsumingRequestBody)
{
await messageBody.StopAsync();
}
}
}
private async ValueTask ProcessRequest<TContext>(IHttpApplication<TContext> application)
{
var context = application.CreateContext(this); var context = application.CreateContext(this);
try try
@ -742,6 +724,21 @@ namespace Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Http
} }
application.DisposeContext(context, _applicationException); application.DisposeContext(context, _applicationException);
// Even for non-keep-alive requests, try to consume the entire body to avoid RSTs.
if (!_connectionAborted && _requestRejectedException == null && !messageBody.IsEmpty)
{
await messageBody.ConsumeAsync();
}
if (HasStartedConsumingRequestBody)
{
await messageBody.StopAsync();
}
// Clear any AsyncLocals set during the request; back to a clean state ready for next request
ExecutionContext.Restore(cleanContext);
}
} }
public void OnStarting(Func<object, Task> callback, object state) public void OnStarting(Func<object, Task> callback, object state)