The connection pool has been exhausted, either raise 'Max Pool Size' (currently 100) or 'Timeout' (currently 15 seconds) in your connection string
See original GitHub issueSteps to reproduce
DIdn’t manage to reproduce in a project yet, sorry. But will continue working on it.
The issue
I get the error in the subject every couple of days, randomly through the month, for a SaaS project I am working on that has a couple hundred daily users.
I made sure I open the connection to postgres in a using statement, so I’ve eliminated that issue completely.
When I get this error and check pg_stat_activity, I only have 1-2 connections, far under the threshold of 100 max connections.
However, I still get it.
This morning, I had a revelation as to why this happens. The postgres logs are sprinkled with errors like could not receive data from client: An existing connection was forcibly closed by the remote host.
So my theory as to why the error is happening is that npgsql does not gracefully close connections that are already closed on the postgres side, keeping them as active and attempting to reuse them from the pool, when they, in fact, are long closed and cannot be used.
So, my question to you: do you know if the scenario when postgres closes the connection (maybe it takes too long to execute, or requires bigger resources than the system has available) is handled in the npgsql code, so that the closed connection is not reused, but disposed completely?
Exception message:
Stack trace:
Further technical details
Npgsql version: 7.0.4 PostgreSQL version: 10 Operating system: Windows Server 2016
Other details about my project setup:
Issue Analytics
- State:
- Created 2 months ago
- Comments:6 (3 by maintainers)
Top Related StackOverflow Question
I tried to reproduce the issue by using
select pg_terminate_backend(pg_backend_pid());+ a max pool size of 2 but it seems everything works fine. I did however found, accidentally, a un-disposed connection which I think was the reason for the leak.Turns out my issue was the same as 99% of the others. Un-closed connections. Thanks @roji for your support! You guys rock!
That might be what the “connection forcibly closed by the remote host” error is saying (Npgsql tries to use a connection that some intermediary closed due to inactivity), but that’s not really related to the Npgsql “pool exhausted” error.