-
Notifications
You must be signed in to change notification settings - Fork 5k
.NET 6+ WebSocket HttpListener - memory leak #101022
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
Tagging subscribers to this area: @dotnet/ncl |
@zxcvqwerasdf thank you for sharing the source code for the repro! I will give it a try. Did both of the windows and Linux machines you were using have similar specs (memory, number of CPU cores)? |
One is vps kvm 2 cores 4 ram, second is vps kvm 1 core 1 ram, third is virtual machine in virtualbox 8 core 16 ram. Same behavior on all specs. |
I was able to repro the memory growth on Linux with .NET 9 too (haven't tested windows). I've taken a dump after the memory consumption reached about 1.2GB. There is some managed leak, just look at the list of the GC heap objects with the largest counts - over 46000 live HttpClient instances doesn't look healthy.
Here is a summary of the memory usage, you can see that the GC heap is 830MB large:
|
@CarnaViire will you be able to take a look at it? |
@zxcvqwerasdf We have identified the issue and found workaround. We are considering a fix for this issue but we are unsure when it will be actually fixed. websocket.Dispose();
context.Response.Abort(); This code will work with both Windows and Linux. |
Result of analysis: runtime/src/libraries/System.Net.HttpListener/src/System/Net/Managed/HttpListener.Managed.cs Line 15 in fccbc77
HttpListenerContext in Dictionary indefinitely for a lifetime of HttpListener instance.In non web socket scenario it is removed by context.Response.Close(); but since this is not supposed nor documented to be called when context.Request.IsWebSocketRequest with the current codebase we have to free it by context.Response.Abort() when the particular HttpListenerComtext is no longer needed. This will call httpConnection.Close(true) to cleanup all references to given HttpConnection
Appealing and most probably correct option is to cleanup everything in |
cc: @mangod9 - this is a memory leak reported couple of months ago where the culprit was just figured out. |
Ah thanks for the FYI. Is this 6 only or occurs in 8 too? |
I believe .NET 9 too, as I have reported above that I could repro it in 9. |
@mangod9 I believe it is 6+. I have reproduced it with 9 main. |
Triage: This is problem in HttpListener, not in ClientWebSockets. It is rarely used, first report in years, moving to Future. |
Description
The same server build works differently on Windows and Linux, on Windows maximum memory usage is ~300 MB, then after GC calls it's drop to 180-250 average. On Linux after 5 minutes RAM usage is ~1.3 GB (and grows)
Reproduction Steps
usings:
Server:
Client(s):
Expected behavior
Same memory usage as on Windows

Actual behavior
Memory leak?

Regression?
Same on .NET 8 ( Microsoft.NETCore.App 8.0.4 )
Known Workarounds
No response
Configuration
Windows:
Windows 10 x64, Version 10.0.19045 Build 19045, 22H2
Microsoft.NETCore.App 6.0.27
Linux:
Debian 11 (5.10.0-28-amd64 Debian 5.10.209-2 (2024-01-31) x86_64 GNU/Linux)
Microsoft.NETCore.App 6.0.27
Other information
No response
The text was updated successfully, but these errors were encountered: