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

asp.net-mvc c# entity-framework iis-7.5 linq-to-entities

Question

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.

1
19
5/3/2015 11:15:16 AM

Accepted Answer

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.

5
9/30/2015 2:04:23 PM

Popular Answer

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



Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow