runtime icon indicating copy to clipboard operation
runtime copied to clipboard

.NET 8.0 ChannelClosedException using gRPC (Http2Connection)

Open SHAZAM-Analytics opened this issue 2 years ago • 9 comments

Description

Under .NET 8.0, if the connection is interrupted for any reason the Http2Connection FinalTeardown() method is called. This hits the Complete() method in ChannelWriter, which then fails to complete, throwing a ChannelClosedException. This exception cannot be caught by our application and so crashes the application.

This is a huge problem on .NET 8.0, via packages such as .NET gRPC, because a client can crash a server. Under .NET 7.0 this never happened.

As you can imagine there are any number of reasons that the connection can be interrupted, from network issues, network changes or problems with either the server or the client. Notably, if a client has an issue that triggers the Channel to close, this will immediately crash the server application.

Reproduction Steps

Create a call from a client to a server and interrupt it.... such as with a debugger breakpoint in a server. Wait until the connection times out and is closed and observe the client then crashing.

Expected behavior

A closed connection should not crash one (or both) of the parties in a communication. At the very least, the exception should be trappable.

Actual behavior

Crashes and hits our UnhandledException handler

       AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException);

Regression?

No issues with the same code in .NET 7.0

Known Workarounds

Revert to .NET 7.0

Configuration

This was triggered using the grpc-dotnet project. This makes using grpc a problem in .NET8.0.

Other information

Application: SA.SQA.Provision.exe CoreCLR Version: 8.0.23.53103 .NET Version: 8.0.0 Description: The process was terminated due to an unhandled exception. Exception Info: System.Threading.Channels.ChannelClosedException: The channel has been closed. at System.Net.Http.Http2Connection.FinalTeardown() at System.Net.Http.Http2Connection.Shutdown() at System.Net.Http.Http2Connection.Abort(Exception abortException) at System.Net.Http.Http2Connection.HeartBeat() at System.Net.Http.HttpConnectionPool.HeartBeat() at System.Net.Http.HttpConnectionPoolManager.HeartBeat() at System.Threading.TimerQueueTimer.Fire(Boolean isThreadPool) at System.Threading.TimerQueue.FireNextTimers() at System.Threading.ThreadPoolWorkQueue.Dispatch() at System.Threading.PortableThreadPool.WorkerThread.WorkerThreadStart()

SHAZAM-Analytics avatar Dec 05 '23 05:12 SHAZAM-Analytics

Tagging subscribers to this area: @dotnet/ncl See info in area-owners.md if you want to be subscribed.

Issue Details

Description

Under .NET 8.0, if the connection is interrupted for any reason the Http2Connection FinalTeardown() method is called. This hits the Complete() method in ChannelWriter, which then fails to complete, throwing a ChannelClosedException. This exception cannot be caught by our application and so crashes the application.

This is a huge problem on .NET 8.0, via packages such as .NET gRPC, because a client can crash a server. Under .NET 7.0 this never happened.

As you can imagine there are any number of reasons that the connection can be interrupted, from network issues, network changes or problems with either the server or the client. Notably, if a client has an issue that triggers the Channel to close, this will immediately crash the server application.

Reproduction Steps

Create a call from a client to a server and interrupt it.... such as with a debugger breakpoint in a server. Wait until the connection times out and is closed and observe the client then crashing.

Expected behavior

A closed connection should not crash one (or both) of the parties in a communication. At the very least, the exception should be trappable.

Actual behavior

Crashes and hits our UnhandledException handler

   AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException);

Regression?

No issues with the same code in .NET 7.0

Known Workarounds

Revert to .NET 7.0

Configuration

This was triggered using the grpc-dotnet project. This makes using grpc a problem in .NET8.0.

Other information

Application: SA.SQA.Provision.exe CoreCLR Version: 8.0.23.53103 .NET Version: 8.0.0 Description: The process was terminated due to an unhandled exception. Exception Info: System.Threading.Channels.ChannelClosedException: The channel has been closed. at System.Net.Http.Http2Connection.FinalTeardown() at System.Net.Http.Http2Connection.Shutdown() at System.Net.Http.Http2Connection.Abort(Exception abortException) at System.Net.Http.Http2Connection.HeartBeat() at System.Net.Http.HttpConnectionPool.HeartBeat() at System.Net.Http.HttpConnectionPoolManager.HeartBeat() at System.Threading.TimerQueueTimer.Fire(Boolean isThreadPool) at System.Threading.TimerQueue.FireNextTimers() at System.Threading.ThreadPoolWorkQueue.Dispatch() at System.Threading.PortableThreadPool.WorkerThread.WorkerThreadStart()

Author: rupreck
Assignees: -
Labels:

area-System.Net.Http

Milestone: -

ghost avatar Dec 05 '23 05:12 ghost

Seeing the 9.0.0 milestone added, is there any chance this will be addressed also for .NET8?

IonutLupsan avatar Dec 12 '23 10:12 IonutLupsan

Hi @rupreck, can you please reach out to me via email that I have on my GitHub profile? I have some questions that would be easier to cover via email. @IonutLupsan feel free to contact me via email as well.

CarnaViire avatar Dec 12 '23 16:12 CarnaViire

Fixed in main for 9.0 in #96157. Backported to 8.0 in #96158.

MihaZupan avatar Dec 19 '23 18:12 MihaZupan

Is this issue related IIS or Kestrel server?

jaydoshiforblog avatar Jan 03 '24 15:01 jaydoshiforblog

This is an issue in HttpClient, not the servers.

MihaZupan avatar Jan 03 '24 16:01 MihaZupan

Are there any workarounds for this on .NET 8 until the patch is available? Is it possible to disable KeepAlive, or would creating/disposing a new GrpcChannel for every request fix the issue?

xqrzd avatar Jan 16 '24 19:01 xqrzd

Disabling HTTP/2 pings (keeping SocketsHttpHandlr.KeepAlivePingDelay on its default of infinite) would avoid this issue.

MihaZupan avatar Jan 16 '24 20:01 MihaZupan

Fixed in main (9.0) in PR #96157 and in 8.0.2 in PR #96158.

karelz avatar Jun 24 '24 20:06 karelz