Losing ASP.NET Sessions - Why Application Pools recycle

I've seen a more or less constant stream of questions on the asp.net newsgroup and the asp.net site forum messageboard, all of which revolve around the problem of "Why am I losing Sessions?". (BTW that is "losing" with one o, not "loosing"! Your pants might be loose, but if your Session goes away you can bet that you are losing it!).

Heres a summary of what I've learned about this; comments and additions are always welcome:

If your ASP.NET application crashes, has an unhandled exception, hangs or otherwise becomes brain-dead, it will cause the application pool to recycle. Sometimes your application pool recycles for no obvious reason. This is usually a configuration issue or it may be caused by your app performing file system operations in the application directory. Many times developers incorrectly set up SqlConnections so that they aren't properly closed and returned to the connection pool, and this can also cause your AppPool to recycle unexpectedly. When your AppPool recycles, you can kiss your InProc Sessions - and everything else -- goodbye.

Application pool settings

Looking at the properties for the application pool in IIS, you'll see the settings for "on purpose" recycling. In IIS6 these are:

  • Recycle worker processes (in minutes)
  • Recycle worker process (in requests)
  • Recycle worker processes at the following times
  • Maximum virtual memory
  • Maximum used memory

If you're running IIS5 or the IIS5 isolation mode you must look at the processModel element of machine.config. The properties you should pay attention to are:

  • memoryLimit
  • requestLimit
  • timeout

In IIS 7.o, you have Fixed Interval or Fixed # Requests, or Specific Times for recycling. Also, there are Memory -based Maximums for Virtual and Private Memory, and additional items for Configurable and Runtime recycling events including "unhealthy ISAPI".

When an application pool recycles, HTTP.SYS holds onto the client connection in kernel mode while the user mode worker process recycles. After the process recycle, HTTP.SYS transparently routes the new requests to the new worker process. Consequently, the client never "loses all connectivity" to the server; the TCP connection is not lost -- only state is lost (Application, Session, Cache, etc.).

memoryLimit

The default value of memoryLimit is 60. This value is only useful if you have a small amount memory on a 32 bit machine. "60" means 60% of total system memory. So if you have 1 GB of memory your IIS worker process will automatically restart once it hits memory usage of 600 MB.

requestLimit

This setting is "infinite" by default, but if it is set to 8000 for example, then ASP.NET will launch a new worker process once it has handled 8000 requests.

timeout

The default timeout is "infinite". This is where you set the lifetime of the worker process. Once the timeout is reached ASP.NET launches a new worker process, so setting this to "00:30:00" would recycle your application every 30 minutes.

Other properties

Another property within the processModel element that will cause your application pool to recycle is responseDeadlockInterval. If you have a deadlock then that's your main "fix" that you need to worry about -- changing the responseDeadlockInterval setting won't do much to resolve the problem. You need to deal with the deadlock itself, find out why it's happening, and change your code.

File Change Notification

ASP.NET 2.0 depends on File Change Notifications (FCN) to see if the application has been updated, and depending on the magnitude of change the application pool will recycle. If you or your application are adding and removing directories to the application folder, then you will be restarting your application pool every time.

Altering the following files also causes an immediate restart of the application pool:

  • web.config
  • machine.config
  • global.asax
  • Any file in the /bin directory or subfolders

Updating .aspx files, etc. causing a recompile eventually triggers a restart of the application pool also. There is a property of the compilation element under system.web called numRecompilesBeforeAppRestart. The default value is 20, meaning that after 20 recompiles the application pool will recycle.

Workaround for the sub-directory issue

If your application actually requires adding and removing sub-directories you can use linkd to create what's called a directory junction:

Create a directory you'd like to exclude from FCN, e.g. c:\inetpub\wwwroot\MyWebApp\MyFolder
Create a separate folder somewhere outside the wwwroot, e.g. c:\MyExcludedFolder
Use linkd to link the two: linkd c:\inetpub\wwwroot\MyWebApp\MyFolder c:\MyExcludedFolder
Now any changes made in the c:\inetpub\wwwroot\MyWebApp\MyFolder will now actually occur in c:\MyExcludedFolder so they will not be sensed by FCN.

Linkd only comes with the Windows XX Resource Kit, which is a pretty big download. But Mark Russinovitch has "junction" which could be even better:

http://www.microsoft.com/technet/sysinternals/FileAndDisk/Junction.mspx

Is recycling the application pool good or bad?

If your app is coded properly, you shouldn't have to recycle the application pool. However, if you're dealing with a memory leak in your app and you need to buy time to fix it, then recycling the application pool could be a good idea. It's important to understand, though, that's not a "Fix" - it's just a "Band-Aid" until you find out what's causing the problem and fix your code. Unlike as with ASP.NET 1.1, in ASP.NET 2.0 if your app generates an unhandled exception the AppDomain will unload causing an application pool recycle. Consequently it is extremely important to ensure that your code is "best practices" and doesn't generate unhandled exceptions except under the most extreme and unusual conditions.

Additional Resources

http://msdn2.microsoft.com/en-us/library/aa719566(VS.71).aspx

http://msdn2.microsoft.com/en-us/library/aa720473(VS.71).aspx

http://forums.asp.net/t/623320.aspx

http://www.microsoft.com/technet/technetmag/issues/2006/01/ServingTheWeb/

http://www.microsoft.com/technet/prodtechnol/WindowsServer2003/Library/IIS/26d8cee3-ec31-4148-afab-b6e089a0300b.mspx?mfr=true

http://blogs.msdn.com/david.wang/default.aspx

Comments

  1. Anonymous10:18 AM

    This is a great summary of all the finer points of application pool recycling points - good post!

    One of the main reasons for the constant stream of questions about Sessions (and sometimes the "near-panic, solve-my-problem" issues) is that perhaps these applications have been designed to rely fully on in-proc sessions and their contents.
    That is, the state-less model of the application has been bypassed with an assumption that the session will always exist for the life of the user visit.
    Such assumptions are nearly always false (as the many points of recycling this post illustrates) and should not be incorporated into any design in the first place.

    Session information can and should be used, just don't rely on it so much that the user experience is affected by the loss of it.

    ReplyDelete
  2. Good advice, totally agree.

    ReplyDelete
  3. An alternative to junction is to just map a virtual sub directory in IIS.

    Also, you can look at System.Web.Hosting.HostingEnvironment.ShutdownReason for a hint on what caused the recycle. See http://weblogs.asp.net/scottgu/archive/2005/12/14/433194.aspx for more info.

    ReplyDelete
  4. I have strange problem for Session.

    Whenever I am trying to login I am storing User Information in Session, now that works fine everytime except first time. So even if i am login and valid user i need to login twice, because some how my session value got null for first time, but for subsequent login value is persist and no problem. I tried debugging each and every scenario to find mistakes in code, but there is no mistakes. Session is getting null on redirection of page. Any idea or help?

    ReplyDelete
  5. Prakash Rout12:44 AM

    this advice can solve all the issues of worker process.

    ReplyDelete

Post a Comment

Popular posts from this blog

Some observations on Script Callbacks, "AJAX", "ATLAS" "AHAB" and where it's all going.

IE7 - Vista: "Internet Explorer has stopped Working"

FIREFOX / IE Word-Wrap, Word-Break, TABLES FIX