Monday 11 February 2013

Thread longevity weirdness

G'day:
Man, I've been slack recently. Sorry: sickness, my "actual real life" getting in the way of sitting in front of the computer all the time, and general malaise have been pre-occupying me. I'm sure y'all been coping without a blog article to read. I have actually been beavering away with a coupla articles, but it's all just investigation at the moment, and I've nothing written down yet.

Here's an interesting thing I encountered on Friday, and just got a chance to write some test code now.  Consider this code:


// MyComponent.cfc
component {

    public void function doThread(){
        cfthread.message = "";
        thread name="t1" {
            cfthread.message &= "Hello world from T1<br />";
        }
        thread action="join" name="t1";
        writeOutput(cfthread.message);
    }

}

// test.cfm
myObj = new MyComponent();

myObj.doThread();

This is all pretty obvious: in a very contrived situation I use a thread to write a message. And it dutifully outputs:

Hello world from T1

Not interesting. But what I was actually doing was along these lines:

// test.cfm
myObj = new MyComponent();

myObj.doThread();
myObj.doThread();

Note that I'm calling the method twice. And I get this output:

Hello world from T1
The web site you are accessing has experienced an unexpected error.
Please contact the website administrator. 


The following information is meant for the website developer for debugging purposes.
Error Occurred While Processing Request

Attribute validation error for the cfthread tag.

Thread with name T1 could not be created. Thread names must be unique within a page.
The error occurred in D:\websites\www.scribble.local\blog\thread\MyComponent.cfc: line 6
4 :  public void function doThread(){
5 :   cfthread.message = "";
6 :   thread name="t1" {
7 :    cfthread.message &= "Hello world from T1<br />";
8 :   }


Now, I can see that if I had two threads running concurrently I'd not be able to give them the same name, but I really don't see why after a thread has completed one should not be able to reuse the name. Initially I thought this was just bad coding on the part of CF, but I tried it on Railo, and got the same thing. So I am guessing there's more to this than meets the eye, and I'll ask the Railo guys if they can explain.

I note that Andy Scott had raised a bug with Adobe about this, and Adobe helpfully closed it as "user error", and without any explanation as to why it's user error, rather than someone should quite legitimately expect to be able to do this. This is typically slack of their approach to addressing bugs. And is also why I'll ask the Railo guys about it rather than Adobe, because I know I'll actually get help from Railo.

When I get a response from the Railo bods, I'll post it here.

And that's it!

Righto.

--
Adam