Repository navigation
Description
CanConnectasync is called from health checks regularly inside container in a kubernetes cluster, but its use can reach max connections for client.
The problem is reproducible in localhost and in my staging environment.
I've created a test console app (.net 5.0) to reproduce the issue:
using System;
using System.Threading.Tasks;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;
namespace NpgsqlTests
{
static class Program
{
static async Task Main(string[] args) {
var pooling = true;
var services = new ServiceCollection();
services.AddDbContext<TestDbContext>(o => {
o.UseNpgsql($"Host=localhost;Database=npgsqltests;Username=postgres;Password=postgres;Pooling={pooling}");
});
using var provider = services.BuildServiceProvider();
await CreateDatabase(provider);
await TestQueries(provider);
await TestCanConnect(provider);
}
private static async Task TestCanConnect(ServiceProvider provider) {
Console.WriteLine("Start connections.");
for (int i = 0; i < 200; i++) {
await Task.Delay(100);
Console.Write($"Connection {i}...");
var scope = provider.CreateScope();
var dbContext = scope.ServiceProvider.GetRequiredService<TestDbContext>();
await dbContext.Database.CanConnectAsync();
Console.WriteLine(" Done.");
}
}
private static async Task TestQueries(ServiceProvider provider) {
Console.WriteLine("Start queries.");
for (int i = 0; i < 200; i++) {
await Task.Delay(100);
Console.Write($"Query {i}...");
var scope = provider.CreateScope();
var dbContext = scope.ServiceProvider.GetRequiredService<TestDbContext>();
dbContext.Entities.Add(new TestEntity());
await dbContext.SaveChangesAsync();
var c = await dbContext.Entities.CountAsync();
Console.WriteLine(" Done.");
}
}
private static async Task CreateDatabase(ServiceProvider provider) {
using (var scope = provider.CreateScope()) {
var dbContext = scope.ServiceProvider.GetRequiredService<TestDbContext>();
await dbContext.Database.EnsureCreatedAsync();
}
}
}
public class TestDbContext : DbContext
{
public DbSet<TestEntity> Entities { get; set; }
public TestDbContext(DbContextOptions<TestDbContext> options) : base(options) {
}
}
public class TestEntity
{
public int Id { get; set; }
public string Text { get; set; }
}
}
The project has two dependencies:
<ItemGroup>
<PackageReference Include="Npgsql" Version="5.0.4" />
<PackageReference Include="Npgsql.EntityFrameworkCore.PostgreSQL" Version="5.0.5" />
</ItemGroup>
When var pooling = true, the method TestCanConnect stucks after 100 calls of CanConnectAsync.
I see all the connection in idle state when querying pg_stat_activity.
When var pooling = false, everything runs without problems, and pg_stat_activity is almost empty.
The method TestQueries is used to ensure that usual queries are executed without problems; in fact, it runs successfully regardless of the value of pooling.
I think the problem is in disposal of the connection when connection pooling is temporarily disabled inside CanConnectAsync.
Activity
roji commentedon Apr 17, 2021
Thanks for the repro, I can indeed see an issue - will investigate.
Fix connection leak in NpgsqlDatabaseCreator.Exists
roji commentedon Apr 17, 2021
@frabe1579 I've submitted #1813 to fix this - there's indeed a connection leak in NpgsqlDatabaseCreator.Exists - thanks! For reference, a more complete fix for the source issue is tracked by #1814.
Fix connection leak in NpgsqlDatabaseCreator.Exists (#1813)
Fix connection leak in NpgsqlDatabaseCreator.Exists (#1813)
taspeotis commentedon Apr 19, 2021
We're seeing this too. Am I right in that it was introduced here and it's as simple as downgrading to 5.0.4 to avoid it until 5.0.6 comes out?
Thanks in advance.
roji commentedon Apr 19, 2021
@taspeotis yes, that's correct.
prcdpr commentedon Apr 20, 2021
Hey, wouldn't it make sense to unlist 5.0.5 from NuGet until it's fixed? It broke my app due to connection pool exhaustion and that version is not usable at least in my case.
frabe1579 commentedon Apr 21, 2021
Currently I use 5.0.5 with a custom health check, and the problem disappeared. So i think it's not necessary to unlist.
roji commentedon Apr 21, 2021
FYI I've just released 5.0.5.1 which contains the fix - can you please check and confirm?
prcdpr commentedon Apr 22, 2021
In my case 5.0.5.1 works correctly
frabe1579 commentedon Apr 22, 2021
@roji I've tested 5.0.5.1 in my staging environment, using original health check for ef core, and everything goes well, no more connection exhaustion.
Thank you very much!
roji commentedon Apr 22, 2021
Thanks for your feedback, and sorry this bug occurred in the first place...