runtime: Endless HttpMessageHandler cleanup cycle with C# cmdlets using .NET generic host
area-Extensions-HttpClientFactory
Hi there,
I have a c# PowerShell module with cmdlets that make use of the .NET generic host. It uses .NET standard 2.1.
Unfortunately after a few calls to the cmdlets I start to get endless debug messages in the PowerShell terminal…
dbug: Microsoft.Extensions.Http.DefaultHttpClientFactory[100]
Starting HttpMessageHandler cleanup cycle with 1 items
I looked at this thread (https://github.com/aspnet/HttpClientFactory/issues/165) and used JetBrains’ dotMemory to force a garbage collection but it doesn’t fix it.
I did the shortest path analysis below but to be honest I’m not sure entirely how to interpret it.
Any help appreciated…

About this issue
- Original URL
- State: closed
- Created 7 years ago
- Reactions: 2
- Comments: 31 (11 by maintainers)
This works as expected.
The cleanup can only process the handlers that were garbage collected. Until your app triggers a GC, the clean up is not able to verify that the handlers are not used anymore.
For the illustration purposes, here is an example of manually triggering Full GC (full src in the end of the post):
Which shows that after GC the handler gets successfully processed and disposed:
If GC doesn’t help, it is possible a handler is erroneously rooted (saved in a field or in a variable) for the prolonged period of time, so it can’t be collected in time.
You can see that
weak-refgot cleaned up right away, whilerootedhad to wait until it was not rooted anymoreComplete runnable example
Still no solution on this? All my services are transient but everytime HttpMessageHandler gets expired after 2 minutes - it justs stacks items for cleanup cycle and nothing is processed but sometimes after stacking some more items its just randomly processes some of (but not all) remaining items and overall unprocessed items keeps increasing
And after long time of spamming in logs about cleanups it just sometimes cleanup them all. Is it intentional behaviour and I just have to supress these logs?
I believe the logging is by-design. It looks to me like your Service is keeping a reference to the HttpClient and that will keep the handler rooted. The service is registered as a singleton which I think means the service will be created once and kept alive for the lifetime of the host and the host will be kept alive by the cmdlet instance. The logging is just because you’ve enabled debug level logging and this debug statement is telling you the handler is still rooted by something after time has passed.
/cc @davidfowl @maryamariyan to check my understanding here. FYI @dotnet/ncl