Repository navigation
Description
Is there an existing issue for this?
- I have searched the existing issues
Describe the bug
RateLimitingMiddleware creates its endpoint limiter with PartitionedRateLimiter.Create, and nothing ever disposes it. DefaultPartitionedRateLimiter starts a heartbeat timer in its constructor, so the timer keeps the limiter alive, the limiter's partitioner keeps the middleware alive, and the middleware's next keeps the rest of the pipeline and the app's services alive. An app that called UseRateLimiter is never collected after it's stopped and disposed.
With one app per process this doesn't matter. In a test project that starts an app per test (our own test server, same with WebApplicationFactory) every one of them stays in memory until the process exits. We found it in a dump of our test process, where each finished test server was still reachable through TimerQueue > TimerAwaitable > DefaultPartitionedRateLimiter > RateLimitingMiddleware > RequestDelegate.
Expected Behavior
Stopping or disposing the app disposes the limiters the middleware created, so the app can be collected.
Steps To Reproduce
using System.Runtime.CompilerServices;
using Microsoft.AspNetCore.RateLimiting;
var useRateLimiter = args.Contains("--rate-limiter");
for (var i = 0; i < 20; i++)
await StartAndDispose(useRateLimiter);
for (var i = 0; i < 3; i++)
{
GC.Collect();
GC.WaitForPendingFinalizers();
await Task.Delay(500);
}
Console.WriteLine($"UseRateLimiter={useRateLimiter}: {Marker.Instances.Count(i => i.IsAlive)} of {Marker.Instances.Count} disposed apps are still alive");
[MethodImpl(MethodImplOptions.NoInlining)]
static async Task StartAndDispose(bool useRateLimiter)
{
var builder = WebApplication.CreateSlimBuilder();
builder.WebHost.UseUrls("http://127.0.0.1:0");
builder.Services.AddRateLimiter(o => o.AddFixedWindowLimiter("fixed", l => { l.PermitLimit = 10; l.Window = TimeSpan.FromSeconds(1); }));
await using var app = builder.Build();
if (useRateLimiter)
app.UseRateLimiter();
app.UseMiddleware<Marker>();
app.MapGet("/", () => "hello");
await app.StartAsync();
await app.StopAsync();
}
sealed class Marker
{
public static readonly List<WeakReference> Instances = [];
private readonly RequestDelegate next;
public Marker(RequestDelegate next)
{
this.next = next;
Instances.Add(new WeakReference(this));
}
public Task Invoke(HttpContext context) => next(context);
}Output:
UseRateLimiter=False: 0 of 20 disposed apps are still alive
UseRateLimiter=True: 20 of 20 disposed apps are still alive
Exceptions (if any)
No response
.NET Version
10.0.401 (ASP.NET Core 10.0.12). Also seen on 11.0.0-rc.1.
Anything else?
No response
Activity
MihuBot commentedon Sep 25, 2026
I'm a bot. Here are possible related and/or duplicate issues (I may be wrong):
github-actions commentedon Sep 25, 2026
Triage Summary
Area:
area-middleware(matchessrc/Middleware/RateLimiting/,RateLimitingMiddleware)Type:
Bug(reproducible:RateLimitingMiddlewarecreates aPartitionedRateLimiterthat is never disposed, keeping the middleware and app alive after disposal)Potential Duplicates
RateLimitingMiddleware never disposes its PartitionedRateLimiter; the limiter's timer loop keeps the whole host reachable after shutdown#69554 as a duplicate of this issue