Graceful ColdFusion Timeout Disaster Recovery (Thanks Barney Boisvert)

Posted August 20, 2007 at 7:00 AM

Tags: ColdFusion

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 »

  • <cffunction
  • name="GetRequestTimeout"
  • access="public"
  • returntype="numeric"
  • output="false"
  • hint="Returns the current request timeout for the current page page request.">
  •  
  • <!--- Define the local scope. --->
  • <cfset var LOCAL = StructNew() />
  •  
  • <!--- Get the request monitor. --->
  • <cfset LOCAL.RequestMonitor = CreateObject(
  • "java",
  • "coldfusion.runtime.RequestMonitor"
  • ) />
  •  
  • <!--- Return the current request timeout. --->
  • <cfreturn LOCAL.RequestMonitor.GetRequestTimeout() />
  • </cffunction>

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 »

  • <cffunction
  • name="KillTime"
  • access="public"
  • returntype="void"
  • output="false"
  • hint="I kill time for the given miliseconds.">
  •  
  • <!--- Define arguments. --->
  • <cfargument
  • name="MS"
  • type="numeric"
  • required="true"
  • />
  •  
  • <!--- Get start and end tick out values. --->
  • <cfset var intStart = GetTickCount() />
  • <cfset var intEnd = (intStart + ARGUMENTS.MS ) />
  •  
  • <!--- Loop until this time is killed. --->
  • <cfloop condition="(GetTickCount() LT intEnd)">
  •  
  • <!--- Just try to kill some processing time. --->
  • <cfset intStart = Sqr(
  • intStart * Pi() * GetTickCount()
  • ) />
  •  
  • </cfloop>
  •  
  • <!--- Return out. --->
  • <cfreturn />
  • </cffunction>
  •  
  •  
  • <!--- ::: DEMO CODE ::: --->
  •  
  •  
  • <!--- Set the current time out to be 2 seconds. --->
  • <cfsetting requesttimeout="2" />
  •  
  • <!---
  • Get the millisecond start time for page processing
  • (so that later on, we can check to see how long
  • the page ran overall).
  • --->
  • <cfset intStart = GetTickCount() />
  •  
  •  
  • <!--- Try to kill some time. --->
  • <cftry>
  •  
  • <!---
  • Here, we are killing time - 4 seconds to be
  • approximate. This will exceed the request time
  • out set above (2 seconds) and will throw an error.
  • --->
  • <cfset KillTime( 4000 ) />
  •  
  •  
  • <!--- The KillTime() method call has timed out. --->
  • <cfcatch>
  •  
  • <p>
  • First Timeout!
  • </p>
  •  
  • <!---
  • Now that our page has timed out, we need to
  • add more time to the request in order to recover
  • gracefully. Add a few seconds onto the request
  • timeout that caused this exception.
  • --->
  • <cfsetting
  • requesttimeout="#(GetRequestTimeout() + 3)#"
  • />
  •  
  •  
  • <!---
  • Now that we have a little more wiggle room,
  • let's email ourselves the caught exception.
  • --->
  • <cfmail
  • to="ben@xxxxxxxx.com"
  • from="no-reply@xxxxxxxx.com"
  • subject="Kinky Solutions Timeout Error"
  • type="html">
  •  
  • <p>
  • The following error was thrown on
  • #DateFormat( Now(), "mmm d, yyyy" )# at
  • #TimeFormat( Now(), "hh:mm TT" )#
  • </p>
  •  
  • <!--- Dump out error. --->
  • <cfdump
  • var="#CFCATCH#"
  • label="Kinky Solutions Exception"
  • />
  •  
  • <!--- Dump out CGI object. --->
  • <cfdump
  • var="#CGI#"
  • label="CGI Struct"
  • />
  • </cfmail>
  •  
  • </cfcatch>
  •  
  • </cftry>
  •  
  •  
  • <cfoutput>
  • <p>
  • Total Time: #(GetTickCount() - intStart)#
  • </p>
  • </cfoutput>

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

Post Comment  |  Ask Ben  |  Permalink  |  Other Searches  |  Print Page




