Skip to content

Commit

Permalink
Merge pull request Cysharp#478 from Cysharp/hotfix/IgnoreExceptionIfD…
Browse files Browse the repository at this point in the history
…isconnected

Ignore InvalidOperationException if the connection is completed
  • Loading branch information
mayuki authored Nov 24, 2021
2 parents 6ce5183 + 1e90a5b commit da8d52e
Showing 1 changed file with 6 additions and 4 deletions.
10 changes: 6 additions & 4 deletions src/MagicOnion.Server/Hubs/StreamingHub.cs
Original file line number Diff line number Diff line change
Expand Up @@ -112,13 +112,15 @@ public async Task<DuplexStreamingResult<byte[], byte[]>> 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)
catch (Exception ex) when (ex is IOException or InvalidOperationException)
{
// NOTE: If the connection closed with STREAM_RST, PipeReader throws an IOException.
var httpRequestLifetimeFeature = this.Context.CallContext.GetHttpContext()?.Features.Get<IHttpRequestLifetimeFeature>();

// NOTE: If the connection is completed when a message is written, PipeWriter throws an InvalidOperationException.
// NOTE: If the connection is 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<IHttpContextAccessor>()?.HttpContext?.Features.Get<IHttpRequestLifetimeFeature>();
if (httpRequestLifetimeFeature is null || !httpRequestLifetimeFeature.RequestAborted.IsCancellationRequested)
if (httpRequestLifetimeFeature is null || httpRequestLifetimeFeature.RequestAborted.IsCancellationRequested is false)
{
throw;
}
Expand Down

0 comments on commit da8d52e

Please sign in to comment.