Skip to content

RateLimitingMiddleware never disposes PartitionedRateLimiter causing memory leak #66434

Description

@kvantetore

Is there an existing issue for this?

  • I have searched the existing issues

Describe the bug

RateLimitingMiddleware creates a PartitionedRateLimiter in the constructor. The DefaultPartitionedRateLimiter implementation uses a Timer to implement a Heartbeat for managing rate limiting. RateLimitingMiddleware does not implement IDisposable, and currently never disposes the created PartitionedRateLimiter. This cases a memory leak when middleware pipelines are expected to be temporary.

My use case for this is a multitenant setup, where a ServiceProvider and a middleware pipeline is set up per tenant, and I want to be able to "recycle" tenants without restarting the entire app.

Expected Behavior

When RateLimitingMiddleware is no longer needed, it should remove all calbacks to itself to allow it to be garbage collected.

I believe a realtively easy fix is to migrate RateLimitingMiddleware to an IMiddleware-type middleware, implement IDisposable, and dispose the constructed PartitionedRateLimiter owned by the middleware. Callers using RateLimiterOptions.GlobalLimiter will have to manage the lifecycle of the supplied PartitionedRateLimiter manually.

Steps To Reproduce

No response

Exceptions (if any)

No response

.NET Version

11.0.100-preview.4.26210.111

Anything else?

No response

Activity

  1. added
    area-middleware
    bug
    on Apr 23, 2026
  2. github-actions commented on Apr 23, 2026

    @github-actions
    Contributor

    Triage Summary

    Area: area-middleware — RateLimitingMiddleware lives in src/Middleware/RateLimiting/ and the issue is directly about rate limiting middleware lifecycle management.

    Type: bug — RateLimitingMiddleware constructs a PartitionedRateLimiter (which holds a Timer/heartbeat internally) but never disposes it, causing a real resource/memory leak in scenarios where middleware pipelines are rebuilt (e.g., per-tenant multi-tenancy setups).

    Potential Duplicates

    • None found — searches for related dispose/memory-leak issues on RateLimitingMiddleware returned no other matches.

    Notes

    • The reporter suggests implementing IDisposable on the middleware (or migrating to IMiddleware) and disposing the internally-created PartitionedRateLimiter, while leaving user-supplied GlobalLimiter instances for the caller to manage.
    • No regression — this appears to be a pre-existing design gap rather than a behavior change across versions.

    Generated by Issue Triage Agent for dotnet/aspnetcore for issue #66434 · ● 167.7K · ◷

  3. added
    feature-rate-limit
    on Apr 24, 2026
  4. added theissue type on May 1, 2026
  5. chris-fixportal commented on Sep 27, 2026

    @chris-fixportal

    Adding a repro and retention chain from #69554 (closed as a duplicate of this). This is the same bug, seen from the test-host side on 10.0.12.

    Impact with WebApplicationFactory<T>. Every host whose pipeline ever included UseRateLimiter() stays reachable after StopAsync() + DisposeAsync(), so you leak one host per test class. In our suite that's ~400 hosts at ~100 MB each. The test process reached 22 GB locally and exhausted a 16 GB CI runner. It surfaced as SQL Server error 596 in a sidecar container, which pointed nowhere near the limiter.

    Repro (net10.0, Microsoft.AspNetCore.TestHost only): https://gist.github.com/chris-fixportal/a36174da57a31565c647879f74f23d5d

    without UseRateLimiter: host collectable: True
    with UseRateLimiter:    host collectable: False
    

    gcroot of the disposed Host (all roots identical):

    System.Threading.TimerQueue (static)
    -> TimerQueueTimer
    -> System.Threading.RateLimiting.TimerAwaitable
    -> AsyncStateMachineBox<DefaultPartitionedRateLimiter<HttpContext,DefaultKeyType>.RunTimer>
    -> DefaultPartitionedRateLimiter<HttpContext,DefaultKeyType>
    -> Func<HttpContext,RateLimitPartition<DefaultKeyType>>
    -> RateLimitingMiddleware
    -> RequestDelegate
    -> EndpointMiddleware
    -> RouteOptions
    -> ObservableCollection<EndpointDataSource>
    -> RouteGroupBuilder+GroupEndpointDataSource
    -> RouteGroupBuilder
    -> WebApplication
    -> Host
    

    If we dispose _endpointLimiter (via reflection, on ApplicationStopped), the same host becomes collectable within a second. So the limiter is the only thing holding it.

    Why tests can't just skip it: omitting UseRateLimiter() makes EndpointMiddleware throw for any endpoint carrying RequireRateLimiting metadata.

    Current workaround: an IStartupFilter that captures the built pipeline, walks it by reflection to RateLimitingMiddleware, and disposes _endpointLimiter/_globalLimiter on ApplicationStopped. It works, but it depends on private field names, so a framework fix (dispose on ApplicationStopped, or the IDisposable route suggested above) would be very welcome.

    Related reports that look like the same root cause: #69061, #69259, #69509, #69532.

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-middlewarebugfeature-rate-limit

    Type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions