dotnet / dotnet/aspnetcore

Multiple memory leaks when restarting WebApplication

Open
#45,579 15 comments 0 reactions 1 assignee Claimed by @davidfowl View on GitHub
area-networking investigate
Dominant language
C#
Stars
38.4k
Forks
10.9k
Avg merge
2d 10h
Merged PRs (30d)
281

Description

### Is there an existing issue for this?

- [X] I have searched the existing issues

### Describe the bug

When writing an application which starts and stops an asp.net core based service multiple times, memory is leaked every time the service is stopped.

I did a little bit of analysis and the leaks I looked into were in 3 buckets. There may be more sources of memory leaks, but these are the ones I found.

Listening Socket related
There were 2 SocketException objects (presumably from the IPv4 and IPv6 listening sockets being closed) rooted with a strong handle. There were also 2 of each of several different overlapped related structures which were similarly rooted. It looks like some of the plumbing around the listening sockets aren't correctly cleaned up.

FileSystemWatcher related
These seemed to be referenced by configuration related classes. The repro I provided below has zero configuration files (no launchsettings or anything else) so these should even be instantiated in the first place. But even when they are, the callbacks should be unregistered and things cleaned up properly when the service is stopped.

Logging related
There looks to be a lot of logging related objects that are rooted. Once path I found was rooted to the static LoggingEventSource. It looked like a change handler had been registered with it (presumable to be notified if an ETW session started to request logging). This should be correctly unregistered if it's been registered. But regardless of that, I have called `ILoggingBuilder.ClearProviders()` in the repro, so I don't understand why any providers are being wired up when there's code to explicitly say to remove all providers. Is this intended behavior?

### Expected Behavior

Stopping the WebApplication/WebHost should cause all memory allocations to be dereferenced so the GC can cleanup.

### Steps To Reproduce

A simple hello world asp.net core app which is started and stopped repeatedly will demonstrate the problem. [I created a simple repro](https://github.com/mconnew/AspNetCoreMemoryLeak) using the older WebHost.CreateDefaultBuilder and a Startup class as well as the newer WebApplication.CreateBuilder way of creating an app and I see memory leaks both ways.
I have added a `Debugger.Break()` call in a loop after starting and stopping a service 100 times then running `GC.Collect(2)` twice to clean up everything possible from the heap. This allows you to use VS to capture a heap snapshot and then do a snapshot diff to see what is leaking.

### Exceptions (if any)

_No response_

### .NET Version

7.0.100

### Anything else?

This was discovered by a developer using CoreWCF in a WPF app. I believe they used to stop and restart a WCF service on .NET Framework and are attempting to achieve the same thing in .NET.

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.