From 318a1b4df2c23d7cb8d33aa53f3f1dcf4ec829c0 Mon Sep 17 00:00:00 2001 From: Mayuki Sawatari Date: Wed, 24 Nov 2021 10:56:52 +0900 Subject: [PATCH] Ignore exception on disconnection by STREAM_RST --- src/MagicOnion.Server/Hubs/StreamingHub.cs | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/src/MagicOnion.Server/Hubs/StreamingHub.cs b/src/MagicOnion.Server/Hubs/StreamingHub.cs index a861dd69c..51a39ed26 100644 --- a/src/MagicOnion.Server/Hubs/StreamingHub.cs +++ b/src/MagicOnion.Server/Hubs/StreamingHub.cs @@ -5,6 +5,9 @@ using System.Threading.Tasks; using MagicOnion.Utils; using Microsoft.AspNetCore.Connections; +using Microsoft.AspNetCore.Http; +using Microsoft.Extensions.DependencyInjection; +using Microsoft.AspNetCore.Http.Features; namespace MagicOnion.Server.Hubs { @@ -109,6 +112,17 @@ public async Task> Connect() // NOTE: If DuplexStreaming is disconnected by the client, IOException will be thrown. // However, such behavior is expected. the exception can be ignored. } + catch (IOException ex) + { + // NOTE: If the connection closed with STREAM_RST, PipeReader throws an IOException. + // However, such behavior is expected. the exception can be ignored. + // https://github.com/dotnet/aspnetcore/blob/v6.0.0/src/Servers/Kestrel/Core/src/Internal/Http2/Http2Stream.cs#L516-L523 + var httpRequestLifetimeFeature = this.Context.ServiceProvider.GetService()?.HttpContext.Features.Get(); + if (httpRequestLifetimeFeature is null || httpRequestLifetimeFeature.RequestAborted.IsCancellationRequested) + { + throw; + } + } finally { Context.CompleteStreamingHub();