We’re seeing this exception with an application running on a test environment. The issue doesn’t happen locally with a dockerised Postgres instance.
Npgsql.NpgsqlException (0x80004005): The connection pool has been exhausted, either raise ‘Max Pool Size’ (currently 100) or ‘Timeout’ (currently 50 seconds) in your connection string. —> System.TimeoutException: The operation has timed out. at Npgsql.PoolingDataSource.g__RentAsync|34_0(NpgsqlConnection conn, NpgsqlTimeout timeout, Boolean async, CancellationToken cancellationToken) at Npgsql.NpgsqlConnection.g__OpenAsync|42_0(Boolean async, CancellationToken cancellationToken) at Npgsql.NpgsqlDataSourceCommand.ExecuteReader(Boolean async, CommandBehavior behavior, CancellationToken cancellationToken)
- Both environments use the same connection string values for pool and timeout.
- The application uses the ‘newer’ style of registering a NpgsqlDataSource with
builder.Services.AddNpgsqlDataSource
- Statements are only executed with
await using NpgsqlCommand command = this.dataSource.CreateCommand(..)
, connections aren’t manually opened. - The datasource is injected into the constructor of a repository class which is injected into the constructor of a class that inherits from
BackgroundService
I’m uncertain from the error if this is an infrastructure issues and the driver cannot open a connection within the timeout setting (seems unlikely given the error isn’t constant and increasing the timeout doesn’t achieve anything) OR the connection pool is being exhausted due to connections not being disposed of correctly. This also seems unlikely given how we are not dealing with connections directly and injecting the datasource as per the documentation. I would expect to see the error locally also if true.
8
Found it, a stray ExecuteReaderAsync()
in a health check class without a using.