Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is a ColdFusion SESSION variable "undefined" after being referenced a few lines before?

Running ColdFusion 8.01 Standard on Windows2003/IIS6

Application.cfc:

<cfcomponent output="false">
    <cfscript>
        THIS.SessionManagement = "Yes";
        THIS.SessionTimeout = CreateTimeSpan(0, 3, 0, 0);
        THIS.ApplicationTimeout = CreateTimeSpan(0, 8, 0, 0);
    </cfscript>

    <cffunction name="onRequestStart" returnType="Boolean" output="false">
        <cfargument name="targetPage" type="string" required="true">

        <cfscript>
            if  (!StructKeyExists(SESSION, "User"))
                SESSION.User = CreateObject("component", "cfc.User");
        </cfscript>
    </cffunction>
</cfcomponent>

Template file Pseudo-Code Sample:

    LOCAL.qItems =
        CreateObject(
                "component",
                "cfc.Items"
                ).setUser(SESSION.User).getItems();

    for (i=1; i<=LOCAL.qItems.RECORDCOUNT; i++) {
        LOCAL.Item =
            CreateObject(
                "component",
                "cfc.Item"
                ).setUser(
                    SESSION.User
                    ).setId(LOCAL.qItems["Sku"][i]);
    }

SESSION.User is set (if not already defined) in onRequestStart() of Application.cfc. The above code runs in a template file. The second reference to SESSION.User has thrown an exception with the message Element USER is undefined in SESSION.

Why would SESSION.User be defined (doesn't throw an exception) a few lines before, and then throw this exception a few lines later (within milliseconds)?

This happens maybe once a day in different templates throughout my application.

How can I prevent this?

like image 854
Eric Belair Avatar asked Jan 15 '14 16:01

Eric Belair


1 Answers

It's most likely a thread safety issue with something else in your code clearing out session scope or assigning NULL to SESSION.User.

I suggest that because you don't seem to have a local declaration for i in your loop, so that code is not thread safe - and so you may have similar errors elsewhere in your code.

like image 181
Sean Corfield Avatar answered Sep 23 '22 01:09

Sean Corfield