From bd925b109a7957c8650839a4f3451cdd153b8999 Mon Sep 17 00:00:00 2001 From: James Newton-King Date: Wed, 13 Jul 2022 16:36:52 +0800 Subject: [PATCH] HTTP/3: Avoid ConnectionAbortedException allocations --- src/Servers/Kestrel/Core/src/Internal/Http3/Http3Stream.cs | 6 +++--- .../Transport.Quic/src/Internal/QuicStreamContext.cs | 5 +++-- 2 files changed, 6 insertions(+), 5 deletions(-) diff --git a/src/Servers/Kestrel/Core/src/Internal/Http3/Http3Stream.cs b/src/Servers/Kestrel/Core/src/Internal/Http3/Http3Stream.cs index 78c7e4a4971f..6ab76daa2b43 100644 --- a/src/Servers/Kestrel/Core/src/Internal/Http3/Http3Stream.cs +++ b/src/Servers/Kestrel/Core/src/Internal/Http3/Http3Stream.cs @@ -637,9 +637,6 @@ public async Task ProcessRequestAsync(IHttpApplication appli } finally { - var streamError = error as ConnectionAbortedException - ?? new ConnectionAbortedException("The stream has completed.", error!); - await Input.CompleteAsync(); // Once the header is finished being received then the app has started. @@ -692,6 +689,9 @@ public async Task ProcessRequestAsync(IHttpApplication appli } catch { + var streamError = error as ConnectionAbortedException + ?? new ConnectionAbortedException("The stream has completed.", error!); + Abort(streamError, Http3ErrorCode.ProtocolError); throw; } diff --git a/src/Servers/Kestrel/Transport.Quic/src/Internal/QuicStreamContext.cs b/src/Servers/Kestrel/Transport.Quic/src/Internal/QuicStreamContext.cs index ec2b41075226..dcf5ce4f1f66 100644 --- a/src/Servers/Kestrel/Transport.Quic/src/Internal/QuicStreamContext.cs +++ b/src/Servers/Kestrel/Transport.Quic/src/Internal/QuicStreamContext.cs @@ -14,6 +14,8 @@ namespace Microsoft.AspNetCore.Server.Kestrel.Transport.Quic.Internal; internal partial class QuicStreamContext : TransportConnection, IPooledStream, IDisposable { + private static readonly ConnectionAbortedException SendGracefullyCompletedException = new ConnectionAbortedException("The QUIC transport's send loop completed gracefully."); + // Internal for testing. internal Task _processingTask = Task.CompletedTask; @@ -499,8 +501,7 @@ private void ShutdownWrite(Exception? shutdownReason) { lock (_shutdownLock) { - // TODO: Exception is always allocated. Consider only allocating if receive hasn't completed. - _shutdownReason = shutdownReason ?? new ConnectionAbortedException("The QUIC transport's send loop completed gracefully."); + _shutdownReason = shutdownReason ?? SendGracefullyCompletedException; QuicLog.StreamShutdownWrite(_log, this, _shutdownReason.Message); _stream.Shutdown();