Skip to content

Revert "Wait to dispose RequestAborted CTS (#4447)" #6994

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 2 commits into from
Jan 24, 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: 0 additions & 1 deletion eng/PatchConfig.props
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ Later on, this will be checked using this condition:
Microsoft.AspNetCore.Mvc.Core;
Microsoft.AspNetCore.Routing;
Microsoft.AspNetCore.Server.IIS;
Microsoft.AspNetCore.Server.Kestrel.Core;
java:signalr;
</PackagesInPatch>
</PropertyGroup>
Expand Down
36 changes: 20 additions & 16 deletions src/Servers/Kestrel/Core/src/Internal/Http/HttpProtocol.cs
Original file line number Diff line number Diff line change
Expand Up @@ -348,8 +348,11 @@ public void Reset()
// Lock to prevent CancelRequestAbortedToken from attempting to cancel an disposed CTS.
lock (_abortLock)
{
_abortedCts?.Dispose();
_abortedCts = null;
if (!_requestAborted)
{
_abortedCts?.Dispose();
_abortedCts = null;
}
}

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

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

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

_requestAborted = true;
}

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

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

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

Expand Down
6 changes: 4 additions & 2 deletions src/Servers/Kestrel/Core/test/Http1ConnectionTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -729,14 +729,16 @@ public async Task RequestAbortedTokenIsResetBeforeLastWriteWithChunkedEncoding()
}

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

_http1Connection.Abort(new ConnectionAbortedException());

Assert.True(originalToken.WaitHandle.WaitOne(TestConstants.DefaultTimeout));
// The following line will throw an ODE because the original CTS backing the token has been diposed.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: diposed -> disposed

// See https://github.com/aspnet/AspNetCore/pull/4447 for the history behind this test.
//Assert.True(originalToken.WaitHandle.WaitOne(TestConstants.DefaultTimeout));
Assert.True(_http1Connection.RequestAborted.WaitHandle.WaitOne(TestConstants.DefaultTimeout));

#if NETCOREAPP2_2
Expand Down