Skip to main content
Ben Nadel at the New York ColdFusion User Group (Mar. 2008) with: Pete Freitag
Ben Nadel at the New York ColdFusion User Group (Mar. 2008) with: Pete Freitag ( @pfreitag )

Ask Ben: Limiting The Amount Of Time A Block Of Code Can Run (Attempt II)

By on

I have this application that uses a black-boxed piece of functionality. 98% of the time, this black-boxed functionality runs very fast as expected. Sometimes though, for reasons I cannot yet debug, this function takes so long that the page times out. Is there a way to make sure that just this block of code only executes for X number of seconds?

Ok, my last attempt at answering this question was a total failure that demonstrated my lack of understanding regarding the CFLock tag. Thankfully, Christoph Schmitz has shown my the light. I have re-attempted an answer using the CFSetting / RequestTimeOut tag and attribute. I am not 100% sure it can be done this way, but testing seems to say that Yes, it can.

<!--- Try to execute the following block of code. --->

		By setting a request time out for this page, we are
		forcing it to have a maximum amount of time that it
		can run. Since the black-boxed code comes after it,
		it will be dominated by this CFSetting.

		This is our black-boxed piece of code. Not
		sure how it works, but we know that 2% of the
		time it runs way longer than it should and
		crashes the page.
		This will only be allowed to execute for
		a maximum of 5 seconds.

		Catch an errors that get thrown from our
		code exceeding the CFSetting timeout attribute.

			Our black-boxed piece of code has run too
			long. Use this opprotunity to set default
			values so that you might be able to recover
			from this timeout.

			Since the rest of the page will take some time
			to execute, set a new time out for this request.

		<!--- Perform recovery code here. --->



Hopefully this is better.

Want to use code from this post? Check out the license.

Reader Comments


while this solution would work for a calling template, it won't work within a function. I think what the person is asking is if they can make a CFC method time out after a certain amount of time. Now personally I have no idea how to do this.


Yeah, this seems to be a very difficult task (or something simple that I am just not thinking of). I saw a presentation on Blue Dragon not so long ago and they had a CFThread tag... I wonder if the CFThread tag has a timeout attribute; that seems like something that would do the trick here - wrapping the "locked down" code in a CFThread tag and forcing a small timeout.


When I use "while" loops, I often do something like this:

<!--- Set the time before entering the loop --->
<cfset var timeout = now() />

<cfloop condition="TRUE"> something here...

<!--- End the loop if it has run for more than five seconds --->
<cfif dateDiff("s", timeout, now()) GTE 5>
<cfbreak />

Of course, that won't prevent the processing during an individual iteration of the loop from running more than five seconds.


YES YES YES!!! thank you so much for this post!

I was having trouble with a cffeed from an external site which was timing out constantly so getting a horrible error message on my page,

but i stumbled across this solution after lots of googling and now i have it so if my first feed times out then it reads in an alternative feed so no more errors!

I know your post is quite old but its just helped me out so much!

I believe in love. I believe in compassion. I believe in human rights. I believe that we can afford to give more of these gifts to the world around us because it costs us nothing to be decent and kind and understanding. And, I want you to know that when you land on this site, you are accepted for who you are, no matter how you identify, what truths you live, or whatever kind of goofy shit makes you feel alive! Rock on with your bad self!
Ben Nadel