Random ASP.NET AppDomain Restarts

Are you having a problem with apparently random application restarts in your ASP.NET application?  Does your session information mystically disappear with no discernable pattern?  Well, the following may help you out.

One of the things that ASP.NET does to help make it easier for developers to modify running web sites is to keep an eye on files that are part of the virtual directory.  If you drop a new version of a DLL into the bin directory, it takes effect from the next request on.  If you make a change to an ASPX file, it too is detected and becomes 'live' with any subsequent request.  No question that this is useful functionality.

A little known fact about this process is that ASP.NET is also aware of the amount of memory that maintaining two versions of the same assembly takes.  If changes were allowed to be made without a restart, eventually it could become detrimental to the overall performance ogf ASP.NET. To combat this, ASP.NET also tracks the number of files that are changed and after a certain number, performs an Application Restart.  As with any other restart, this causes the current session information to be lost.  By default, the number of changes before a restate is 15.  To modify this, you can change the numRecompilesBeforeApprestart attribute in the machine.config file.

While all of this is useful information, there is a small twist that can make our life more difficult.  The files that ASP.NET is watching while looking for a change is not limited to those with an .aspx extension. In fact, it is any file in the virtual directory.  Also, the name of the setting implies that the Recompiles are what is being counted.  It's not.  What is being counted is the number of files that change.  Or, more accurately, the number of changes to any file in the virtual directory. 

How can this cause problems?  What if you were to put a log file into the virtual directory of a web site.  What if that log file were opened and kept open while the information was written to it.  When that log file filled up, it is closed and a new one opened.  Of course, this close counts as a file change.  That would mean that after 15 log files are created (assuming no other changes to the web site), the virtual directory would automatically reset.  And since the amount of information written to the log file will be different depending on which pages/web methods are called, the frequency of the reset is dependent on factors that are not immediately apparent.  Imagine the joy of trying to discover why ASP.NET applications are being randomly reset without the benefit of this blog.

The moral of this story:  Don't put any non-static files into an ASP.NET virtual directory. My good deed for the day is done. 

