Avoiding infinite loop with cfinclude?
Copy link to clipboard
Copied
Is there a way I can configure CF or use CFInclude so that this doesn't happen?
thanks,
Chuck
Copy link to clipboard
Copied
quote:
Is there a way I can configure CF or use CFInclude so that this doesn't happen?
Not that I am aware of. Note that CF documentation warns about this situation, so it is up to the programmer to avoid using cfinclude in this manner. I've attached some code that might help you to troubleshoot your problem.
http://livedocs.adobe.com/coldfusion/8/htmldocs/help.html?content=reuseIntro_3.html
Copy link to clipboard
Copied
>
> Is there a way I can configure CF or use CFInclude so that this doesn't
> happen?
>
Other then JR's interesting and useful code you can set the 'Timeout
Requests after (seconds)' property in the Administrator so that
'infinite' loops are not truly infinite. This setting can be used to
restrict how long a request can run on ColdFusion before it aborts it.
It defaults to 60 seconds. This can be a very long time for a loop but
a very short time if you have long running reports or other time
intensive code. So it can be a real balancing act on what this timeout
is set to.
The one thing to note is that it is pretty easy set this value at a
reasonably low level and override it on individual templates with a
<cfsetting requesttimeout=""> tag at the top of any code that needs more
time to process.
Copy link to clipboard
Copied
Copy link to clipboard
Copied
A CF version of PHP's include once sounds like it could be useful. Here is a link to Adobe's feature request form.
http://www.adobe.com/cfusion/mmform/index.cfm?name=wishform
Copy link to clipboard
Copied
> Maybe
> there is a way the include tag could work to detect whether the included value
> equals the name of the actual template being processed, and then throw an
> exception. Seems like it would be simple enough logic to be made part of the
> actual tag process.
Just a note about this, now that you have already officially suggested it.
There are times when I *want* to do exactly this. Have an include
template call itself. This is a common way to do a type of recursion
logic. Of course the template must have conditional logic on when to
break the loop so it is not infinite. But I would not want my CFML to
prevent me from using this useful functionality.
Copy link to clipboard
Copied
quote:
There are times when I *want* to do exactly this. Have an include template call itself. This is a common way to do a type of recursion logic. Of course the template must have conditional logic on when to break the loop so it is not infinite. But I would not want my CFML to prevent me from using this useful functionality.
I'd use a CFC method rather than an include to handle recursion.
Copy link to clipboard
Copied
> I'd use a CFC method rather than an include to handle recursion.
>
I would to, these days.
And before that, a custom tag is probably better then just a plain include.
But still, why be limiting of programmers who *could* use this, just to
protect other programmers who did not test their code?
Copy link to clipboard
Copied
Copy link to clipboard
Copied
Copy link to clipboard
Copied
Our server took another nosedive today when someone did this again -- included a template within itself. Our execution timeout is set to about 35 sec's.
I've noticed that BlueDragon is set to throw an exception for this situation, tho' a few folks have complained:
http://blog.newatlanta.com/index.cfm?mode=entry&entry=12A89E65-158A-6D16-D801BEAD314B7370
Looking into the "include count" option, but that would mean we'd have to touch every page with an include, which isn't really a viable option on a huge site!
-Chuck
Copy link to clipboard
Copied
Thanks,
Chuck
Copy link to clipboard
Copied
If you are resorting to considering custom code "an include on the site that would look for looping" to avoid infinate loops with cfinclude you may want to stop and reconsider how you are using the CFINCLUDE tag and see if there is better way to accomplish what you trying to do.
Copy link to clipboard
Copied
> a template within itself. Our execution timeout is set to about 35 sec's.
>
> I've noticed that BlueDragon is set to throw an exception for this situation,
> tho' a few folks have complained:
[etc]
The problem with this is that you and New Atlanta are both trying to get
the programming language to protect against bad programming.
This is a fool's errand. Creating an endless recursive loop with
<cfinclude> is just an example of poor attention to detail, poor coding, or
poor programming. And the system should just let you do it: you might not
like the results, but you'll get the results you asked for. And you'll
know about it pretty darn quick! :-)
And this sort of thing can crop up in any number of less plain-dumb
situations (come on: including a template from within itself is just plain
*dumb*): loops having an unobtainable exit condition, by-design recursive
functionality not having an exit strategy. A foolishly long-running query.
All these things can flatten a server. And all of them *should*. And the
programming language should not waste cycles proofing for you - the coder -
doing something dumb. It's up to *you* to not be dumb; not the computer.
One thing that concerns me with the situation you describe is that it
sounds like you're doing your *development* on your server, hence when bad
code gets written, it takes the server out. You should not be releasing
untested code to a server. This is very poor practice. Obviously not all
bugs will be caught during the development phase, but this sort of thing
should be, I would think? You should be developing on your workstation -
or at a pinch - a shared dev server. Although by the time code gets to a
shared dev server, it should be more stable that what you're describing.
Last point. PHP's include_once directive is not designed to proof against
this sort of thing. It's designed for situations in which many discrete
components might be used to service one request, and all / any of the
components might require a library to be loaded (like many "pods" needing a
common JS library for their common routines). Any component comprising the
repsonse can load the include, and it might not be known until runtime
which order the components are executed in; but once the include is loaded,
it does not need to be loaded again. It's not to protect against bad code.
--
Adam
Copy link to clipboard
Copied
thanks for any thoughts!
Copy link to clipboard
Copied
> thanks for any thoughts!
Generally that is what a development and|or testing environment is for.
So bad code can be found before it gets to a production server and
affects everybody.
Also the ColdFusion Administrator as the 'Timeout Requests' setting. It
will stop any requests after x seconds. It can be a bit of a balancing
act to set the long enough to run the required applications but short
enough to not let endless code to run too long.
It is helpful that this setting can be modified on an individual
template when required for long running reports and the like.
Copy link to clipboard
Copied
I realize this is a discussion from 13 years ago, but for the sake of any who may come upon it, note that 3 years after the original quesiton (and answers, in 2009), CF10 came out with a new optional runOnce attribute (for cfinclude or its script equivalent), which may help in SOME cases where folks were looking for the rough equivalent to PHP's include_once directive. They're not the same, and it may not have helped in the OP's case here (and nearly everyone involved in the thread may have long-since moved on from CF, or at least that one problem), but I wanted to offer it in case others go looking for the info, as I was helping someone today who was looking for it.
/Charlie (troubleshooter, carehart. org)

