Search code examples
c#hangfiresqlclient

How to detect connection pool issues in Hangfire?


We are seeing the following errors from Hangfire in our logs from time to time:

Execution BackgroundServerProcess is still in the Failed state for 1.12:52:00.6668371 due to an exception, will be retried no more than in 00:00:15 System.InvalidOperationException: Timeout expired. The timeout period elapsed prior to obtaining a connection from the pool. This may have occurred because all pooled connections were in use and max pool size was reached. at System.Data.ProviderBase.DbConnectionFactory.TryGetConnection(DbConnection owningConnection, TaskCompletionSource1 retry, DbConnectionOptions userOptions, DbConnectionInternal oldConnection, DbConnectionInternal& connection) at System.Data.ProviderBase.DbConnectionInternal.TryOpenConnectionInternal(DbConnection outerConnection, DbConnectionFactory connectionFactory, TaskCompletionSource1 retry, DbConnectionOptions userOptions) at System.Data.SqlClient.SqlConnection.TryOpenInner(TaskCompletionSource1 retry) at System.Data.SqlClient.SqlConnection.TryOpen(TaskCompletionSource1 retry) at System.Data.SqlClient.SqlConnection.Open() at Hangfire.SqlServer.SqlServerStorage.CreateAndOpenConnection() at Hangfire.SqlServer.SqlServerStorage.UseConnection[T](DbConnection dedicatedConnection, Func2 func) at Hangfire.Server.BackgroundServerProcess.CreateServer(BackgroundServerContext context) at Hangfire.Server.BackgroundServerProcess.Execute(Guid executionId, BackgroundExecution execution, CancellationToken stoppingToken, CancellationToken stoppedToken, CancellationToken shutdownToken) at Hangfire.Server.BackgroundProcessingServer.RunServer(Guid executionId, Object state) at Hangfire.Processing.BackgroundExecution.Run(Action2 callback, Object state)

It seems like we are maxing out the number of connections in the SQL Connection pool.

I would like to to detect this somehow and clear out the pool. How do I detect it? Can I use Job Filters for this?


Solution

  • Upgrading to the latest version of Hangfire seems to have fixed this issue.