Comments

  • bruce November 9, 2004 12:49 PM

    This is indeed interesting and useful news, but I have to say I can't replicate your entire conclusion (on .net 1.1).

    When you say that it's not just recompiles of .net managed files (like aspx) but also simply changes to them, and more over even changes to log files, I couldn't make that happen.

    I only experienced the restart when one or more aspx files were changed and re-executed. After the 15th change, the web app did restart.

    I tried just changing (but not executing) an aspx file 15 times and the app never restarted, so it does seem to require re-execution (recompilation). That would make sense, of course, given the purpose you laid out for the behavior (of managing memory).

    Similarly, I created a log file and changed it 16 times. The web app did not restart.

    Still, it was a good deed to point all this out. If you've something that counters my conclusion, I'd love to hear about it.

    Also, it's a shame your blog here doesn't offer a means for us to enter our email to be notified if and when you or anyone else adds a new comment. I'll check back.

  • bruce November 9, 2004 1:02 PM

    My experience was somewhat anecdotal. A client was experiencing random restarts. They were updating a log file using log4net. After a period of time, the AppDomain would restart. They moved the location of the log file out of the virtual directory. Problem went away.

    That having been said, I'll do a bit more digging into my conclusions, especially as it relates to log4net. Perhaps log4net is doing something different than I expected to make this happen. I'll report back when I know more.

  • bruce January 12, 2005 3:24 AM

    It could be a security issue with log4net. Check out my log4net tutorial and read what I discovered. I write logs to a folder under my solution, but outside of my virtual folder...

    log4net Tutorial: Using log4net in an ASP.NET Application
    http://tom.gilki.org/programming/net/120604/

    I hope this helps!

  • bruce May 15, 2005 12:07 PM

    I ran across your piece when I was having a similar problem. In my case I was renaming (moving) a directory in a file intensive app. At least with IIS5 this is easily reproducable.
    You can not rename a directory anywhere in the virtual directory (no matter how deep) or the appdomain will reset. This is not a feature, and is hopefully resolved in .net2
    regards

  • bruce August 16, 2005 8:37 PM

    I have a config file in my web app directory that gets written to and ASP.Net reloads the web app at random. You'd think MS would have defined a bunch of extensions as "ignored" so you can put logging information into it. Really makes me wonder what people do w/ logging files...

  • bruce September 19, 2005 5:53 AM

    You said do not put any non static pages in virtual directory...
    Then how would u suggest setting up a website? I mean, where would you put all the tons of files?
    Thanks

  • bruce September 19, 2005 9:37 AM

    Well Rohan, most of the web sites I know don't have tons of files that are changing regularly. The term 'non-static' might not have been a good choice. From the perspective of this problem, aspx files are static files. That is to say that the contents of the file does not change frequently. even if the page that they display does. The non-static files I'm talking about are XML files, text files, etc that are being added to or changed regularly.

  • bruce September 19, 2005 10:41 PM

    ahh... i see, then in my case, since i am not using any of the files that will change, this should not be a problem. But that does not seem to be the case, I am still experiencing this problem, eventhough i dont have any files that change except of course the SQL Server 2000 which is used as DB, but thats not in the virtual directory...
    Any idea what still might be causing the problems?
    Thanks

  • bruce September 22, 2005 6:02 AM

    hi mr. Bruce,

    many-2 thnx to u 2 solve my prob in just givin a line.i was fighting with it for last couple of months but cudnt catch the flaw.

    thank you once again.

    Mohit Savita.

  • bruce February 6, 2006 9:34 AM

    Hey Guys,

    Our application allows customers to upload pictures that are dropped into a virtual directory below the root.
    Looking at the discussion, I would expect that our application would restart every 15 photos that are uploaded ?
    But this is not the case.
    Why is that ?
    Does ASP.NET only monitor the root and /bin folder ?

  • bruce April 29, 2006 6:42 AM

    I am facing a problem to Dleting a folder at the runtime and session is lost. if i am plaing a folder which files vey frequently changes. is their any security issues which comes afterwords. or could u suggest me something else.

  • bruce April 29, 2006 6:46 AM

    I am facing a problem to Deleting a folder at the runtime and session is lost. if i am placing a folder outside of the virtual directory which files very frequently change. is their any security issues which comes afterwords. or could u suggest me something else to deleting a directory or maintain the session values.

  • bruce June 12, 2006 4:51 AM

    My application restarts and causes problems as the session is lost. I have a virtual directory under root which i use for uploading files. I read and process these files and move them to error or processed folders depending upon the status of the processing.

    Can anyone tell me if this can cause numRecompilesBeforeAppRestart to get incremented?

  • bruce September 22, 2006 4:44 AM

    Thanks for the posting! My AppDomain kept restarting on every webrequest, because I had accidently put my hibernate log files in the Bin directory.

  • bruce September 27, 2006 11:48 AM

    picka, but verry good think.

  • David August 27, 2007 11:47 AM

    Thank you so much! now I can sleep easier again, this been annoying me for months :(

  • oyun September 1, 2007 4:06 PM

    I am facing a problem to Deleting a folder at the runtime and session is lost. if i am placing a folder outside of the virtual directory which files very frequently change. is their any security issues which comes afterwords. or could u suggest me something else to deleting a directory or maintain the session values

  • Farrukh December 3, 2007 11:43 AM

    Hi,

    i have an ASP.Net 2.0 application, my session variables are expiring randomly, usualy when i response.redirect to other pages, i havent place any log files in virtual directory, kindly help me out

  • Ryan Garaygay | Blog March 9, 2008 1:52 PM

    Deleting sub-directories (folders) in ASP.NET 2.0 restarts the AppDomain

Leave a Comment

(required) 
(optional)
(required) 

Comment Notification

If you would like to receive an email when updates are made to this post, please register here

Subscribe to this post's comments using RSS