IIS application pool is recycled every 20 minutes despite configuration - iis

I'm trying to disable application pool recycling and changed recycling interval in the app pool configuration to 0. Here's full configuration from .config:
<add name="DefaultAppPool" autoStart="true" managedRuntimeVersion="v4.0">
<recycling logEventOnRecycle="Time, Memory, IsapiUnhealthy, OnDemand, ConfigChange, PrivateMemory">
<periodicRestart time="00:00:00">
<schedule>
<clear />
</schedule>
</periodicRestart>
</recycling>
</add>
Despite that application is still recycled several times a day which can be seen in the event log:
A worker process with process id of '1584' serving application pool 'DefaultAppPool' was shutdown due to inactivity. Application Pool timeout configuration was set to 20 minutes. A new worker process will be started when needed.
This happens on Azure Windows 2008 R2 VM with IIS 7.5.
Is there anything else I need to do to make this setting work?

Possible duplicate of IIS: Idle Timeout vs Recycle and others.
What you are looking for is the idleTimeout which you will find under the process model element in applicationhost.config. Unless you have a strong case I would not recommend disabling the timeout as it is a primary mechanism for releasing unused resources when the site in question is not under load.
If it is idle and shuts down then there is a very small overhead as the new process is spooled up, if this is really an issue then I would suggest investigating some form of persisted cache such disk cache offered by the Application Request Routing IIS Module.

Related

How to keep Blazor Server connection active when IIS is recycling the application pool

I am building a Blazor Server intranet application for my customer. One of the requirements is that they can stay logged in indefinitely. If they starting inputting some data on a Friday afternoon, they should be able to return on Monday morning and continue working without interruption.
I came to observe that the client-side was getting disconnected from the server about once per day. When this happened I would see the dreaded Blazor error “Reconnection failed. Try reloading the page if you’re unable to reconnect.”. If I click the link to Reload, it immediately reconnects to my server, but any work in process would be lost.
I found the root cause: by default, IIS is recycling the application pool every 29 hours. When this happens, the Blazor SignalR connection is getting interrupted, and hence the code running in the browser times out and disconnects.
I am able to work around this issue by disabling application pool recycling altogether. So far, it looks like that works fine (I could keep connectivity for the past 3 days). But I am worried this may not be safe long term, since application pool recycling helps deal with issues such as memory leaks, fragmentation, etc.
SO, my question is: is it possible to configure IIS in way that I can recycle the application pool AND also keep my blazor server connection available during that recycle period?
When you recycle an application pool, 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. Thus, the client never "loses all connectivity" to the server - the TCP connection is never lost - and never notices the process recycle.
I believe your problem is with the applications running in your application pool that store state within the process, such as whether a user is logged in or not. Everytime the process recycles, that state is automatically lost... which is by-design since that is what a process recycle accomplishes. As a result, your users "lose all connectivity" and "have to log back into their applications" to re-establish that lost state. The only way to fix this is for your applications to store its state outside of the IIS worker process such that it is friendly to being recycled.
The following blog entry talks more about what is going on:
https://learn.microsoft.com/en-us/archive/blogs/david.wang/why-do-i-lose-asp-session-state-on-iis6

IIS7.5+ : Is it the correct way to describe Application Initialization feature?

Recently we want to cater the slow loading problem of IIS for first request, after I did some research, I've found that IIS7.5+ has a feature named "Application Initialization" which maybe what I need.
However I have to understand the mechanism before I try to apply it and here is my understanding:
With default IIS setting:
The application pool idle after 20 minutes
The corresponding worker process is killed
First request comes in
IIS starts to create a new worker process
IIS starts to load the application
The client can see after application is loaded
And step 4, 5 makes first request not so responsive.
With Application Initialization set:
The application pool idle after 20 minutes
The corresponding worker process is killed
IIS starts to create a new worker process
IIS starts to load the application through a "fake" request
First request comes in
The client can see after application is loaded
Now the first request is responsive as indeed it is not the first request to the server, sometimes before there was a "fake" request which kicks loading of the application.
What I would like to know is that:
Is my understanding correct?
When application initialization is set, the worker process is still being killed, but a new one is created right after it, is it the case?
That's pretty much how it works. Without Application Initialization, as you mentioned, once the worker process is killed, it is not restarted until a request is sent to it. Upon the first request, a new worker process (W3WP.exe) is started and it starts to load the application. And this cold start of the application is what typically makes the first request less responsive. For eg. if it's an ASP.NET application, the first request triggers the recompilation of the temporary ASP.NET files and this can take several seconds in a moderately large enterprise application.
If you look at the setup of Application Initialization, you will see that there are two main parts to it:
You need to set the startMode of the application pool associated with the website to AlwaysRunning
You need to set preloadEnabled to true on some path (path to the website) on the ApplicationPool
Step 1 is what tells IIS to automatically restart the IIS worker process whenever there is a reboot or IISReset. (You can easily see this in action in TaskManager - do only step 1 and do an IISReset, you should be seeing the existing W3WP.exe process getting removed and a new one is getting created)
Step 2 is what tells IIS to make the initial fake/dummy request that will do all the required initialisation of your web application. For eg. for an ASP.NET application, this essentially will trigger the compilation of all the ASP.NET files, so that the next request - the actual first request to the page does not experience the long delays associated with app initialisation.
While it is true that a traditional approach of keeping using a script to poll the app to prevent it from going idle can do the job, the ApplicationInitalization module makes the job much easier. You can even have IIS issue the dummy request to a custom warmup script that does much more than a simple page load - preloading a cache of several webpages, ahead of time generate/do any task that might otherwise take longer etc.
Official documentations here:
IIS 7.5
IIS 8.0
Your understanding is correct based on my experiences. I first ran into this capability in a performance testing scenario way back in 2014. I was custom coding the ping portion of this into monitoring jobs :O
"The Application Initialization Module basically allows you to turn on
Preloading on the Application Pool and the Site/IIS App, which
essentially fires a request through the IIS pipeline as soon as the
Application Pool has been launched. This means that effectively your
ASP.NET app becomes active immediately, Application_Start is fired
making sure your app stays up and running at all times." - Rick Strahl
Official detailed docs are on the MSDN site, from what I see not much has changed between IIS 7.5 and 8.0 in the way of config.

