Skip to content

Wait to dispose RequestAborted CTS #4447

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Jan 15, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions eng/PatchConfig.props
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ Later on, this will be checked using this condition:
Microsoft.AspNetCore.Authentication.Google;
Microsoft.AspNetCore.Http;
Microsoft.AspNetCore.Server.IIS;
Microsoft.AspNetCore.Server.Kestrel.Core;
java:signalr;
</PackagesInPatch>
</PropertyGroup>
Expand Down
36 changes: 16 additions & 20 deletions src/Servers/Kestrel/Core/src/Internal/Http/HttpProtocol.cs
Original file line number Diff line number Diff line change
Expand Up @@ -348,11 +348,8 @@ public void Reset()
// Lock to prevent CancelRequestAbortedToken from attempting to cancel an disposed CTS.
lock (_abortLock)
{
if (!_requestAborted)
{
_abortedCts?.Dispose();
_abortedCts = null;
}
_abortedCts?.Dispose();
_abortedCts = null;
}

_requestHeadersParsed = 0;
Expand Down Expand Up @@ -394,15 +391,16 @@ protected virtual bool BeginRead(out ValueTask<ReadResult> awaitable)

private void CancelRequestAbortedToken()
{
try
{
_abortedCts.Cancel();
_abortedCts.Dispose();
_abortedCts = null;
}
catch (Exception ex)
lock (_abortLock)
{
Log.ApplicationError(ConnectionId, TraceIdentifier, ex);
try
{
_abortedCts?.Cancel();
}
catch (Exception ex)
{
Log.ApplicationError(ConnectionId, TraceIdentifier, ex);
}
}
}

Expand All @@ -416,12 +414,12 @@ protected void AbortRequest()
}

_requestAborted = true;
}

if (_abortedCts != null)
{
// Potentially calling user code. CancelRequestAbortedToken logs any exceptions.
ServiceContext.Scheduler.Schedule(state => ((HttpProtocol)state).CancelRequestAbortedToken(), this);
if (_abortedCts != null && !_preventRequestAbortedCancellation)
{
// Potentially calling user code. CancelRequestAbortedToken logs any exceptions.
ServiceContext.Scheduler.Schedule(state => ((HttpProtocol)state).CancelRequestAbortedToken(), this);
}
}
}

Expand All @@ -441,8 +439,6 @@ private void PreventRequestAbortedCancellation()
}

_preventRequestAbortedCancellation = true;
_abortedCts?.Dispose();
_abortedCts = null;
}
}

Expand Down
25 changes: 24 additions & 1 deletion src/Servers/Kestrel/Core/test/Http1ConnectionTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,11 @@ public Http1ConnectionTests()
var connectionFeatures = new FeatureCollection();
connectionFeatures.Set(Mock.Of<IConnectionLifetimeFeature>());

_serviceContext = new TestServiceContext();
_serviceContext = new TestServiceContext()
{
Scheduler = PipeScheduler.Inline
};

_timeoutControl = new Mock<ITimeoutControl>();
_http1ConnectionContext = new HttpConnectionContext
{
Expand Down Expand Up @@ -724,6 +728,25 @@ public async Task RequestAbortedTokenIsResetBeforeLastWriteWithChunkedEncoding()
Assert.False(_http1Connection.RequestAborted.IsCancellationRequested);
}

[Fact]
public void RequestAbortedTokenIsFullyUsableAfterCancellation()
{
var originalToken = _http1Connection.RequestAborted;
var originalRegistration = originalToken.Register(() => { });

_http1Connection.Abort(new ConnectionAbortedException());

Assert.True(originalToken.WaitHandle.WaitOne(TestConstants.DefaultTimeout));
Assert.True(_http1Connection.RequestAborted.WaitHandle.WaitOne(TestConstants.DefaultTimeout));

#if NETCOREAPP2_2
Assert.Equal(originalToken, originalRegistration.Token);
#elif NET461
#else
#error Target framework needs to be updated
#endif
}

[Fact]
public async Task ExceptionDetailNotIncludedWhenLogLevelInformationNotEnabled()
{
Expand Down