Semaphore exception - Adding the specified count to the semaphore would cause it to exceed its maximum count

Ali Haider picture Ali Haider · May 3, 2015 · Viewed 23.8k times · Source

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:

  • Check whether a DB transaction object is being disposed.
  • Check whether a DBContext (ctx) is being disposed prematurely.
  • Check application build (32/62 bit). In this case - I build the application in ANY CPU mode and my server is 64 bit.

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.

Answer

Luis Teijon picture Luis Teijon · Apr 6, 2017

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