You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
High native memory usage in certificate revocation checking #52577
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.
@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?
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:
an HttpClient with the CheckCertificateRevocationList property enabled
CORECLR_ENABLE_PROFILING=1
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?
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.
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.
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.
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
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.
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.
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.
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.
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.
Activity
alexrosenfeld10 commentedon May 7, 2021
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.karelz commentedon May 11, 2021
@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?
alexrosenfeld10 commentedon May 11, 2021
@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 theHttpClientHandleras well as on theServicePointManagerdirectly.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:
HttpClientwith theCheckCertificateRevocationListproperty enabledCORECLR_ENABLE_PROFILING=1I am unsure of what
CheckCertificateRevocationListreally 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?tdg5 commentedon May 11, 2021
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
grepfor indications that the profiler is using something related toServicePointManagerorHttpClientorWeborSocketorRevocation, 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.antonfirsov commentedon May 11, 2021
Unless the code in
initHttpClient()runs on .NET Framework, theServicePointManagerpoperties should not make any impact onHttpClient's behavior, sinceHttpClientHandler's underlying handler isSocketsHttpHandlerwhich doesn't depend on theServicePointManagerstuff 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.
46 remaining items
wfurt commentedon Jul 22, 2021
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
X509Chainbuild with revocation @MihaZupan it would clearly belong to System.Security.bartonjs commentedon Jul 22, 2021
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
MihaZupan commentedon Jul 22, 2021
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.
alexrosenfeld10 commentedon Jul 22, 2021
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.
wfurt commentedon Jul 22, 2021
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.
aik-jahoda commentedon Sep 1, 2021
Tested @MihaZupan minimal reproduction and wasn't able to see such terrible allocation.
testes on:
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?
alexrosenfeld10 commentedon Sep 5, 2021
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.
rzikm commentedon May 26, 2022
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.
Vandersteen commentedon Jun 23, 2022
This might be related: #57213
alexrosenfeld10 commentedon Jun 23, 2022
Definitely seems related, especially if the solution is disabling the same property