Learning ColdFusion 9 - ColdFusion 9 tutorials, samples, examples, demos

Reader Comments

Aug 20, 2007 at 9:43 AM // reply »
102 Comments

Nice! Been wondering how this would be possible.


Aug 20, 2007 at 10:55 AM // reply »
109 Comments

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. ;-)


jax
Aug 21, 2007 at 3:18 AM // reply »
2 Comments

Very cool. I can use this :-)


Aug 21, 2007 at 12:24 PM // reply »
6,516 Comments

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)?


Aug 23, 2007 at 3:34 PM // reply »
26 Comments

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.)


Aug 23, 2007 at 3:36 PM // reply »
6,516 Comments

@Tom,

I can only test on Standard, so it certainly not limited to Enterprise.


Aug 23, 2007 at 3:45 PM // reply »
34 Comments

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!


Aug 23, 2007 at 3:48 PM // reply »
26 Comments

@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?)


May 14, 2009 at 6:51 PM // reply »
4 Comments

Could something be worked into the onError function for app.cfc?

i.e.

<cffunction name="onError">

<cfargument name="Exception" required="true" />
<cfargument type="String" name="EventName" required="true" />

<cfif arguments.Exception.rootCause eq "coldfusion.runtime.RequestTimedOutException">

<cfsetting requesttimeout="#(GetRequestTimeout() + 3)#"/>

...


May 19, 2009 at 9:41 AM // reply »
6,516 Comments

@Richard,

What I've started to do is actually just set the CFSetting RequestTimeOut to a high number in the OnError method. I no longer bother with checking the existing value - I just set it to something like 10 minutes since I know that nothing in my OnError will hang.


May 19, 2009 at 10:09 AM // reply »
4 Comments

Nice plan!


May 27, 2009 at 3:01 PM // reply »
4 Comments

thanks. I have just added this to our global error-handling as well as our Site-wide Error Handler template, and it works like a charm :D


May 27, 2009 at 3:04 PM // reply »
6,516 Comments

@Aaron,

Awesome! Over time, I have found that setting RequestTimeout to a high number in the error handler is as effective and easier to implement than worrying about what the actual existing timeout is. But, either way, rock on!


Post Comment  |  Ask Ben

Recent Blog Comments
Nov 22, 2009 at 4:30 AM
jQuery Live() Method And Event Bubbling
dasegtezr ... read »
Nov 22, 2009 at 4:03 AM
jQuery Live() Method And Event Bubbling
C_fieri ... read »
Nov 22, 2009 at 1:56 AM
Learning ColdFusion 9: Using CFQuery In CFScript Can Enable SQL Injection Attacks
Why adobe would give you script equivalent of cfquery is beyond me. I love cfquery tag because it helps me wriite clean sql, and get away from the horrible jdbc queries If I wanted to write javali ... read »
Nov 22, 2009 at 1:45 AM
Streaming Text Using ColdFusion's CFContent Tag And The Variable Attribute
The reason you would want to do this is to stream. Ack json/xml files to ria clients I used thus technique before because putting json in response stream causes debugging info to come thru As well a ... read »
Nov 21, 2009 at 6:47 PM
Hal Helms - Real World Object Oriented Development, Sarasota - Day Five
@charlie griefer, Thank you.. ... read »
Nov 21, 2009 at 5:15 PM
Using ColdFusion Structures To Remove Duplicate List Values
@Jose Galdamez, Oh heh yeah I didn't paste the whole code. I should have defined the vars -- my bad. It's fixed thou. Thanks. ... read »
Nov 21, 2009 at 4:49 PM
Styling The ColdFusion 8 WriteToBrowser CFImage Output
Great work yet again Ben! Whilst I didn't use this whole code, I copied some of your regex code for a similar problem with the lack of an alt attribute and unescaped ampersands in CFIMAGE for Railo 3 ... read »
Nov 21, 2009 at 1:13 PM
My First ColdFusion Builder Extension - Encrypting And Decrypting CFM / CFC Files
@Ben, Because I am pedantic, I just want to make sure that everyone knows there is absolutely no encryption going on. There is only encoding and obfuscation. The cfencode tool only obfuscates your C ... read »