microsoft / microsoft/aspire

HttpServiceEndpointResolver timers leak on IHttpClientFactory handler rotation when using AddServiceDiscovery()

Open
#15,673 4 comments 3 reactions 0 assignees View on GitHub
area-service-discovery
Dominant language
C#
Stars
6.3k
Forks
991
Avg merge
2d 15h
Merged PRs (30d)
196

Description

### Is there an existing issue for this?

- [x] I have searched the existing issues

### Describe the bug

Hey folks,

When we apply `AddServiceDiscovery();` to `builder.Services` and especially when we apply `AddServiceDiscovery();` on `IHttpClientBuilder` in `ConfigureHttpClientDefaults` in application which uses `IHttpClientFactory`, it seems that underlying `HttpServiceEndpointResolver` never disposes its internal `System.Threading.Timer`. Over days, this accumulates, causing (steadily) growing memory and CPU usage. _(growing CPU usage in particular)_

If I'm correct, It has to do something with HandlerLifetime and HandlerChain of a HttpClient, because leak becomes even more noticeable if you lower the amount for HandlerLifetime, from default 2 minutes, to seconds for example.

### Expected Behavior

When `IHttpClientFactory` retires and his `HttpClient` gets disposed, all resources attached to them, including `HttpServiceEndpointResolver `and its timer should be disposed.

### Steps To Reproduce

[https://github.com/kr3s0/LeakyClient](url) is a minimalistic project which reproduces this issue (repro).

### Exceptions (if any)

No visible exceptions

### .NET Version info

- .NET 10
- Aspire 13
- Microsoft.Extensions.ServiceDiscovery 9.5.1
- Microsoft.Extensions.Http.Resilience 9.10.0
- Running in Kubernetes (Linux containers)

### Anything else?

Image

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.