Skip to content

RateLimitingMiddleware keeps a disposed app alive #69509

Open

Description

@yasmoradi

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

  1. added
    area-middleware
    on Sep 25, 2026
  2. github-actions commented on Sep 25, 2026

    @github-actions
    Contributor

    Triage Summary

    Area: area-middleware (matches src/Middleware/RateLimiting/, RateLimitingMiddleware)
    Type: Bug (reproducible: RateLimitingMiddleware creates a PartitionedRateLimiter that is never disposed, keeping the middleware and app alive after disposal)

    Potential Duplicates

    Generated by Issue Triage Agent for dotnet/aspnetcore for #69509 · copilot · auto · 61.2 AIC · ⌖ 3.39 AIC · ⊞ 27.6K · ◷

  3. added
    bug
    on Oct 23, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    area-middlewarebug

    Type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions