Skip to main content
Inspiring
January 7, 2009
Question

Avoiding infinite loop with cfinclude?

  • January 7, 2009
  • 7 replies
  • 1766 views
Every once in a while it happens. Someone inadvertently has a file, "filename.cfm" with an include like <cfinclude template="filename.cfm"> and it is as simple as that... when requested an infinite loop situation is at hand and the server goes down. On a very large site with multiple developers, this is almost inevitable, so...

Is there a way I can configure CF or use CFInclude so that this doesn't happen?

thanks,

Chuck
    This topic has been closed for replies.

    7 replies

    Charlie Arehart
    Community Expert
    Community Expert
    October 10, 2022

    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)
    Inspiring
    January 21, 2009
    ChuckWWW wrote:
    > 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.
    ChuckWWWAuthor
    Inspiring
    January 21, 2009
    OK -- let me ask it a different way. Say you are a web hosting provider, because this is more like our situation. Say you manage a platform where a *lot* of people are coding. How do you keep your system from going down if someone sticks in some rogue code? We have a number of departments that have developers, and simply mis-typing this include syntax shouldn't be the cause of the server going down. Not if CF is to be deployed in a shared environment at least.

    thanks for any thoughts!
    ChuckWWWAuthor
    Inspiring
    January 7, 2009
    I'd say because it is such a timebomb. Have it throw an exception at least if you don't have the "infinite is OK" flag turned on or some such. An added addtribute that would make it behave like the PHP include_once seems like it would solve more problems that it would cause.
    ChuckWWWAuthor
    Inspiring
    January 9, 2009
    Just a wee update:

    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
    ChuckWWWAuthor
    Inspiring
    January 9, 2009
    Further update -- seems like I could have an include on the site that would look for looping. If it were at the top of every page (that's easy enough to do with a global search/replace, it would abort upon looping more than once. Any reason not to do this, assuming I don't ever code where I include a file in itself?

    Thanks,

    Chuck



    ChuckWWWAuthor
    Inspiring
    January 7, 2009
    Done! Thanks!
    Inspiring
    January 7, 2009
    ChuckWWW wrote:
    >
    > 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.
    ChuckWWWAuthor
    Inspiring
    January 7, 2009
    Thanks, Ian, Bob for the great suggestions. I think the server configuration piece definitely makes sense. A PHP developer colleague tells me that in PHP the include_once function is used precisely to avoid this scenario. 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.
    Inspiring
    January 7, 2009
    ChuckWWW,

    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
    Inspiring
    January 7, 2009
    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