diff --git a/src/Microsoft.AspNetCore.Server.Kestrel/Http/Frame.cs b/src/Microsoft.AspNetCore.Server.Kestrel/Http/Frame.cs index 0a9eb6870c..e4f0b419ec 100644 --- a/src/Microsoft.AspNetCore.Server.Kestrel/Http/Frame.cs +++ b/src/Microsoft.AspNetCore.Server.Kestrel/Http/Frame.cs @@ -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(); diff --git a/src/Microsoft.AspNetCore.Server.Kestrel/Http/FrameOfT.cs b/src/Microsoft.AspNetCore.Server.Kestrel/Http/FrameOfT.cs index 13a04dea49..c11150fadc 100644 --- a/src/Microsoft.AspNetCore.Server.Kestrel/Http/FrameOfT.cs +++ b/src/Microsoft.AspNetCore.Server.Kestrel/Http/FrameOfT.cs @@ -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) {