Duplicate Application name issue

A friend of mine was tearing his hair out trying to solve a problem that occurred randomly. For some reason he would get an error about a certain application variable not existing. He showed me the code - and we both agreed that this was simply not possible. The variable was clearly being defined in the application. The error would normally occur after some amount of down time. If he started the application himself he could never recreate the error. I had him turn down the application timeout to 5 minutes, let the application time out, and we still couldn’t recreate the issue.

Turns out the problem was far simpler. He had another application on the site with the same name! The reason the error would occur randomly and after a period of inactivity was that sometimes the other application would load up before the one that was throwing errors.

So this brings up the question - how do you prevent this? On a box that you control this is relatively simple. You could do a global search and replace over all the folders that contain code. Unfortunately, this will only be easily done for cfapplication tags. You would also need to search for this.name but only inside Application.cfc files. It is possible though. (Do I smell a new Friday Puzzler?) On a shared server though this is impossible, and even if you did do a scan, some other client can easily upload code after you scan. (To be clear, this is a problem on a shared server where you share one ColdFusion instance.)

You can help prevent this by using unique application names. Since the only purpose of the application name is to give the code a unique memory space on the server, you don’t have to make a ‘friendly’ name at all. You could use a long name - just be sure the name is less than or equal to 64 characters in length. As an example, consider this code from BlogCFC. It actually uses a “base” name and also adds part of the file path to it. I did this for folks running BlogCFC on shared servers:

<cfset prefix = hash(getCurrentTemplatePath())> <cfset prefix = reReplace(prefix, "[^a-zA-Z]","","all")> <cfset prefix = right(prefix, 64 - len("_blog_#blogname#"))> <cfapplication name="#prefix#_blog_#blogname#" sessionManagement="true" loginStorage="session">

Raymond Camden's Picture

About Raymond Camden

Raymond is a developer advocate. He focuses on JavaScript, serverless and enterprise cat demos. If you like this article, please consider visiting my Amazon Wishlist or donating via PayPal to show your support. You can even buy me a coffee!

Lafayette, LA https://www.raymondcamden.com

Comments