5

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?

Eric Belair
  • 10,574
  • 13
  • 75
  • 116
  • 3
    Not sure what your problem is, but you should really be init-ing your session variables in `onSessionStart()`, not `onRequestStart()`. – Adam Cameron Jan 15 '14 at 17:18
  • Maybe some users are working in your site with multiple tabs/windows, maybe one tab is doing something, but then they sign-out in another, thus destroying your session vars. Or maybe after they sign-out in one tab they go back to a previous one and attempt some kind of action. In my CF app if a user signs-out all other tabs are forced to sign out as well and return to the sign-in page. – gfrobenius Jan 15 '14 at 22:16

2 Answers2

7

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.

Leigh
  • 28,765
  • 10
  • 55
  • 103
Sean Corfield
  • 6,297
  • 22
  • 31
  • so simply adding "i = 1;" before the for loop will make this piece of code thread safe? – Eric Belair Jan 15 '14 at 21:34
  • No, you either need to do ' var i="" ' or ' local.i="" ' before the loop to make the "i" variable local to the function and thus thread safe. – Carl Von Stetten Jan 15 '14 at 22:09
  • 2
    Since you are on CF8 you have to use `var i = `. The local scope was introduced in CF 9 – Sean Coyne Jan 15 '14 at 22:55
  • It's not a function, it's a piece of cfscript in a CFM template - the default template in the root directory, as a matter of fact. So, not scoping a simple index will cause my SESSION to clear? – Eric Belair Jan 17 '14 at 01:41
  • ALso, I would never reference or use SESSION variables in a function - bad practice.... – Eric Belair Jan 17 '14 at 01:46
  • 1
    How is that CFM template executed? Is it as part of a CFC? Just a plain CFM template invoked directly via a URL? Is it possible that multiple requests are running in the same session and one of those requests is deleting the user from session scope? – Sean Corfield Feb 11 '14 at 23:44
1

I'd put this line "SESSION.User = CreateObject("component", "cfc.User");" into onSessionStart() then it will run when each users session is first initiated.

WilGeno
  • 185
  • 8
  • How will that solve my problem of a SESSION (or a property of the SESSION) disappearing in the middle of a template loading? – Eric Belair Jan 17 '14 at 01:39