Is it possible to get App Pool recycle error?

I have process named agent.exe, my app pool runs 2 instances of this process.
When recycle my AppPool then most of the times it works and those 2 instances are shutting down and 2 new instances are up.
but sometimes when I recycle my 2 old instances aren't shutting down while my 2 new instances are up and working, so I can have 4,6 and even more instances running.
Can I see why the instances aren't been recycled? I only found this http://technet.microsoft.com/en-us/library/cc735206(v=ws.10).aspx, but those are only informative logs and I can't see errors in them.
Does anyone know way to see recycling errors logs?
My server is Windows 2003 and I'm running IIS6.
It should be noted that there is no connection to "Overlapping recycling" because the instances don't shutting down at all.
thanks.
I found the following logs that can use to identify App Pool recycle errors:
AppPoolRecycleTime - Logs event on time-based recycles.
AppPoolRecycleRequests - Logs event on number of request-based
recycles. AppPoolRecycleSchedule - Logs event on schedule-based
recycless. AppPoolRecycleMemory - Logs event on memory-based
recycle. AppPoolRecycleIsapiUnhealthy - Logs event when worker
processes request recycles because an ISAPI reported unhealthy.
AppPoolRecycleOnDemand - Logs event when an administrator requests a
recycle of all processes in the application pool.
AppPoolRecycleConfigChange - Logs an event if an application pool is
recycled because one of the application pool properties that requires
a recycle to take effect has changed. AppPoolRecyclePrivateMemory -
Logs an event if an application pool is recycled based on private
memory.
taken from here:
http://www.microsoft.com/technet/prodtechnol/WindowsServer2003/Library/IIS/87892589-4eda-4003-b4ac-3879eac4bf48.mspx?mfr=true

what is meant by "failure" in IIS rapid fail protection?

according to the the IIS documentation the rapid fail protection once activated leads to the deactivation of an application pool if a "failure" occurs. However, I could not find the definition of the "failure" case. In my web application I have a special exception that I would like the IIS to consider it as a "failure".
Does anyone have an idea? Thanks
This appears to have a list, for Server 2003 at least: http://web.archive.org/web/20130511004652/http://technet.microsoft.com/en-us/library/cc787273(WS.10).aspx
The WWW service shuts down an application pool whenever a worker
process in the application pool fails often enough to equal or exceed
the Rapid-Fail Protection (RFP) interval time window (for example:
five failures in five minutes). The WWW service detects failure
whenever:
A worker process does not start within the startup time limit.
A worker process does not shut down within the shutdown time limit.
A worker process shuts itself down because of a fatal error and sends
the WWW service an error code.
A worker process fails to respond to a ping message.
The WWW service detects that a worker process is sending non-standard
communications (the worker process may have been taken over).
(updated with archive.org to fix broken link, and replicated detail here)
The documentation for configuring rapid fail protection alludes to a "failure" meaning a worker process crash.
Through experimentation I've noticed that you should expect something like the following in Windows Event Application Logs for a w3wp.exe crash:
An unhandled exception occurred and the process was terminated.
Application ID: /LM/W3SVC/1/ROOT
Process ID: 2628
Exception: System.SomeUnhandledException
Indeed with rapid fail protection enabled with the default configuration, 5 such events within 5 minutes of each other cause the application pool to stop, and you'll see a further Windows Event Application Log similar to:
Application pool 'my-test-application-pool' is being automatically
disabled due to a series of failures in the process(es) serving that
application pool.

Recycle AppPool

Is Recycling an App Pool programatically the same as starting and stopping the app pool in IIS.
Kind of, the difference being that during a recycle http.sys keeps the client connection alive whilst the worker process recycles and transparently routes new requests to the new worker process once it's started.
Also IIS (6 and 7) defaults to using "Overlapped Recycling". What this means is that IIS will start a new worker process before it shuts down the old worker process. Once the new process is started all new requests are directed to the new process.
If you stop and start the application pool you will dump all of your client connections and users may receive a Service Unavailable message whilst the new pool is being started.
Additional differences are:
You can do recycle to your application pool only, while stop & start influences all application pools.
When you are asking to recycle you have time to finalize your work (like saving your data etc.), which is not in restart.
You can schedule the IIS to run the recycle job depends on specific time, after period of time, depends on your memory consumption etc..

Resources