Avoid Null ReferenceException in Frame.RequestAbortedSource

- Avoid unneeded resetting of _abortedCts and _manuallySetRequestAbortToken
This commit is contained in:
Stephen Halter 2016-06-01 16:51:41 -07:00
parent 6bff2ecb34
commit d14f18012a
2 changed files with 10 additions and 8 deletions

View File

@ -54,8 +54,8 @@ namespace Microsoft.AspNetCore.Server.Kestrel.Http
private Task _requestProcessingTask;
protected volatile bool _requestProcessingStopping; // volatile, see: https://msdn.microsoft.com/en-us/library/x13ttww7.aspx
protected int _requestAborted;
protected CancellationTokenSource _abortedCts;
protected CancellationToken? _manuallySetRequestAbortToken;
private CancellationTokenSource _abortedCts;
private CancellationToken? _manuallySetRequestAbortToken;
protected RequestProcessingStatus _requestProcessingStatus;
protected bool _keepAlive;
@ -196,7 +196,14 @@ namespace Microsoft.AspNetCore.Server.Kestrel.Http
{
// Get the abort token, lazily-initializing it if necessary.
// Make sure it's canceled if an abort request already came in.
var cts = LazyInitializer.EnsureInitialized(ref _abortedCts, () => new CancellationTokenSource());
// 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 (Volatile.Read(ref _requestAborted) == 1)
{
cts.Cancel();

View File

@ -87,9 +87,6 @@ namespace Microsoft.AspNetCore.Server.Kestrel.Http
InitializeStreams(messageBody);
_abortedCts = null;
_manuallySetRequestAbortToken = null;
var context = _application.CreateContext(this);
try
{
@ -164,8 +161,6 @@ namespace Microsoft.AspNetCore.Server.Kestrel.Http
{
await TryProduceInvalidRequestResponse();
_abortedCts = null;
// If _requestAborted is set, the connection has already been closed.
if (Volatile.Read(ref _requestAborted) == 0)
{