Repository navigation
Conversation
|
Thanks for your PR, @SENESO. Someone from the team will get assigned to your PR shortly and we'll get it reviewed. |
|
@dotnet-policy-service agree |
|
Any update on this? 👀 |
|
Hi @MohamedAliTera, thanks for checking in. The fix is complete and all CI checks are green — it's currently waiting on a maintainer review. Hopefully it gets picked up soon. |
|
Looks like this PR hasn't been active for some time and the codebase could have been changed in the meantime. |
|
This still reproduces on .NET 11.0.0 ( // repro.csproj: <Project Sdk="Microsoft.NET.Sdk.Web">, net11.0, ImplicitUsings enabled
using System.Runtime.CompilerServices;
foreach (var useRateLimiter in new[] { false, true })
{
var refs = new List<WeakReference>();
for (var i = 0; i < 20; i++)
{
refs.Add(await StartAndDisposeAsync(useRateLimiter));
}
for (var i = 0; i < 3; i++)
{
GC.Collect();
GC.WaitForPendingFinalizers();
}
Console.WriteLine($"UseRateLimiter={useRateLimiter}: {refs.Count(r => r.IsAlive)}/20 disposed apps still alive");
}
[MethodImpl(MethodImplOptions.NoInlining)]
static async Task<WeakReference> StartAndDisposeAsync(bool useRateLimiter)
{
var builder = WebApplication.CreateBuilder();
builder.WebHost.UseUrls("http://127.0.0.1:0");
builder.Services.AddRateLimiter(_ => { });
var app = builder.Build();
app.UseRouting();
if (useRateLimiter)
{
app.UseRateLimiter();
}
app.MapGet("/", () => "ok");
await app.StartAsync();
var weak = new WeakReference(app.Services);
await app.StopAsync();
await app.DisposeAsync();
return weak;
}Output on macOS arm64, .NET 11.0.0: For scale: in an endpoint test suite that starts a fresh in-memory host per test (~1,000 hosts), every host stayed reachable and the managed heap grew steadily to ~3.7 GB over the run. Bisecting the pipeline pointed at |
|
Thanks for the independent repro — especially the standalone .NET 11 case without a test host, and the 1,000-host heap measurement. That matches what I found: with @BrennanConroy — ready for review whenever you have a moment. Per the policy note above, CI validation needs a rerun (I do not have commit rights for |
|
@BrennanConroy it's a very big issue for us, can you please push it forward? We're hitting this too, at scale, on ASP.NET Core 10. Adding numbers in case they help get #69914 reviewed. Our test projects start a test host per test class, and 22 of our services call We confirmed the chain in a dump of one of them, which has one live endpoint-limiter timer for each live host. That project holds 4.5 GB after a full GC at the end of its CI run; most of our other test projects end at about 80 MB.
|
|
A workaround until this (or a different) PR land: |
Fixes #66434 (also closes #69061 as a duplicate).
RateLimitingMiddlewarecreates its ownPartitionedRateLimiter<HttpContext>(_endpointLimiter) inCreateEndpointLimiter()but never disposes it. The limiter's internal timer infrastructure keeps the host rooted, so everyWebApplicationFactory-based test class leaks roughly one host (~100MB). Verified with aWeakReferencerepro: the host is not collectable withUseRateLimiterbefore this change, and collectable after.The fix registers the middleware-owned limiter for disposal on
IHostApplicationLifetime.ApplicationStopped. Design notes:_endpointLimiteris disposed — it is created inside the middleware, so the middleware is its sole owner._globalLimitercomes fromRateLimiterOptions.GlobalLimiter(user-provided) and is deliberately left alone: the user may share it across hosts or dispose it themselves.GetService(notGetRequiredService) keeps this null-safe for non-host scenarios; the callback is astaticlambda so the registration can't root the middleware instance.Tests:
EndpointLimiterIsDisposedWhenApplicationStops— fails without the fix, passes with it.GlobalLimiterIsNotDisposedWhenApplicationStops— documents the ownership decision.Note: I couldn't execute the repo's own test suite in my environment (it needs the source-built targeting pack from a near-full repo build), so CI is the real gate here. The change is minimal, additive, and null-safe.