Skip to content

RateLimitingMiddleware never disposes the limiter it creates, keeping disposed hosts alive #69061

Description

@themidnightgospel

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:

  1. The middleware builds a limiter unconditionally in its constructor (RateLimitingMiddleware.cs#L56): _endpointLimiter = CreateEndpointLimiter();
  2. That limiter starts a timer in its own constructor (DefaultPartitionedRateLimiter.cs#L48-L49). The only thing that stops it is _timer.Stop() in CommonDispose (#L194), reachable only from Dispose/DisposeAsync. TimerAwaitable wraps a real System.Threading.Timer (TimerAwaitable.cs#L55), so while it runs it is a GC root.
  3. The partitioner passed to PartitionedRateLimiter.Create reads _policyMap and _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 disposed IHost stays fully reachable.
  4. Nothing can break that chain. internal sealed partial class RateLimitingMiddleware implements neither IDisposable nor IAsyncDisposable, 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 — a WeakReference to the host survives a blocking gen-2 collection after StopAsync() and Dispose().
  • DisposedHost_WithoutRateLimiter_IsCollected — the identical host without UseRateLimiter() is collected, so the retention is the middleware.
  • DisposedHost_WithRateLimitersDisposed_IsCollected — disposing _globalLimiter / _endpointLimiter by 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

Activity

  1. MihuBot commented on Sep 4, 2026

    @MihuBot

    I'm a bot. Here is a possible related and/or duplicate issue (I may be wrong):

  2. added
    area-middleware
    on Sep 4, 2026
  3. github-actions commented on Sep 4, 2026

    @github-actions
    Contributor

    Triage Summary

    Area: area-middleware (RateLimitingMiddleware lives in src/Middleware/RateLimiting)
    Type: Bug (preserved)

    Potential Duplicates

    Generated by Issue Triage Agent for dotnet/aspnetcore for #69061 · copilot · auto · 41.4 AIC · ⌖ 14.7 AIC · ⊞ 29.6K · ◷

  4. 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