Skip to content

Memory Leak on NpgsqlDataSource with password provider #3646

Closed

Description

@NikitaCh

I've originally opened this issue in npsql repo, but after further research I think the issue is in this package, particularly in the fact that connectionString is being used as a key for cache of DataSource

The issue:

There appears to be a memory leak when using EF + NpgsqlDataSource with password provider.

Versions:
Npgsql.EntityFrameworkCore.PostgreSQL - 9.0.4
Npgsql - 9.0.3

Image Image

Reproducible example attached - MemoryLeak.zip (includes simple docker-compose with postgres for comfort)

Relevant code breakdown:

AppDbContext.cs

internal class AppDbContext : DbContext
{
    public DbSet<SomeModel> SomeModels { get; set; }

    public DbSet<SomeOtherModel> SomeOtherModels { get; set; }

    public AppDbContext(DbContextOptions<AppDbContext> options) : base(options)
    {
    }

    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        modelBuilder.Entity<SomeModel>(b =>
        {
            b.HasKey(e => e.Id);
            b.OwnsOne(e => e.JsonModel).ToJson();
        });

        modelBuilder.Entity<SomeOtherModel>(b =>
        {
            b.HasKey(e => e.Id);
        });

        base.OnModelCreating(modelBuilder);
    }

    protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
    {
        optionsBuilder.UseNpgsql(@"User ID=postgres;Server=localhost;Port=5433;Database=postgres_db;Pooling=true;", sqlOptions =>
        {
            sqlOptions.EnableRetryOnFailure(maxRetryCount: 3);
            sqlOptions.ConfigureDataSource(dsBuilder =>
            {
                dsBuilder.ConnectionStringBuilder.Host = "localhost";
                dsBuilder.Name = "postgres_db";
                dsBuilder.UsePasswordProvider(_ => Guid.NewGuid().ToString(), (_, _) => ValueTask.FromResult(Guid.NewGuid().ToString()));
            });
        });
        optionsBuilder.UseSnakeCaseNamingConvention();
        base.OnConfiguring(optionsBuilder);
    }
}

Program.cs

using MemoryLeak;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;

var builder = Host.CreateApplicationBuilder(args);
builder.Services.AddDbContext<AppDbContext>();

var app = builder.Build();

for(var i = 0; i < 1_000_000; ++i)
{
    using var scope = app.Services.CreateScope();
    var dbContext = scope.ServiceProvider.GetRequiredService<AppDbContext>();
    await dbContext.Database.CanConnectAsync();

    if (i % 100 == 0)
    {
        GC.Collect();
        GC.WaitForPendingFinalizers();
        GC.Collect();
        Console.WriteLine($"Iteration: {i}");
        await Task.Delay(3000);
    }
}

From what I can understand, it seems like there is some cache based on ConnectionString, giving the fact that password provider changes the connection string (in this example all the time) - the cache grows indefinitely.

As soon as you change the password provider to return some constant string - everything goes to normal
(For example - dsBuilder.UsePasswordProvider(_ => "constant", (_, _) => ValueTask.FromResult("string"));) :
Image

This is a critical issue, because it is a requirement for a lot of applications to use for example IAM Authorization to connect to RDS - hence use of RDSAuthTokenGenerator for password generation. The application memory grows "indefinitely" in this scenario. The rate of growth seems to be dependent on model size.

Thanks for the help.

