Repository navigation
Description
Description
Since #131275 (merged 2026-07-30), http.client.active_requests and http.client.open_connections are ObservableUpDownCounter<long> instruments. They are created in the constructors of MetricsHandler and SocketsHttpHandlerMetrics, that is, once per handler instance, on the shared System.Net.Http meter:
src/libraries/System.Net.Http/src/System/Net/Http/Metrics/MetricsHandler.cs:meter.CreateObservableUpDownCounter<long>("http.client.active_requests", observeValues: _activeRequestsTracker.GetMeasurements, ...)src/libraries/System.Net.Http/src/System/Net/Http/SocketsHttpHandler/Metrics/SocketsHttpHandlerMetrics.cs: the same forhttp.client.open_connections
The comment in MetricsHandler says "Meter has a cache for the instruments it owns", but Meter only caches non-observable instruments (GetOrCreateInstrument is used for Counter, UpDownCounter, Histogram, Gauge). CreateObservableUpDownCounter always allocates a new instrument. Instruments are removed only when the Meter is disposed, and the System.Net.Http meter lives for the lifetime of the process (or of the IMeterFactory). Disposing the handler does not remove its instruments.
With IHttpClientFactory the primary handler is recreated every 2 minutes by default, so every handler that sent at least one request adds two instruments that are never released, together with their trackers and the listeners' per-instrument state (for example OpenTelemetry's metric state). Every collection also invokes all accumulated observeValues callbacks, so collection cost grows with uptime.
Before #131275 these were UpDownCounter<long> instruments, which Meter caches by name, so there was one instrument per meter. The code is the same on main and release/11.0.
Reproduction Steps
- Subscribe a
MeterListenerto theSystem.Net.Httpmeter and countInstrumentPublishedcallbacks forhttp.client.active_requests. - In a loop, create a new
SocketsHttpHandler+HttpClient, send one request, dispose both. - Alternatively: run an ASP.NET Core app that uses
IHttpClientFactorywith default settings and an OpenTelemetry metrics exporter, make periodic outbound requests, and take memory dumps some hours apart.
Expected behavior
The number of http.client.active_requests / http.client.open_connections instruments stays bounded (one per meter, as with UpDownCounter before #131275), or a handler's instruments are released when the handler is disposed.
Actual behavior
Two new instruments per handler, kept until the process exits. In a production service on .NET 11 RC 2 a memory dump after several days of uptime showed about 33 700 ObservableUpDownCounter<long> instances rooted from Meter.s_allMeters → meter → List<Instrument> (about 51 MB), plus about 11 MB of OpenTelemetry per-instrument state for them.
Regression?
Yes. Not present in .NET 10, where these instruments are UpDownCounter<long>. Introduced by #131275.
Known Workarounds
Stop recreating handlers so the number of instruments is bounded by the number of named clients: services.ConfigureHttpClientDefaults(b => b.SetHandlerLifetime(Timeout.InfiniteTimeSpan).UseSocketsHttpHandler((h, _) => h.PooledConnectionLifetime = TimeSpan.FromMinutes(2))). This does not fix the growth for code that creates handlers directly.
Configuration
.NET 11.0 RC 2, Linux x64 containers, ASP.NET Core with IHttpClientFactory and OpenTelemetry metrics (OTLP exporter).
Other information
Possible fixes: create the observable instruments once per meter (for example a static or per-meter holder that aggregates trackers of all live handlers), or let the handler unregister its tracker on dispose and keep a single callback per meter.
Activity
dotnet-policy-service commentedon Oct 24, 2026
Tagging subscribers to this area: @karelz, @dotnet/ncl
See info in area-owners.md if you want to be subscribed.
asad24-dev commentedon Oct 26, 2026
I've reproduced this on current main and have a fix that shares the observable instruments and their trackers per meter. The regression tests are passing, including concurrent handler initialization and aggregation across handlers.
I'll open a PR shortly.
nikolay-collab commentedon Nov 2, 2026
We've run into the same issue, please, we need this change shipped for the .net11 before it's released
ManickaP commentedon Nov 3, 2026
@nikolay-collab can you share some real numbers how is this hurting you? In terms of compute or money. We need real world business impact to reason why it should be serviced.
Also note that even if we accept the fix a have enough data to support a backport, we are already past deadline for 11 GA. It would happen in some follow up servicing at earliest.
nikolay-collab commentedon Nov 3, 2026
@ManickaP
Two common patterns trigger it, and neither is a misuse:
IHttpClientFactorywith default settings. The primary handler is rotated every 2 minutes, so each named client used at least that often adds 30 handlers, and 60 instruments, per hour.DefaultInstanceProfileAWSCredentialsrenews the credentials every 2 minutes with 4 requests to the instance metadata service, andAWSSDKUtils.CreateClientbuilds a newHttpClient(new HttpClientHandler())for each one. That is 120 handlers per hour from code the application does not own, and it is where our production growth comes from.Each leaked handler costs about the same on both paths: 3 to 3.5 KB that stays reachable whether or not a listener enables the two instruments, plus 304 bytes and 0.7 to 1 µs on every
RecordObservableInstruments()pass when one does.IHttpClientFactoryreproduction: 100 named clients, default settings exceptSetHandlerLifetime(TimeSpan.FromSeconds(1))to compress time, and aMeterListenerenabling theSystem.Net.Httpinstruments.ManickaP commentedon Nov 3, 2026
Are these numbers coming from your production?
ManickaP commentedon Nov 3, 2026
Also note that I am now considering reverting #131275 instead of trying to fix this by re-implementing yet another Instrument related logic in sockets handler. So neither of the 2 competing PRs might be merged in the end.
I will discuss the options with diagnostics team and with my team and I'll share when we come up with a decision.
asad24-dev commentedon Nov 3, 2026
Thanks for the update, @ManickaP . That makes sense. I've prepared a local revision of #136493 incorporating @unsafePtr's simpler approach from #136849, with updated regression tests. I'll hold off on pushing it until you've discussed the direction with the team.
Share one HTTP handler per proxy for instance metadata and container …
normj commentedon Nov 6, 2026
@unsafePtr has sent us, the AWS .NET SDK team, a PR to work around the issue and hope to release it Monday as part of the SDK's daily release schedule.
laurihelkkula commentedon Nov 16, 2026
Hi,
We've noticed this affecting one scenario in production, and I can share some data points. We had a piece of code related to pushing log messages that runs 700k times in a day. This results in quite linear growth in memory usage of about 1,3 Gb per day.
The code was creating HttpClient instances for each invocation. We fixed it to use a shared instance and the immediate issue was resolved.
Here's my vibecoded benchmark
And measurements:
Tragetaschen commentedon Nov 19, 2026
I was bitten by that as well - a tiny service, checking reachability of services by doing an HTTP ping every 30 seconds. I recreate the SocketsHttpHandler each time, so DNS, SSL etc. are re-checked as well.
Maybe a bit pushy: I'm very confused that a memory leak in such a central component of .NET needs business cases to be fixed. In my mental model, this is simply unacceptable for the product. Period.
Tragetaschen commentedon Nov 19, 2026
To put a bit more weight behind it, but it's also mentioned in the OP already: This fully affects the recommended
IHttpClientFactorypattern as well.https://gist.github.com/Tragetaschen/30ff63b9463212ec53e195ad108744b4
With the defaults, there are two leaked meters every two minutes.
ManickaP commentedon Nov 19, 2026
We are looking into it and trying to figure out what is the best course of action here.
I'm very reluctant to introduce any more logic into HTTP metrics that is there only workaround OTel shortcomings. And honestly should just simply be part of diagnostics.
Also note that the memory leak is not substantial (100MB in 40 days if I'm reading #136418 (comment) correctly) and the impact of the original issue is likely more painful than this.
Tragetaschen commentedon Nov 20, 2026
This highly depends on the use case. It's two leaked per SocketsHttpHandler. You get one of those per named client every two minutes. My example above showed that 40MiB can be reached "over night" in a different use cases with more churn on the handler.
I've been running with the following workaround, very focused around just fully turning off the HTTP metrics. It's the first thing my Program.cs calls to control when the static class constructor of the internal MetricsHandler runs.