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
Is AddDbContextPool supposed to release contexts from the pool? #35855
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?
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.
@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?
@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.
@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.
Activity
roji commentedon Mar 27, 2025
@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_dbcontextstelemetry 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?Steve887 commentedon Mar 27, 2025
@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.
roji commentedon Mar 28, 2025
@cincuranet assigning to you as you worked in the metrics area - I think
active_dbcontextis 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.