Just when we thought we had conquered it, the VAR scope came back to haunt us.
We installed TeaLeaf to improve the usability of our website. While mining the error reports, we found an error that was happening nearly a dozen times a week and had never been reported to our help desk.
We have a Reports application written with Mach-II where Insurance Agents can select from a list and generate HTML or XLS reports on the fly.
1. What is the error message?
2. In which file did this happen?
The error was occuring inside a Mach-II Listener.
So what do we know about the Mach-II Listener?
- It's a ColdFusion Component (CFC) file.
- It's setup in the mach-ii.xml file.
- It's a singleton.
- When it's created, it's cached in the application scope.
3. Which function is throwing the error?
Can you see the error? Does it just jump out at you?
4. Why is reports in the variables scope? Shouldn't it be var scoped?
If the error didn't jump out at you, then read up on the VAR scope.
If I hadn't been writing that series, this would not have been the next step in my thought process. The rest of my thought process would have been:
Thankfully, I was able to skip those steps and realized where the error lie.
- What does this function do?
- Where does the data come from?
- Where does the data go?
What do we know about CFCs placed in the application scope?
- You should var scope variables specific to a function.
- Anything in its internal variables scope is available to every function in it.
- Anything in its internal variables scope is available to and can be manipulated by every thread that calls it.
Now that we know the solution, what's more interesting is how we were even able to find the error. When we overhauled the site, one of the Coding Standards I asked everyone to follow was to use Struct Functions as much as possible instead of shortcutting through the code.
The error occured on this line:
The message was "Cannot insert item with key claimsReported.". claimsReported is the name of one of the reports we list.
The message detail was "This key already exists", which means that the code was trying to add a key to the struct variables.reports that was already in the struct.
But the list we send in always has unique values, so there's no way that this error should occur. Right?
What had been occuring is that two or more report requests would happen concurrently and the value of variables.reports would be manipulated by all of the threads at once.
Thread A asks for "claimsReported,newPolicies,reportE,reportG".
Thread B asks for "cancelledPolicies,claimsReported,reportB,reportC,reportD"
Thread C asks for "newPolicies,reportA,reportD,reportH"
The value of variables.reports would be changed constantly as each thread entered the function and began to loop over its list of reports.
By the time Thread A completed its loop of 4 list items, it's possible that Thread B had started looping over its list, so Thread A could get back a struct with more or less than 4 keys in it.
Recreating the error
Let's create a simple function that loops over a list and turns each list element into a key of a struct.
Now let's try and cause the same error. We need to try and insert a struct key that already exists.
StructInsert requires the struct you're using, which key to create and what its value will be. The optional attribute is allowoverwrite, which is defaulted to false. This is why the error was thrown.
So what would have happened if we hadn't used StructInsert? What if we had used an alternate Struct syntax?
See that? No error.
ColdFusion just overwrites the value of reports["a"] (a.k.a. reports.a), no questions asked.
So thankfully, this was an easy fix.
Now each thread gets its own instance of reports to manipulate. The method is now thread safe.
TeaLeaf & Coding Standards Return on Investment (ROI)
FWIW, we had run Mike Schierberl's varScoper tool over our entire code base before we launched the new site. While it finds unscoped variables that should be in the var scope (unscoped variables are placed in the variables scope by default), it doesn't find explicitly variables scoped variables that should be in the var scope.
We asked the help desk if there had been any errors reported for the Agent Reports section that didn't seem to make any sense. They had been getting calls from Agents saying that from time to time, the reports they selected didn't match the reports they would receive. If they went back and selected the list again, they would get what they requested. It happened so infequently that they never filed a ticket for it.
While the impact of this syntax error was pretty small, in some of our other applications, it could have been critical.
Had we not been using a tool like TeaLeaf, we couldn't have been so proactive in finding unreported errors.
Had we not been using StructInsert, and Struct Functions in general, I doubt we would have ever figured out why this was happening to our Agents.
Sometimes being verbose with your code can be worth much more than the few seconds you would save otherwise. I know some of you newer developers are thinking, "No. That's not true. That's impossible!"
Search your feelings, you know it to be true!