Activity

  1. self-assigned this
    on Oct 28, 2025
  2. added theissue type on Oct 28, 2025
  3. added this to the milestone on Oct 28, 2025
  4. added a commit that references this issue on Oct 30, 2025
    f3f3145
  5. added 2 commits that reference this issue on Oct 30, 2025
    3bb3109
    36d5508
  6. roji commented on Oct 30, 2025

    @roji
    Member

    @NikitaCh I pushed a fix for this - thanks again for reporting.

    Can you please give the preview package 9.0.5-ci.20251030T090032 a try, from the stable feed? This should already contain the fix and would help us verify that my fix is correct. If it works, you can safely use this package until 9.0.5 gets released.

  7. NikitaCh commented on Oct 30, 2025

    @NikitaCh
    Author

    @roji, for me to understand, does your fix rely on previous password being rejected by database? Just based on your code changes that is my understanding

  8. roji commented on Oct 30, 2025

    @roji
    Member

    No, it shouldn't. It simply uses the same connection string and pool as what's used for normal EF operations (before this fix it was created a new one).

  9. NikitaCh commented on Oct 30, 2025

    @NikitaCh
    Author

    @roji thank you! I can confirm that I cannot reproduce it with the same code I used to be able to.

    I will play around a bit more though and let you know if I find some other case where this is reproducible, just to make sure 😄 The reason for this is because I definitely knew and confirmed with above code that my services have memory leak because of DbContext health check. But there are other services suffering from similar in intensity memory leak, yet there are no health checks there... Yet I see that your fix specifically targets that (If I understand it correctly), so probably that will lead me to another round of investigation on my side 😕

    I have one minor comment though specifically about the fix, the fact that now you are executing SELECT 1 gives an info log, might lead to some confusions/questions coming your way, so might be good to document/note it somewhere, or better yet move to debug?

    info: Microsoft.EntityFrameworkCore.Database.Command[20101]
          Executed DbCommand (2ms) [Parameters=[], CommandType='Text', CommandTimeout='30']
          SELECT 1
    
    
  10. NikitaCh commented on Oct 30, 2025

    @NikitaCh
    Author

    @roji that didn't take long, but I assume then it is my mis-usage, since I have your attention, would be a miss not to use it 😄

    Do I understand correctly, that even if I register DbContext as transient I still have to use scope from DI? Can I have a brief explanation or link where this is described?

    So consider below code

    using MemoryLeak;
    using Microsoft.EntityFrameworkCore;
    using Microsoft.Extensions.DependencyInjection;
    using Microsoft.Extensions.Hosting;
    using System.Diagnostics;
    
    var builder = Host.CreateApplicationBuilder(args);
    builder.Services.AddDbContext<AppDbContext>(ServiceLifetime.Transient, ServiceLifetime.Singleton);
    
    var app = builder.Build();
    
    Console.WriteLine($"ProcessId: {Process.GetCurrentProcess().Id}");
    
    
    for (var i = 0; i < 1_000_000; ++i)
    {
        //using var scope = app.Services.CreateScope();
        //using var dbContext = scope.ServiceProvider.GetRequiredService<AppDbContext>();
        using var dbContext = app.Services.GetRequiredService<AppDbContext>();
    
        await dbContext.Database.CreateExecutionStrategy().ExecuteAsync(async () =>
        {
            return await dbContext.Database.ExecuteSqlRawAsync("SELECT {0}", 1);
        });
    
        if (i % 100 == 0)
        {
            GC.Collect();
            GC.WaitForPendingFinalizers();
            GC.Collect();
            Console.WriteLine($"Iteration: {i}");
            await Task.Delay(3000);
        }
    }
    

    This has a memory leak.

    Uncommenting

        using var scope = app.Services.CreateScope();
        using var dbContext = scope.ServiceProvider.GetRequiredService<AppDbContext>();
    

    And commenting using var dbContext = app.Services.GetRequiredService<AppDbContext>();

    Does not have a memory leak.

  11. roji commented on Oct 30, 2025

    @roji
    Member

    the fact that now you are executing SELECT 1 gives an info log

    That's a good point. The SQL Server provider works this way, and a fix would need to happen on the EF side rather than here. Let's see if people complain about this to the point where that's needed.

    So consider below code

    This is an issue in your code - you should never resolve services like that directly on the root container without a scope. When using DI, the container is responsible for disposing all the instances it hands out (which also means you should not be disposing the DbContext as you're currently doing with using). Because of this, the container must track disposable instances that it hands out, and because you've registered the DbContext as transient, a different instance must be handed out each time. This causes a growing list of DbContexts in your root container above which will only ever be releasable once you dispose the root container.

    Read this page to better understand all this.

  12. roji commented on Oct 30, 2025

    @roji
    Member

    Otherwise thanks for confirming - happy to hear the problem is resolved. And let me know if you run into any similar issues.

  13. added a commit that references this issue on Oct 30, 2025
    3e175da
  14. modified the milestones: , 10.0.4 on Nov 20, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions