Skip to content

DbContextPool not decrementing active_dbcontexts metrics when overloaded #38538

Closed

Description

@gijs-hendrikx

Bug description

I want to use the DbContext pool. Because I have a few places where it's up to the developer to properly dispose the DbContext I want to monitor pool usage and context disposal.

There are no metrics for the pool (e.g. rented out db contexts), so I tried monitoring the 'microsoft.entityframeworkcore.active_dbcontexts' metric. However I notice that this metric is not correct when used in combination with a pool.

I know that the metric includes the idle contexts in the pool as discussed in #35855 , but whenever the pool is exhausted and falls back to creating new contexts, those contexts are also not decremented from the metric after disposal, so you get an ever-incrementing metric.

A project to reproduce and show this error can be found here.

The project demonstrates the following:

  • A pool of 20 db contexts
  • Every 30 seconds: rent out 25 db contexts at once and dispose them all afterwards
  • The active_dbcontexts metric keeps growing on each iteration (25, 30, 35, etc.). Even though my code disposes ALL db contexts and thus the active_dbcontexts metric should be equal to the pool size of 20.

This is a duplicate of #38306 but that issue was misunderstood and I can't re-open it or get in touch.

Your code

https://github.com/gijs-hendrikx/DbContextPool_Debugger

Stack traces


Verbose output


EF Core version

9.0.5

Database provider

No response

Target framework

.NET 10.0

Operating system

Windows 11

IDE

No response

Activity

  1. self-assigned this
    on Jul 2, 2026
  2. added theissue type on Jul 2, 2026
  3. added this to the 11.0.0 milestone on Jul 2, 2026
  4. added 2 commits that reference this issue on Jul 3, 2026
    e718d90
    efb5e76
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions