diff --git a/src/Servers/IIS/IIS/src/Core/IISHttpContext.IO.cs b/src/Servers/IIS/IIS/src/Core/IISHttpContext.IO.cs index 79ac033f6531..32843c36ab03 100644 --- a/src/Servers/IIS/IIS/src/Core/IISHttpContext.IO.cs +++ b/src/Servers/IIS/IIS/src/Core/IISHttpContext.IO.cs @@ -164,6 +164,11 @@ private async Task WriteBody(bool flush = false) var buffer = result.Buffer; try { + if (_bodyOutput.Aborted) + { + break; + } + if (!buffer.IsEmpty) { await AsyncIO!.WriteAsync(buffer); diff --git a/src/Servers/IIS/IIS/src/Core/OutputProducer.cs b/src/Servers/IIS/IIS/src/Core/OutputProducer.cs index 00934ea1b623..60a189ecd61c 100644 --- a/src/Servers/IIS/IIS/src/Core/OutputProducer.cs +++ b/src/Servers/IIS/IIS/src/Core/OutputProducer.cs @@ -9,9 +9,10 @@ namespace Microsoft.AspNetCore.Server.IIS.Core; internal class OutputProducer { - // This locks access to _completed. + // This locks access to _completed and _aborted. private readonly object _contextLock = new object(); private bool _completed; + private volatile bool _aborted; private readonly Pipe _pipe; @@ -29,6 +30,8 @@ public OutputProducer(Pipe pipe) public PipeReader Reader => _pipe.Reader; + public bool Aborted => _aborted; + public Task FlushAsync(CancellationToken cancellationToken) { _pipe.Reader.CancelPendingRead(); @@ -40,7 +43,7 @@ public void Complete() { lock (_contextLock) { - if (_completed) + if (_completed || _aborted) { return; } @@ -54,12 +57,12 @@ public void Abort() { lock (_contextLock) { - if (_completed) + if (_completed || _aborted) { return; } - _completed = true; + _aborted = true; _pipe.Reader.CancelPendingRead(); _pipe.Writer.Complete(); @@ -70,7 +73,7 @@ public Task WriteAsync(ReadOnlyMemory buffer, CancellationToken cancellati { lock (_contextLock) { - if (_completed) + if (_completed || _aborted) { return Task.CompletedTask; } diff --git a/src/Servers/IIS/IIS/test/IIS.Tests/ResponseAbortTests.cs b/src/Servers/IIS/IIS/test/IIS.Tests/ResponseAbortTests.cs index f614cdc2a12b..dc03cef8df45 100644 --- a/src/Servers/IIS/IIS/test/IIS.Tests/ResponseAbortTests.cs +++ b/src/Servers/IIS/IIS/test/IIS.Tests/ResponseAbortTests.cs @@ -34,7 +34,6 @@ public async Task ClosesWithoutSendingAnything() } [ConditionalFact] - [QuarantinedTest("https://github.com/dotnet/aspnetcore/issues/31404")] public async Task ClosesAfterDataSent() { var bodyReceived = CreateTaskCompletionSource();