Skip to content

Is AddDbContextPool supposed to release contexts from the pool? #35855

Open

Description

@Steve887

Question

I have a large context model, so I'm experimenting with context pooling.

My current setup is:

service.AddDbContextPool<AppDbContext>((provider, optionsBuilder) =>
{
    AppDbContextOptionsFactory.Get(optionsBuilder);
});

When I run my app, everything seems to work ok, except the active_dbcontexts seems to indicate the pooled dbcontexts are created but never released. Is this the expected behaviour for this?

Image

From debugging with EFCore symbols, it appears that AddDbContextPool uses a ScopedDbContextFactory that does not allow the lease to be released because IsStandalone is set to false.

EF Core version

9.0.0

Database provider

SqlServer

Target framework

.Net 9

Operating system

Windows 10

IDE

vs 2022

Activity

  1. roji commented on Mar 27, 2025

    @roji
    Member

    @Steve887 the point of context pooling is to keep unused contexts in the pool, so they can be reused later. There's no mechanism for pruning unused contexts after a while, so if you e.g. have a load spike which allocates 100 contexts, these will afterwards sit idle in the pool without getting removed. This is intentional - unlike e.g. physical database connections, which can consume considerable resources on the databases side and therefore need to be pruned, contexts just consume a bit of memory - nothing more - and keeping them around has very little impact.

    Having said that, IIRC the intention behind the active_dbcontexts telemetry was to track non-idle instances, i.e, how many are at this moment in-use, out of the pool; you're saying that this isn't the behavior you're seeing?

  2. Steve887 commented on Mar 27, 2025

    @Steve887
    Author

    @roji that makes sense from a design viewpoint, I suppose trading a small amount of memory for better performance makes sense.

    I was expecting the metric to go down once the request was finished, however. If I run a test with 10 simultaneous requests, it goes up to 10 and stays there forever. They are reused, so running the request again doesn't increase it further. I just wanted to make sure that's expected or if there's an issue with my setup.

  3. roji commented on Mar 28, 2025

    @roji
    Member

    @cincuranet assigning to you as you worked in the metrics area - I think active_dbcontext is indeed supposed to represent active contexts in use by the program, and should not take into account idle contexts stored in the pool. Let's discuss if anything's unclear.

  4. added this to the 11.0.0 milestone on Aug 26, 2025
  5. modified the milestones: 11.0.0, Backlog on May 20, 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

    Type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions