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 issue

Steps 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:closed
  • Created 2 months ago
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
ra00lcommented, Jul 14, 2023

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!

0reactions
rojicommented, Jul 14, 2023

But when trying to use that already closed connection, I suspect the “connection pool” error occurs.

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.

  • The “pool exhausted” error is a client-side Npgsql error telling you that your application is using too many open connections concurrently (for whatever reason)
  • The “connection forcibly closed by the remote host” is saying that PG - or some network intermediary - decided to close the connection for some reason.
Read more comments on GitHub >

github_iconTop Results From Across the Web

Error with Npgsql: "The connection pool has been ...
The connection pool has been exhausted, either raise MaxPoolSize (currently 100) or Timeout (currently 15 seconds). how to solve this.
Read more >
Connection Pool has been Exhausted but not on Server
... connection pool has been exhausted, either raise MaxPoolSize (currently 100) or Timeout (currently 15 seconds) at Npgsql.ConnectorPool.
Read more >
What can I do to stop getting Connection Pool error
Just add using to your command creation: using (NpgsqlCommand sqlCmd = new NpgsqlCommand(cmdString, sqlCon)) {. Disposing all objects which ...
Read more >
Npgsql MaxPoolSize exhausted exception
... (0x80004005): The connection pool has been exhausted, either raise MaxPoolSize (currently 100) or Timeout (currently 15 seconds).
Read more >
PostgreSQL-13 The connection pool has been exhausted
... (0x80004005): The connection pool has been exhausted, either raise MaxPoolSize (currently 30) or Timeout (currently 15 seconds).
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found