Skip to content

Commit c9af606

Browse files
authored
Improve abort handling (#41263)
1 parent 4eef6a1 commit c9af606

File tree

3 files changed

+13
-6
lines changed

3 files changed

+13
-6
lines changed

src/Servers/IIS/IIS/src/Core/IISHttpContext.IO.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,11 @@ private async Task WriteBody(bool flush = false)
164164
var buffer = result.Buffer;
165165
try
166166
{
167+
if (_bodyOutput.Aborted)
168+
{
169+
break;
170+
}
171+
167172
if (!buffer.IsEmpty)
168173
{
169174
await AsyncIO!.WriteAsync(buffer);

src/Servers/IIS/IIS/src/Core/OutputProducer.cs

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,10 @@ namespace Microsoft.AspNetCore.Server.IIS.Core;
99

1010
internal class OutputProducer
1111
{
12-
// This locks access to _completed.
12+
// This locks access to _completed and _aborted.
1313
private readonly object _contextLock = new object();
1414
private bool _completed;
15+
private volatile bool _aborted;
1516

1617
private readonly Pipe _pipe;
1718

@@ -29,6 +30,8 @@ public OutputProducer(Pipe pipe)
2930

3031
public PipeReader Reader => _pipe.Reader;
3132

33+
public bool Aborted => _aborted;
34+
3235
public Task FlushAsync(CancellationToken cancellationToken)
3336
{
3437
_pipe.Reader.CancelPendingRead();
@@ -40,7 +43,7 @@ public void Complete()
4043
{
4144
lock (_contextLock)
4245
{
43-
if (_completed)
46+
if (_completed || _aborted)
4447
{
4548
return;
4649
}
@@ -54,12 +57,12 @@ public void Abort()
5457
{
5558
lock (_contextLock)
5659
{
57-
if (_completed)
60+
if (_completed || _aborted)
5861
{
5962
return;
6063
}
6164

62-
_completed = true;
65+
_aborted = true;
6366

6467
_pipe.Reader.CancelPendingRead();
6568
_pipe.Writer.Complete();
@@ -70,7 +73,7 @@ public Task WriteAsync(ReadOnlyMemory<byte> buffer, CancellationToken cancellati
7073
{
7174
lock (_contextLock)
7275
{
73-
if (_completed)
76+
if (_completed || _aborted)
7477
{
7578
return Task.CompletedTask;
7679
}

src/Servers/IIS/IIS/test/IIS.Tests/ResponseAbortTests.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@ public async Task ClosesWithoutSendingAnything()
3434
}
3535

3636
[ConditionalFact]
37-
[QuarantinedTest("https://github.com/dotnet/aspnetcore/issues/31404")]
3837
public async Task ClosesAfterDataSent()
3938
{
4039
var bodyReceived = CreateTaskCompletionSource();

0 commit comments

Comments
 (0)