Odd Error Involving CFThread, WriteDump(), Format Text, CFSaveContent And ColdFusion 10
Yesterday, I happened upon a really odd bug in ColdFusion 10. I point out ColdFusion 10 as this seems to work just fine in ColdFusion 11; so, if you're on 11, feel free to ignore. The error only occurs in a extremely specific context: Using the writeDump() method with format="text" inside a CFSaveContent block inside CFThread. And, it only occurs if you call the writeDump() method more than once in the same thread.
To see this in action, take a look at the following code:
<cfscript>
// Initiate a thread.
thread
name = "testThread"
action = "run"
{
// Capture the output of writeDump() in a buffer.
savecontent variable="thread.resultA" {
writeDump( var = "a", format = "text" );
}
// Capture the output of writeDump() in a buffer.
// --
// NOTE: We have to do this TWICE - it won't error if you only do it once.
savecontent variable="thread.resultB" {
writeDump( var = "b", format = "text" );
}
} // END: Thread.
// Wait for the threads to come back so we can see the output.
thread action = "join";
// Output our thread so we can see the Error object.
writeDump( cfthread.testThread );
</cfscript>
As you can see, I'm making two different calls to writeDump(), with format="text", inside a CFSaveContent output buffer with format="text". And, when we run the above code, we get the following thread error:
Attribute validation error.The tag requires the attribute var.
And, here's the entire Thread output:
As you can see, the "resultA" key made it safely into the output; this is because the error only occurs during the second invocation of writeDump().
This works fine if you don't use format="text". It also works fine if you're not inside a CFSaveContent block. It also works fine if you're not inside a CFThread context.
To fix this (for the code I was working on), I moved the writeDump() into a ColdFusion component. This fixed the issue, which I believe was some sort of confusion over local scoping. Super odd bug, though!
Want to use code from this post? Check out the license.
Reader Comments
@All,
I filed this bug. It only seems to affect ColdFusion 10, not 11.
@Ben,
I believe this is a bug i filed with adobe shortly after cf10 was released. It had to do with scope changing not working correctly inside a thread.
https://bugbase.adobe.com/index.cfm?event=bug&id=3521203
They addressed it by saying upgrade to CF11. (long after we filed the bug.)
The worst part was the way they refused to run cf from the command line to reproduce it in the way specified in the ticket and forced me to write a different reproduction script to get the bug to happen using cflog rather than cfdump.
BTW. the solution we went with in production was ridiculous.
file 1 "fixthread.cfm" (in the custom tag path) contents:
------------------------
<cf_fixthread2 />
<cf_fixthread2 />
------------------------
file 2 "fixthread2.cfm" is an empty file.
Then as the first line in any cfthread call is <cf_fixthread>
@Tom,
Super funky stuff! I tried to search the bug-base for things related to CFDump, but didn't see anything matching. But, yeah, it seems like it might be something related to scopes getting messed up.
I had to use a work-around like you - I moved the block of code into a totally different CFC. Then, I could call that from within the Thread and it seems to work properly. So weird!