Skip to content

Commit 522705f

Browse files
authored
Merge pull request #6994 from aspnet/halter73/1531-part2
Revert "Wait to dispose RequestAborted CTS (#4447)"
2 parents f4c5ac7 + 2853b45 commit 522705f

File tree

3 files changed

+24
-19
lines changed

3 files changed

+24
-19
lines changed

eng/PatchConfig.props

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@ Later on, this will be checked using this condition:
3636
Microsoft.AspNetCore.Mvc.Core;
3737
Microsoft.AspNetCore.Routing;
3838
Microsoft.AspNetCore.Server.IIS;
39-
Microsoft.AspNetCore.Server.Kestrel.Core;
4039
java:signalr;
4140
</PackagesInPatch>
4241
</PropertyGroup>

src/Servers/Kestrel/Core/src/Internal/Http/HttpProtocol.cs

Lines changed: 20 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -348,8 +348,11 @@ public void Reset()
348348
// Lock to prevent CancelRequestAbortedToken from attempting to cancel an disposed CTS.
349349
lock (_abortLock)
350350
{
351-
_abortedCts?.Dispose();
352-
_abortedCts = null;
351+
if (!_requestAborted)
352+
{
353+
_abortedCts?.Dispose();
354+
_abortedCts = null;
355+
}
353356
}
354357

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

392395
private void CancelRequestAbortedToken()
393396
{
394-
lock (_abortLock)
397+
try
395398
{
396-
try
397-
{
398-
_abortedCts?.Cancel();
399-
}
400-
catch (Exception ex)
401-
{
402-
Log.ApplicationError(ConnectionId, TraceIdentifier, ex);
403-
}
399+
_abortedCts.Cancel();
400+
_abortedCts.Dispose();
401+
_abortedCts = null;
402+
}
403+
catch (Exception ex)
404+
{
405+
Log.ApplicationError(ConnectionId, TraceIdentifier, ex);
404406
}
405407
}
406408

@@ -414,12 +416,12 @@ protected void AbortRequest()
414416
}
415417

416418
_requestAborted = true;
419+
}
417420

418-
if (_abortedCts != null && !_preventRequestAbortedCancellation)
419-
{
420-
// Potentially calling user code. CancelRequestAbortedToken logs any exceptions.
421-
ServiceContext.Scheduler.Schedule(state => ((HttpProtocol)state).CancelRequestAbortedToken(), this);
422-
}
421+
if (_abortedCts != null)
422+
{
423+
// Potentially calling user code. CancelRequestAbortedToken logs any exceptions.
424+
ServiceContext.Scheduler.Schedule(state => ((HttpProtocol)state).CancelRequestAbortedToken(), this);
423425
}
424426
}
425427

@@ -439,6 +441,8 @@ private void PreventRequestAbortedCancellation()
439441
}
440442

441443
_preventRequestAbortedCancellation = true;
444+
_abortedCts?.Dispose();
445+
_abortedCts = null;
442446
}
443447
}
444448

src/Servers/Kestrel/Core/test/Http1ConnectionTests.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -729,14 +729,16 @@ public async Task RequestAbortedTokenIsResetBeforeLastWriteWithChunkedEncoding()
729729
}
730730

731731
[Fact]
732-
public void RequestAbortedTokenIsFullyUsableAfterCancellation()
732+
public void RequestAbortedTokenIsUsableAfterCancellation()
733733
{
734734
var originalToken = _http1Connection.RequestAborted;
735735
var originalRegistration = originalToken.Register(() => { });
736736

737737
_http1Connection.Abort(new ConnectionAbortedException());
738738

739-
Assert.True(originalToken.WaitHandle.WaitOne(TestConstants.DefaultTimeout));
739+
// The following line will throw an ODE because the original CTS backing the token has been diposed.
740+
// See https://github.com/aspnet/AspNetCore/pull/4447 for the history behind this test.
741+
//Assert.True(originalToken.WaitHandle.WaitOne(TestConstants.DefaultTimeout));
740742
Assert.True(_http1Connection.RequestAborted.WaitHandle.WaitOne(TestConstants.DefaultTimeout));
741743

742744
#if NETCOREAPP2_2

0 commit comments

Comments
 (0)