Skip to content

High native memory usage in certificate revocation checking #52577

Closed

Description

@alexrosenfeld10

Description

Signs point to a memory leak in the Service Point Manager when the CheckCertificateRevocationList property is set to true and there is an expired cert

Configuration

netcoreapp3.1, net5.0 running inside mcr.microsoft.com/dotnet/core/aspnet:3.1-buster-slim and mcr.microsoft.com/dotnet/aspnet:5.0-buster-slim

Regression?

Not sure, maybe?

Other information

On April 12th I and @tdg5 noticed many services that we work on and maintain started using orders of magnitude more memory, from around 500mb to 4.5gb give or take, increasing rapidly and then ultimately receiving an OOMKill from k8s.

As we debugged the issue, we tracked it down to this line in the C# Snowflake connector. We maintain a private fork of the C# connector and removed that line from my fork. After doing so, all of the memory pressure returned to normal.

The strange part about this is that the memory pressure spiked on running pods, there was no new code or new deployments, it just started seemingly randomly. On further investigation (thanks @tdg5), this seems like a likely culprit: https://discuss.newrelic.com/t/important-upcoming-new-relic-server-certificate-update-will-impact-most-users-of-java-agent-version-6-1-and-a-few-users-of-java-agent-version-6-2-0-to-6-4-2/141711 . All of our services also use New Relic, so.. signs point to this being an issue. Initially we looked into debugging the problem with them, see newrelic/newrelic-dotnet-agent#544, but it seems to be more of a .net problem with the Service Point Manager.

