• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

Avoiding infinite loop with cfinclude?

Explorer ,
Jan 07, 2009 Jan 07, 2009

Copy link to clipboard

Copied

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

Views

1.2K

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Advisor ,
Jan 07, 2009 Jan 07, 2009

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

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Jan 07, 2009 Jan 07, 2009

Copy link to clipboard

Copied

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.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Jan 07, 2009 Jan 07, 2009

Copy link to clipboard

Copied

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.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Advisor ,
Jan 07, 2009 Jan 07, 2009

Copy link to clipboard

Copied

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

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Jan 07, 2009 Jan 07, 2009

Copy link to clipboard

Copied

ChuckWWW wrote:
> 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.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Advisor ,
Jan 07, 2009 Jan 07, 2009

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.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Jan 07, 2009 Jan 07, 2009

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?

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Jan 07, 2009 Jan 07, 2009

Copy link to clipboard

Copied

Done! Thanks!

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Jan 07, 2009 Jan 07, 2009

Copy link to clipboard

Copied

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.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Jan 09, 2009 Jan 09, 2009

Copy link to clipboard

Copied

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

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Jan 09, 2009 Jan 09, 2009

Copy link to clipboard

Copied

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



Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Advisor ,
Jan 09, 2009 Jan 09, 2009

Copy link to clipboard

Copied

ChuckWWW,

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.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Jan 09, 2009 Jan 09, 2009

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:

[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

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Jan 20, 2009 Jan 20, 2009

Copy link to clipboard

Copied

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!

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Jan 21, 2009 Jan 21, 2009

Copy link to clipboard

Copied

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.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Oct 10, 2022 Oct 10, 2022

Copy link to clipboard

Copied

LATEST

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)

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Resources
Documentation