You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
RateLimitingMiddleware never disposes PartitionedRateLimiter causing memory leak #66434
area-middlewareIncludes: URL rewrite, redirect, response cache/compression, session, and other general middlewaresbugThis issue describes a behavior which is not expected - a bug.feature-rate-limitWork related to use of rate limit primitives
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
Reacted by Marco Minerva, Bitchiko Tchelidze, Jesus Flores, Alexandru Dan Maftei, Mohamed Ali, Buro CITE and Doron Grinzaig
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.
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.
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.
area-middlewareIncludes: URL rewrite, redirect, response cache/compression, session, and other general middlewaresbugThis issue describes a behavior which is not expected - a bug.feature-rate-limitWork related to use of rate limit primitives
Activity
github-actions commentedon Apr 23, 2026
Triage Summary
Area:
area-middleware—RateLimitingMiddlewarelives insrc/Middleware/RateLimiting/and the issue is directly about rate limiting middleware lifecycle management.Type:
bug—RateLimitingMiddlewareconstructs aPartitionedRateLimiter(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
RateLimitingMiddlewarereturned no other matches.Notes
IDisposableon the middleware (or migrating toIMiddleware) and disposing the internally-createdPartitionedRateLimiter, while leaving user-suppliedGlobalLimiterinstances for the caller to manage.chris-fixportal commentedon Sep 27, 2026
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 includedUseRateLimiter()stays reachable afterStopAsync()+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.TestHostonly): https://gist.github.com/chris-fixportal/a36174da57a31565c647879f74f23d5dgcrootof the disposedHost(all roots identical):If we dispose
_endpointLimiter(via reflection, onApplicationStopped), 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()makesEndpointMiddlewarethrow for any endpoint carryingRequireRateLimitingmetadata.Current workaround: an
IStartupFilterthat captures the built pipeline, walks it by reflection toRateLimitingMiddleware, and disposes_endpointLimiter/_globalLimiteronApplicationStopped. It works, but it depends on private field names, so a framework fix (dispose onApplicationStopped, or theIDisposableroute suggested above) would be very welcome.Related reports that look like the same root cause: #69061, #69259, #69509, #69532.