Activity

  1. alexrosenfeld10 commented on May 7, 2021

    @alexrosenfeld10
    Author

    Also, it's worth nothing that this seems to be an unmanaged memory leak. We only ran into the problem when setting CORECLR_ENABLE_PROFILING=1.

  2. ghost added
    untriaged
    on May 11, 2021
  3. karelz commented on May 11, 2021

    @karelz
    Member

    @alexrosenfeld10 I am a bit confused. You mention ServicePointManager on .NET Core 3.1/5.0, but your referenced code of C# Snowflake connector uses HttpClientHandler.
    BTW: On .NET Core, ServicePointManager/ServicePoint/HttpWebRequest are obsolete APIs, suboptimal and available only for basic compat. They are not used to implement HttpClient/HttpClientHandler/SocketsHttpHandler unlike on .NET Framework.

    Also, it seems the problem happens only when profiler is enabled CORECLR_ENABLE_PROFILING=1. Does it require NewRelic in the repro, or does it happen also without NewRelic attached?
    Overall we need to rule out NewRelic profiler doing something invasive and leaking the memory. For that, it would be best to provide a simplified standalone HelloWorld-style repro. Is that possible?

  4. added
    api-needs-work
    and removed
    api-needs-work
    on May 11, 2021
  5. alexrosenfeld10 commented on May 11, 2021

    @alexrosenfeld10
    Author

    @karelz thanks for the reply, I can understand some of your confusion. Allow me to further detail below:

    At the time of starting the debugging and drafting the original issues, the Snowflake Connector set these static properties on the ServicePointManager. Traveling back into the commit history, you can find them set here: https://github.com/snowflakedb/snowflake-connector-net/blob/7331e46dea0cc28d59a2660e0ee71d7d9fa4a28a/Snowflake.Data/Core/HttpUtil.cs#L40. In our testing, we've found this to be a problem in both approaches (that being, setting the property on the HttpClientHandler as well as on the ServicePointManager directly.

    Thanks for noting that they're obsolete, that's what I found in my research here as well. However.. they still caused us a serious problem here... It's more or less dumb luck that the Snowflake library is open source and we were able to make our own fork with those settings removed. If it wasn't, I guess we'd just be in support ticket land.

    On the profiling front, yes, we only found this to be an issue with NewRelic enabled. However, I'd imagine that's less to do with NewRelic's Agent and more to do with the fact that there were certs updated on the same exact day and time this memory leak started to occur in our services. In order to repro, we'd probably need:

    1. an HttpClient with the CheckCertificateRevocationList property enabled
    2. CORECLR_ENABLE_PROFILING=1
    3. Calls to a service with a revoked cert

    I am unsure of what CheckCertificateRevocationList really does under the covers when scenario 3 is the case. However, clearly something funky goes on. Hopefully you might be able to get some more ideas with this further detail?

  6. tdg5 commented on May 11, 2021

    @tdg5

    The NewRelic profiler code can be found here: https://github.com/newrelic/newrelic-dotnet-agent/tree/main/src/Agent/NewRelic/Profiler

    I've tried to grep for indications that the profiler is using something related to ServicePointManager or HttpClient or Web or Socket or Revocation, but I didn't turn up anything interesting. Any suggestions on what I might look for? I have no experience with the Profiler API or unmanaged code so I could be way off base in my search.

  7. antonfirsov commented on May 11, 2021

    @antonfirsov
    Contributor

    Unless the code in initHttpClient() runs on .NET Framework, the ServicePointManager poperties should not make any impact on HttpClient's behavior, since HttpClientHandler's underlying handler is SocketsHttpHandler which doesn't depend on the ServicePointManager stuff in any way.

    I recommend to raise an issue against snowflake-connector-net first to get a clarification on their internal logic, which is very confusing and might be buggy (intended settings not taking any effect on .NET Core). Unless there is a repro with a definite proof of an issue with SocketsHttpHandler, there is not much we can do about this.

  8. 46 remaining items

  9. wfurt commented on Jul 22, 2021

    @wfurt
    Member

    yes, @bartonjs is probably most knowledgeable in that regard. We may cache the revocation results as well but I'm note sure. If you can reproduce it with simple calling X509Chain build with revocation @MihaZupan it would clearly belong to System.Security.

  10. bartonjs commented on Jul 22, 2021

    @bartonjs
    Member

    The fact that it eventually stabilizes means it isn't a leak, per se. Since the test is doing a GC.Collect/GC.WaitForPendingFinalizers that rules out us having some largeish objects tracked by SafeHandles that got pushed out to finalization.

    My guess is that it's just down to malloc/free's "lazy" implementation (free won't give the memory back to the OS by default, it keeps it around for the next malloc) combined with how many small allocations OpenSSL requests for loading a CRL... and it probably just ends up being a while before the "ready for a future malloc" list ends up nicely aligning with when OpenSSL does need the occasional larger chunk.

    We already have a similar issue described in #55672

  11. MihaZupan commented on Jul 22, 2021

    @MihaZupan
    Member

    Right, it's not exactly a leak, but 100 MB per request seems excessive.
    It's even weirder that it keeps growing so much on subsequent requests to the same host.

  12. alexrosenfeld10 commented on Jul 22, 2021

    @alexrosenfeld10
    Author

    Thanks for putting eyes on this folks. As a user here, I agree with @MihaZupan, the growth per-request is the big problem for apps. The fact that this was caused by calls happening inside our APM tooling (calls that happen a lot in the background) ballooned our memory pretty darn fast. We saw up to 8GB after a few minutes of running sometimes.

  13. wfurt commented on Jul 22, 2021

    @wfurt
    Member

    if there is simple repro, one can p/invoke to https://man7.org/linux/man-pages/man3/mallinfo.3.html
    That would provide info to if the memory is still used or if the process size is outcome of fragmented memory or something else.

  14. aik-jahoda commented on Sep 1, 2021

    @aik-jahoda
    Contributor

    Tested @MihaZupan minimal reproduction and wasn't able to see such terrible allocation.
    testes on:

    - .net core 3.1 .net 5
    WSL Ubuntu 20.04
    NoCheck
    0: 47 MB private, 135 KB managed
    1: 77 MB private, 253 KB managed
    2: 94 MB private, 270 KB managed
    3: 94 MB private, 290 KB managed
    4: 95 MB private, 283 KB managed
    5: 95 MB private, 284 KB managed
    6: 95 MB private, 284 KB managed
    7: 95 MB private, 287 KB managed
    8: 95 MB private, 304 KB managed
    9: 95 MB private, 303 KB managed
    10: 95 MB private, 320 KB managed
    11: 95 MB private, 330 KB managed
    12: 95 MB private, 317 KB managed
    13: 95 MB private, 318 KB managed
    14: 95 MB private, 334 KB managed
    15: 95 MB private, 335 KB managed
    16: 95 MB private, 321 KB managed
    17: 95 MB private, 321 KB managed
    18: 95 MB private, 324 KB managed
    19: 95 MB private, 327 KB managed
    20: 95 MB private, 345 KB managed
    NoCheck
    0: 55 MB private, 146 KB managed
    1: 101 MB private, 282 KB managed
    2: 118 MB private, 292 KB managed
    3: 118 MB private, 313 KB managed
    4: 119 MB private, 325 KB managed
    5: 119 MB private, 335 KB managed
    6: 119 MB private, 347 KB managed
    7: 119 MB private, 349 KB managed
    8: 119 MB private, 363 KB managed
    9: 119 MB private, 367 KB managed
    0: 119 MB private, 361 KB managed
    1: 120 MB private, 375 KB managed
    2: 120 MB private, 370 KB managed
    3: 120 MB private, 379 KB managed
    4: 120 MB private, 371 KB managed
    5: 121 MB private, 381 KB managed
    6: 121 MB private, 383 KB managed
    7: 121 MB private, 388 KB managed
    8: 121 MB private, 387 KB managed
    9: 121 MB private, 390 KB managed
    0: 121 MB private, 390 KB managed

    I observed same results on Ubuntu 20.04 and Docker mcr.microsoft.com/dotnet/core/aspnet:3.1-buster-slim, mcr.microsoft.com/dotnet/aspnet:5.0-buster-slim

    It is interesting the issue is not even reproducible on the same docker images mentioned in the issue description.

    @alexrosenfeld10 - can you confirm the issue persists and it is reproducible with @MihaZupan repro?

  15. alexrosenfeld10 commented on Sep 5, 2021

    @alexrosenfeld10
    Author

    Hey, apologies but I don't currently have the time in my day to day to spend on this anymore.

    Right now I don't see the issues in my app since removing the cert revocation check setting. If I find more time I can try to look into the other repro, but I don't think that will be anytime soon.

  16. self-assigned this
    on May 26, 2022
  17. rzikm commented on May 26, 2022

    @rzikm
    Member

    I have tried to reproduce this issue multiple times with different setups and did not observe any unbounded growth of memory. I will close this issue but feel free to reopen this if more info is available.

  18. removed their assignment
    on May 26, 2022
  19. Vandersteen commented on Jun 23, 2022

    @Vandersteen

    This might be related: #57213

  20. alexrosenfeld10 commented on Jun 23, 2022

    @alexrosenfeld10
    Author

    Definitely seems related, especially if the solution is disabling the same property

  21. ghost locked as resolved and limited conversation to collaborators on Jul 23, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    area-System.Net.Securitybugos-linuxtenet-performance

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions