The connection pool has been exhausted

See original GitHub issue

Npgsql version: 3.2.4 PostgreSQL version: 9.5 .Net version: 4.6.1 Operating system: Win7 x64

We are working on converting an application from RavenDB to Marten which uses Npgsql under the covers. While importing files we are running into the “The connection pool has been exhausted” exception even though I’ve never seen the connections in pgAdmin go over 25-30. I’m running this application on my local box with a local instance of Postgres.

Here is what pgAdmin shows right before the exception occurs: pgadmin The ‘Windows Service’ is what is doing all of the heavy lifting. The ‘Website’ is just the front end that pushes messages over to the service for processing and does reads for display purposes. It takes a few minutes for this exception to surface and the ConnectorPool.Busy property is increasing the entire time until it happens. So there isn’t a sudden burst of activity that makes it exceed the pool size.

After the exception occurs I took a memory dump of the process and dumped the ConnectorPool object and it still shows 98 busy connections. You can also see that there are only 4 NpgsqlConnection and NpgsqlConnector classes in memory.
windbg

At this point the service needs to be restarted as doing about anything results in subsequent “The connection pool has been exhausted” exceptions being thrown since the pool only has 2 free slots. Calling something like NpgsqlConnection.ClearAllPools doesn’t reset Busy so there isn’t really a way to recover from this that I can see.

One thing I noticed when browsing the ConnectorPool code is that calls to IncrementBusy and DecrementBusy are usually protected with a lock/monitor but not always(here).

Any thoughts? Even if we are leaking connections(which I haven’t seen/verified yet) it doesn’t look like Npgsql is recovering from this exception gracefully. I’m still digging into this issue on my end.

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Reactions:2
  • Comments:13 (7 by maintainers)

github_iconTop GitHub Comments

1reaction
JordanZaerrcommented, Jun 20, 2017

After digging into this a bit more it looks like the underlying cause is we have open connections that are getting garbage collected without being disposed(this was buried under lots of abstractions/DI). Rebuilding the npgsql code with a couple finalizers that clean up proved this was what was actually going on. That explains why the server wasn’t seeing the connections and why the objects didn’t show up in the memory dumps. Since they were never disposed the pool didn’t get notified to decrement the busy count.

On a side note the NumberOfNonPooledConnections performance counter would jump from zero to approximately 2,147,483,647 (Int.MaxValue).

1reaction
lwansbroughcommented, Jun 16, 2017

@roji I’m seeing a similar issue. I’m not getting an exception, but my busy pool fills up and no connections are released back into the pool. I’m running in a single-threaded async environment (.NET Orleans). What I see is my application very quickly uses the 50 available connections (as expected), but they all remain “busy” despite being disposed (I checked their internal _disposed value to verify.) These are trivial read ops, by the way - not long running SQL ops. So effectively what happens is, my Orleans app launches as many grains as it can when it starts up, but after the 50 connections get used up, the rest of the grains end up waiting indefinitely for the connections to be released. (I have Timeout=0). I would expect the connections to be released back into the connection pool once they have been disposed but this doesn’t appear to be the case. I’m happy to provide code if necessary.

Read more comments on GitHub >

github_iconTop Results From Across the Web

What can I do to stop getting Connection Pool error
Npgsql.NpgsqlException: 'The connection pool has been exhausted, either raise MaxPoolSize (currently 100) or Timeout (currently 15 seconds)'.
Read more >
Error with Npgsql: "The connection pool has been ...
This is a problem on your client / npgsql side. It seems like you set Max connection limit to 100 and are trying...
Read more >
Connection Pool Exhausted · Issue #3041 · npgsql ...
On high traffic I'm getting Pool exhausted problem. ... The pool implementation has indeed been rewritten for 5.0.0, and there's no longer a ......
Read more >
PostgreSQL-13 The connection pool has been exhausted
You probably have a connection leak in your application code, where connections aren't returned to the pool. This all happens on the application ......
Read more >
Npgsql MaxPoolSize exhausted exception
Npgsql.NpgsqlException (0x80004005): The connection pool has been exhausted, either raise MaxPoolSize (currently 100) or Timeout (currently ...
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