Repository navigation
Labels
area-middlewareIncludes: URL rewrite, redirect, response cache/compression, session, and other general middlewaresbugThis issue describes a behavior which is not expected - a bug.
Description
Is there an existing issue for this?
- I have searched the existing issues
Describe the bug
RateLimitingMiddleware creates a PartitionedRateLimiter in its constructor and implements no disposal interface, so nothing ever disposes it.
The chain, on v10.0.10:
- The middleware builds a limiter unconditionally in its constructor (
RateLimitingMiddleware.cs#L56):_endpointLimiter = CreateEndpointLimiter(); - That limiter starts a timer in its own constructor (
DefaultPartitionedRateLimiter.cs#L48-L49). The only thing that stops it is_timer.Stop()inCommonDispose(#L194), reachable only fromDispose/DisposeAsync.TimerAwaitablewraps a realSystem.Threading.Timer(TimerAwaitable.cs#L55), so while it runs it is a GC root. - The partitioner passed to
PartitionedRateLimiter.Createreads_policyMapand_defaultPolicyKey(#L252), so it captures the middleware instance, and the middleware holds_next, which is the rest of the pipeline. In an app with routing and endpoints, endpoint execution captures the service provider, so the chain reaches the whole DI container and a disposedIHoststays fully reachable. - Nothing can break that chain.
internal sealed partial class RateLimitingMiddlewareimplements neitherIDisposablenorIAsyncDisposable, and the limiter it creates is registered nowhere.
Expected Behavior
Disposing the host should release the host, its pipeline and its service provider. RateLimitingMiddleware should implement IAsyncDisposable and dispose the endpoint limiter it created, so the host disposes it along with the rest of the pipeline.
Steps To Reproduce
Minimal repro: https://github.com/themidnightgospel/RateLimiterHostLeak
dotnet test RateLimiterHostLeak.slnx -c Release
Three tests, all passing on 10.0.10:
DisposedHost_WithRateLimiter_IsStillReachable— aWeakReferenceto the host survives a blocking gen-2 collection afterStopAsync()andDispose().DisposedHost_WithoutRateLimiter_IsCollected— the identical host withoutUseRateLimiter()is collected, so the retention is the middleware.DisposedHost_WithRateLimitersDisposed_IsCollected— disposing_globalLimiter/_endpointLimiterby reflection releases the host, confirming the limiter is the root.
The repro configures no policies at all (services.AddRateLimiter(_ => { })) and still leaks.
Exceptions (if any)
No response
.NET Version
10.0.300
Anything else?
- ASP.NET Core version: Microsoft.AspNetCore.App 10.0.10
- OS: Windows 11
Metadata
Metadata
Assignees
Labels
area-middlewareIncludes: URL rewrite, redirect, response cache/compression, session, and other general middlewaresbugThis issue describes a behavior which is not expected - a bug.
Activity
MihuBot commentedon Sep 4, 2026
I'm a bot. Here is a possible related and/or duplicate issue (I may be wrong):
github-actions commentedon Sep 4, 2026
Triage Summary
Area:
area-middleware(RateLimitingMiddleware lives in src/Middleware/RateLimiting)Type:
Bug(preserved)Potential Duplicates
IDisposableonRateLimitingMiddleware.