Previously, I blogged about how hard it is to recovery gracefully from ColdFusion request timeout exceptions. The big problem is that after a particular tag or algorithm times out, even if you CFCatch the thrown exception, you simply don't have any processing time to do anything with the error object. Once a page times out, you have about 16-30 milliseconds to perform recovery actions before the thread actually craps out. That pretty much excludes any kind of CFMail or CFDump action (and if you want to log anything to a file, just forget about it!).
In the brief post-timeout period, I demonstrated that you could simply set a higher request timeout in the ColdFusion CFSetting tag. However, unless your page timeouts are always the same, this was pretty much hit or miss. Barney Boisvert just dropped a bomb shell on me last week, demonstrating how to get the current request timeout from ColdFusion Request Monitor.
When I saw that, I immediatly knew it could be leveraged for this task. Using Barney's tip, it now becomes super easy to gracefully recover from a ColdFusion timeout exception. First, we need to create a simple ColdFusion user defined function that encapsulates the getting of the current request timeout; after all, I don't necessarily want to remember how to do this:
Launch code in new window » Download code as text file »
Once we have that in place, we can update our disaster recovery methodology to add just a few seconds to the current request timeout once a ColdFusion timeout exception has been thrown:
Launch code in new window » Download code as text file »
Notice that in our CFCatch tag, once we know that the function, KillTime(), has timed out, we are not playing hit-or-miss with the new CFSetting tag. In fact, we are making very sure to only add three more seconds to the page processing. In that time, we are CFMailing ourselves the error as well as the user's CGI object.
Running the above page, we get the following output:
First Timeout!
Total Time: 2078
Notice that after the first request timeout exception was thrown (First Timeout!) we were able to continue processing the CFMail tag. All in all, it took 78 milliseconds to run the additional CFMail / CFDump scripts. This is the kind of disaster recovery that we would NOT have had time to do if we didn't mess with the page's request timeout.
Thanks Barney!
Oh, and incidentally, I had the pleasure of meeting Barney in person at CFUNITED 2007. Very cool guy - seemed way smarter than myself. However, I am embarrassed to say that I was totally mispronouncing his name. Apparently, it is a name of french descent and is pronounced "Bo-v'air". Sorry Barney :)
Download Code Snippet ZIP File
Comments (8) | Post Comment | Ask Ben | Permalink | Other Searches | Print Page
Nice! Been wondering how this would be possible.
Posted by Gareth on Aug 20, 2007 at 9:43 AM
Yes, Barney is just a great guy as well as being a friggin' genius on just about any topic. And don't feel too bad about mispronouncing his name, I think everyone does at first. ;-)
Posted by Brian Kotek on Aug 20, 2007 at 10:55 AM
Very cool. I can use this :-)
Posted by jax on Aug 21, 2007 at 3:18 AM
I think I would like to make this standard place in my applications as part of my catch-all error handling. Just add one second and you should have enough time to take care of all the logging and possible emailing. I think it's just good practice.... anyone see any red flags (and remember and error that goes of inside of the OnError() event method will NOT cause infinite error handling)?
Posted by Ben Nadel on Aug 21, 2007 at 12:24 PM
This is fantastic! I see that this technique works on CF7-- this isn't limited only to the Enterprise version of CF, is it? (I ask because Enterprise is all I can currently test on.)
Posted by Tom Mollerus on Aug 23, 2007 at 3:34 PM
@Tom,
I can only test on Standard, so it certainly not limited to Enterprise.
Posted by Ben Nadel on Aug 23, 2007 at 3:36 PM
Saw this post on the ColdFusion Weekly podcast del.icio.us link roll.
I just incorporated this idea into the error plugin file for my Fusebox 5 application, and it works like a charm! Thanks for the info!
Posted by Brian Swartzfager on Aug 23, 2007 at 3:45 PM
@Ben: Good to know that it works on Standard as well as Enterprise, and CF7 as well as CF8. (Anyone care to test it out on CF6, for the heck of it?)
Posted by Tom Mollerus on Aug 23, 2007 at 3:48 PM