views:

57

answers:

2

I'm working on a ASP.NET website that on some requests will run a very lengthy caching process. I'm wondering what happens exactly if the execution timeout is reached while it is still running in terms of how the code handles it.

Particularly I am wondering about things like if the code is in the try of a try/finally block will the finally still be run?

Also given I am not sure I want the caching to terminate even if it goes on that long is there a way with spawning new threads, etc. that I can circumvent this execution timeout? I am thinking it would be much nicer to return to the user immediately and say "a cache build is happening" rather than just letting them time out. I have recently started playing with some locking code to make sure only one cache build happens at a time but am thinking about extending this to make it run out of sync.

I've not really played with creating threads and such like myself so am not sure exactly how they work, particularly in terms of interacting with ASP.NET. eg if the parent thread that launched it is terminated will that have any effect on the spawned thread?

I know there is kind of a lot of different questions in here and I can split them if that is deemed best but they all seem to go together... I'll try to summarise the questions though:

  1. Will a finally block still be executed if a thread is terminated by ASP.NET while in the try block
  2. Would newly created threads be subject to the same timeouts as the original thread?
  3. Would newly created threads die at the same time as the parent thread that created them?
  4. And the general one of what is the best way to do long running background processes on an ASP.NET site?

Sorry for some noobish questions, I've never really played with threads and they still intimidate me a bit (my brain says they are hard). I could probably test the answer to a lot of tehse questions but I wouldn't be confident enough of my tests. :)

Edit to add:

In response to Capital G:

The problem I have is that the ASp.NET execution timeout is currently set to one hour which is not always long enough for some of these processes I reckon. I've put some stuff in with locks to prevent more than one person setting off these long processes and I was worried the locks might not be released (which if finally blocks aren't always run might happen I guess).

Your comments on not running long processes in ASP.NET is why I was thinking of moving them to other threads rather than blocking the request thread but I don't know if that still counts as running within the ASP.NET architecture that you said was bad.

The code is not actually mine so I'm not allowed (and not sure I 100% understand it enough) to rework it into a service though that is certainly where it would best live.

Would using a BackgroundWorker process for something that could take an hour be feasible in this situation (with respect to comments on long running processes in ASP.NET). I would then make request return a "Cache is building" page until its finished and then go back to serving normally... Its all a bit of a nightmare but its my job so I've got to find a way to improve it. :)

+1  A: 
  1. Interesting question, just tested and no it's not guaranteed to execute the code in the finally block, if a thread is aborted it could stop at any point in the processing. You can design some sanity checking and other mechanisms to handle special cleanup routines and such but it has a lot to do with your thread handling as well.

  2. Not necessarily, it depends on how your implementing your threads. If you are working with threads yourself, then you can easily get into situations where the parent thread is killed while it's child threads are still out there processing, you generally want to do some cleanup in the parent thread that ends the child threads as well. Some objects might do a lot of this for you as well, so it's a tough call to say one way or the other. Never assume this at the very least.

  3. No, not necessarily, don't assume this at least, again has to do with your design and whether your doing threading yourself or using some higher level threading object/pattern. I would never assume this regardless.

  4. I don't recommend long running processes within the ASP.NET architecture, unless its within the typical timeout, if it's 10-20s okay but if it's minutes, no, the reason is resource usage within ASP.NET and it's awfully bad on a user. That being said you could perform asynchronous operations where you hand off the work to the server, then you return back to the user when the processing is finished, (this is great for those 10-20s+ processes), the user can be given a little animation or otherwise not have their browser all stuck for that long waiting for whatever is happening on the server to happen.

If it is a long running process, things that take greater than 30-60s+, unless it absolutely has to be done in ASP.NET due to the nature of the process, I suggest moving it to a windows service and schedule it in some way to occur when required.

Note: Threading CAN be complicated, it's not that it's hard so much as that you have to be very aware of what your doing, which requires a firm understanding of what threads are and how they work, I'm no expert, but I'm also not completely new and I'll tell you that in most situations you don't need to get into the realm of threading, even when it seems like you do, if you must however, I would suggest looking into the BackgroundWorker object as they are simplified for the purposes of doing batched processing etc. (honestly for many situations that DO need threads, this is usually a very simple solution).

http://msdn.microsoft.com/en-us/library/system.componentmodel.backgroundworker.aspx

Capital G
@Capital G: thanks for the comments. They are really useful. I had a lots of questions but they were too big for comments so I updated the question text with questions arising from your suggestions.
Chris
Yes, sorry I'll clarify, your application is running against the context of an ASP.NET application, usually hosted in IIS and every so often it will be schedule to recycle (free up resources, etc) to keep things running smoothly, when this happens any threads you created in that context are going to be destroyed. I think it's a bad idea to have a one hour long process running within the ASP.NET context, that really ought to be moved to a service that could then communicate "I'm processing" via the database to the website is connected to. I understand your non-ideal work situation though.
Capital G
If you absolutely HAVE to put this in your ASP.NET application and can't move it out to a service, then use a BackgroundWorker and increase the recycle time in IIS so it doesn't happen for many hours, a lengthy enough time for your process to complete. It's definitely less than ideal but it sounds like your hands are pretty tied with moving this caching process into a service where it belongs, I feel for ya man.
Capital G
+1  A: 

Long or time consuming processes to be started behind the web-page; it should not hit the ASP.NET execution time out; the user page should be freed; running the requests under lock etc. All these situation points towards using async services. In one of the products, where I architected, used services for such scenarios. The service exposes some async method to initiate. The status of the progress can be queried using another method. Every request is given some id and no duplicate requests are fired ever. The progress proceeds even if the user logs out. The user can see the results at a later time.

If you have looked at such options already, let me know if there is any issue. Or if you are yet to look in this direction, please get it this way. For any help, just send in your comments.

Kangkan
@Kangkan: Unfortunately I am not practically allowed to rewrite the current code into a service based approach. Technically I am not allowed to touch any of the caching code which this is but I can change the code that calls the caching code which is in my website. This is why I have looked at implementing locking and am now worried about all the other parts of the process and indeed the potential pitfalls of locking. Thanks for your thoughts though.
Chris
So is the code that you can not touch runs as a DLL? In such a case you can simply wrap it up in a service. Let me know the details of how the other part runs.
Kangkan