google-cloud-dotnet: [BigQuery/Storage Write API] Default GrpcAdapter Memory Leak on Server 2022

Tangentially Related Context: https://github.com/googleapis/google-cloud-dotnet/issues/10034

Headline

I’ll start with the fix/workaround:-

Switching from the default GrpcAdapter to GrpcCoreAdapter.Instance as per this comment from a different issue solved the slow memory leak.

Longer explanation

We are in the process of upgrading all of production servers (we are on-prem) from Server 2019 to Server 2022. We’ve rolled out 2022 with http3 enabled as per this post.

We started to notice a new slow leak on the newly upgraded 2022 boxes:-

image

You’ll notice that the grey line remains flat because that box was still on 2019.

We hooked into AppDomain.CurrentDomain.FirstChanceException and saw that 2022 was throwing a lot of QUIC errors:-

System.Threading.Channels.ChannelClosedException: The channel has been closed.
 ---> System.Net.Quic.QuicException: The connection timed out from inactivity.
   at System.Net.Quic.QuicConnection.HandleEventShutdownInitiatedByTransport(_SHUTDOWN_INITIATED_BY_TRANSPORT_e__Struct& data)
   at System.Net.Quic.QuicConnection.HandleConnectionEvent(QUIC_CONNECTION_EVENT& connectionEvent)
   at System.Net.Quic.QuicConnection.NativeCallback(QUIC_HANDLE* connection, Void* context, QUIC_CONNECTION_EVENT* connectionEvent)
--- End of stack trace from previous location ---
 
   --- End of inner exception stack trace ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
System.Net.Quic.QuicException: The connection timed out from inactivity.
   at System.Net.Quic.QuicConnection.HandleEventShutdownInitiatedByTransport(_SHUTDOWN_INITIATED_BY_TRANSPORT_e__Struct& data)
   at System.Net.Quic.QuicConnection.HandleConnectionEvent(QUIC_CONNECTION_EVENT& connectionEvent)
   at System.Net.Quic.QuicConnection.NativeCallback(QUIC_HANDLE* connection, Void* context, QUIC_CONNECTION_EVENT* connectionEvent)
--- End of stack trace from previous location ---
   at System.Net.Quic.QuicConnection.AcceptInboundStreamAsync(CancellationToken cancellationToken)
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()

We tried turning off (including a reboot) EnableAltSvc but the leak still occurred. Then we tried the same with EnableHttp3 but no joy. This was because naively we thought http3 == quic.

Eventually, I remembered from this comment that there was a suspicion of the default GrpcAdapater was the cause of that issue. So we gave that a shot:-

<PackageReference Include="Grpc.Core" Version="2.46.6" />

And the slow memory leak was fixed (deployed just before the weekend) hence the above graph flatlines at the start of the weekend.

The leak state took a little time to reproduce; eventually I switched to using an EventListener to watch for any QUIC activity because my [broad] theory was basically QUIC was somehow causing the memory leak.

public sealed class MyListener : EventListener
{
  protected override void OnEventSourceCreated(EventSource eventSource)
  {
    if ("Private.InternalDiagnostics.System.Net.Quic".Equals(eventSource.Name))
    {
      EnableEvents(eventSource, EventLevel.LogAlways, EventKeywords.All);
    }
  }

  protected override void OnEventWritten(EventWrittenEventArgs eventData)
  {
    Console.WriteLine($"{DateTime.UtcNow:ss:fff} {eventData.EventName}: " +
      string.Join(' ', eventData.PayloadNames!.Zip(eventData.Payload!).Select(pair => $"{pair.First}={pair.Second}")));
  
  }
}

I stopped digging once the problem was resolved; my best guess is that somehow those QUIC errors aren’t disposing of resources and just slowly leaks. The actual application tier code hasn’t changed at all since I opened the previous issue.

Happy to provide more details/logs/diagnostics.

Environment details

  • OS: Upgrading from Server 2019 to Server 2022.
  • .NET version: dotnet-hosting-8.0.6-win.exe
  • Package name and version:
<PackageReference Include="Google.Cloud.BigQuery.Storage.V1" Version="3.6.0" />
<PackageReference Include="Google.Protobuf" Version="3.22.5" />
<PackageReference Include="Google.Protobuf.Tools" Version="3.22.5" />

About this issue

  • Original URL
  • State: closed
  • Created 2 years ago
  • Comments: 19

Commits related to this issue

Most upvoted comments

Will do - thanks so much for testing things so quickly. We’ll keep an eye on the whole QUIC situation… it sounds like we may well want to work out how to disable it by default. (Unfortunately, that’s easier said than done, as the “right” HttpHandler to use is a fairly involved decision.)

Hmm… that’s deeper than I normally go, to be honest - but I will say we’ve seen a few issues with QUIC recently. All the more reason to disable it in the handler 😃

Am I correct in thinking that ExactVersionHandler forces to HTTP2 and therefore skips the QUIC path entirely?

Yes. (I believe the request contains HTTP/2.0 by default, and this basically says “and don’t upgrade it”.)

ExactVersionHandler also solves the problem

Excellent! I would personally go that route. While using Grpc.Core is still supported, it’s definitely legacy - it’ll be years before we are able to stop supporting it completely, but that’s the direction of travel. In time I absolutely expect that we’ll make it easier to modify the Grpc.Net.Client handler (ideally making it simple to control keepalive, specify the HTTP version etc) to avoid these problems - but for now it sounds like the workaround is the way to go.

We have server-to-server communication.

Yes, but the BigQueryStorageClient is still acting as a client, therefore any settings which are about serving HTTP/3 are probably irrelevant to whether the client uses HTTP/3.

Yes by turning off HTTP3 I meant reversing the instructions here. So disabling http3 on OS level.

That post is all about acting as an HTTP/3 server. I don’t know for sure, but I’d strongly suspect that it has no effect on clients.

Ah, that is nice. I did have a play around with Options API but it was not immediately apparent that it was done like that. I was thinking I would have the pass the delegating handler in. I’m happy to try that.

Great, thank you.

The code hasn’t changed since the previous issue. But happy to provide a minimal example. This may take a while.

No hurry - but it’s much appreciated 😃