I've been having this SemaphoreFullException for quiet some time.
To summarize.. I have hosted an application on IIS 7.5 with ASP.NET v4.0 framework Application Pool (integrated). I am using windows authentication to authenticate my users through domain (isinrole).
I've seen all other threads on this topic, where it is suggested to set Pooling = False. I do not want to do that and I would like to keep on using pooling because of the performance benefits.
I am using Entity Framework 6 to query the database and I am not "disposing" the dbcontext anywhere in user code. It looks like the issue is in the DbConnectionPool code.
The error occurs randomly at any given moment of time. It doesn't matter if the application is being used or not. Sometimes, due to this issue - I have to restart IIS because new users stop getting authenticated.
What I've tried so far:
Note: In my application, I've mostly used linq-to-EF objects to query the DB.
Exception: System.Threading.SemaphoreFullException
Message: Adding the specified count to the semaphore would cause it to exceed its maximum count.
StackTrace: at System.Threading.Semaphore.Release(Int32 releaseCount)
at System.Data.ProviderBase.DbConnectionPool.CleanupCallback(Object state)
at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
at System.Threading.TimerQueueTimer.CallCallback()
at System.Threading.TimerQueueTimer.Fire()
at System.Threading.TimerQueue.FireNextTimers()
Any help in this regard will be greatly appreciated.
In my case, the problem was that I stopped the application while debugging. The application was making a lot of async callings.
So I reset my IIS server: iisreset
via Command Prompt or PowerShell, and it worked.
EDIT: Look at the @aaroncatlin comment for IIS Express
I think that this may be a solution to the problem: http://www.davepaquette.com/archive/2013/03/27/managing-entity-framework-dbcontext-lifetime-in-asp-net-mvc.aspx - as you can see there, it is essential to take care for disposal of the DbContext when it´s lifetime is over.
Remember, Db connections end up in unmanaged db handling code, so the problem is unless garbage collection disposes the context it stays sleeping in the main memory, thereby also blocking a connection from the connection pool. So sooner or later, under the right conditions, you empty the connection pool and get your exception.
I recently hit a similar issue of SemaphoreSlim. The problematic code is below and the same exception is raised in semaphoreSlime.Release()
. The cause here is that WaitAsync
failed due to token cancellation, hence Release()
will cause allowed max count exceeded. So, a solution could be adding some defense code before calling Release()
, e.g., check whether exception happened during WaitAsync
, check allowed account whether exceeding allowed count.
try
{
await semaphoreSlim.WaitAsync(cancellationToken);
// do some work
}
catch (OperationCanceledException)
{
// exception handling
}
finally
{
semaphoreSlim.Release();
}
I had the same problem and was because I was doing .Dispose();
before closing the connection this is how I solved it:
I had two instances of .Dispose();
- one in a SqlDataAdapter, the other in one SqlCommand and after that was closing the connection and getting the error.
Just removed the .Dispose();
from my SqlCommand and my SqlDataAdapter, and no more error! I hope this helps somehow.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With