title: ColdFusion VARIABLES Race Condition?
body: <p>I would like some help identifying why this particular code, in rare circumstances, produces a race condition.  I've found a fix, which I'll outline as well, but I really want to understand it.</p>
  
  <p>We have a CMS based system comprised of many modules loosely based off a fusebox model.  Everything runs through a single index.cfm.</p>
  
  <p>In our Index.cfm we are creating a couple instances of Components, some are bsaed on the APPLICATION.PortalApp instance created in Application.cfc.  I'm not including that code because it's not entirely relevant:</p>
  
  <pre><code>&lt;cfset REQUEST.ActionHandler = CreateObject("Component", "Components.ActionHandler").init(APPLICATION.PortalApp.Config) /&gt;
  &lt;cfset VARIABLES.Modules = CreateObject("Component", "Components.Modules").init(APPLICATION.PortalApp.Config, REQUEST.ActionHandler.GetModuleList(), REQUEST.ActionHandler.GetSuppressOutput(), REQUEST.ActionHandler.GetRoleList(), REQUEST.ActionHandler.GetAccessList(), REQUEST.ActionHandler.GetMasterRoleList()) /&gt;
  </code></pre>
  
  <p>After we instantiate these objects, we get the content for the modules on the page (based on their 'pane': top, left, middle, right) by calling a PageManager component that's instantiated as part of the application, Application.PortalApp.</p>
  
  <pre><code>&lt;cfsavecontent variable="Variables.Portal_Content.Top"&gt;&lt;cfset APPLICATION.PortalApp.PageManager.DisplayContent( SESSION, REQUEST.ActionHandler, VARIABLES.Modules, 0  ) /&gt;&lt;/cfsavecontent&gt;
  &lt;cfsavecontent variable="Variables.Portal_Content.Left"&gt;&lt;cfset APPLICATION.PortalApp.PageManager.DisplayContent( SESSION, REQUEST.ActionHandler, VARIABLES.Modules, 1  ) /&gt;&lt;/cfsavecontent&gt;
  &lt;cfsavecontent variable="Variables.Portal_Content.Middle"&gt;&lt;cfset APPLICATION.PortalApp.PageManager.DisplayContent( SESSION, REQUEST.ActionHandler, VARIABLES.Modules, 2  ) /&gt;&lt;/cfsavecontent&gt;
  &lt;cfsavecontent variable="Variables.Portal_Content.Right"&gt;&lt;cfset APPLICATION.PortalApp.PageManager.DisplayContent( SESSION, REQUEST.ActionHandler, VARIABLES.Modules, 3  ) /&gt;&lt;/cfsavecontent&gt;
  </code></pre>
  
  <p>PageManager.DisplayContent basically loops over the modules and wraps them in a wrapper.  However, at some points, there is a race condition and the function craters and displays no module at all.  It seems to be based on VARIABLES.Modules becoming corrupt but that's in the VARIABLES scope which is not shared.</p>
  
  <p>To fix it, we changed the code to the following:</p>
  
  <pre><code>&lt;!--- If we do not use VARIABLES scope and create a ContentManager, race condition can cause empty modules ---&gt;
  &lt;cfset VARIABLES.CurrPageMgr = CreateObject("Component", "Components.ContentManager").init() /&gt;
  
  &lt;cfsavecontent variable="Variables.Portal_Content.Top"&gt;&lt;cfset VARIABLES.CurrPageMgr.DisplayContent( SESSION, REQUEST.ActionHandler, VARIABLES.Modules, 0  ) /&gt;&lt;/cfsavecontent&gt;
  &lt;cfsavecontent variable="Variables.Portal_Content.Left"&gt;&lt;cfset VARIABLES.CurrPageMgr.DisplayContent( SESSION, REQUEST.ActionHandler, VARIABLES.Modules, 1  ) /&gt;&lt;/cfsavecontent&gt;
  &lt;cfsavecontent variable="Variables.Portal_Content.Middle"&gt;&lt;cfset VARIABLES.CurrPageMgr.DisplayContent( SESSION, REQUEST.ActionHandler, VARIABLES.Modules, 2  ) /&gt;&lt;/cfsavecontent&gt;
  &lt;cfsavecontent variable="Variables.Portal_Content.Right"&gt;&lt;cfset VARIABLES.CurrPageMgr.DisplayContent( SESSION, REQUEST.ActionHandler, VARIABLES.Modules, 3  ) /&gt;&lt;/cfsavecontent&gt;
  </code></pre>
  
  <p>The DisplayContent of ContentManager is the exact same function text as the PageManager.DisplayContent with the exception on ContentManager existing only in VARIABLES scope and PageManager existing as part of the APPLICATION.</p>
  
  <p>This was very hard to reproduce after getting reports of it.  I essentially started a jmeter session hammering the Development server as hard as possible with a breakpoint set based on a condition I knew would fire with VARIABLES.Module got corrupt.  That was the only way to reproduce it.</p>
  
  <p>I'm also not 100% sure this fix works, but so far jmeter has not fired the condition with it in place.</p>
  
  <p>Edit: Per Request, DisplayContent function:</p>
  
  <pre><code>&lt;cffunction name="displayContent" access="public" output="true"&gt;
      &lt;cfargument name="SessionData" required="yes" type="Struct" /&gt;
      &lt;cfargument name="ActionHandler" required="yes" type="ActionHandler" /&gt;
      &lt;cfargument name="Modules" required="yes" type="Modules" /&gt;
      &lt;cfargument name="Pane" required="yes" type="numeric" /&gt;
      &lt;cfswitch expression="#Arguments.Pane#"&gt;
          &lt;cfcase value="1"&gt;&lt;cfset Variables.blnPane = ARGUMENTS.Modules.getLeft()&gt;&lt;/cfcase&gt;
          &lt;cfcase value="2"&gt;&lt;cfset Variables.blnPane = ARGUMENTS.Modules.getCenter()&gt;&lt;/cfcase&gt;
          &lt;cfcase value="3"&gt;&lt;cfset Variables.blnPane = ARGUMENTS.Modules.getRight()&gt;&lt;/cfcase&gt;
          &lt;cfdefaultcase&gt;&lt;cfset Variables.blnPane = ARGUMENTS.Modules.getTop()&gt;&lt;/cfdefaultcase&gt;
      &lt;/cfswitch&gt;
      &lt;cfif VARIABLES.blnPane&gt;
          &lt;cfset VARIABLES.qryPaneModules = ARGUMENTS.Modules.GetModulesInPane(Arguments.Pane)&gt;
          &lt;cfset VARIABLES.aryModulesInPane = ArrayNew(1)&gt;
          &lt;cfloop query="VARIABLES.qryPaneModules"&gt;
              &lt;cfset VARIABLES.blnResult = ArrayAppend(aryModulesInPane,VARIABLES.qryPaneModules.MOD_SYS_NR)&gt;
          &lt;/cfloop&gt;
          &lt;cfset VARIABLES.Template = "../CustomTags/Portalv#ARGUMENTS.SessionData.intPortalVersion#/DisplayModuleAlternate.cfm"&gt;
          &lt;cfif Arguments.ActionHandler.GetDocumentType() EQ 3&gt;
              &lt;cfset VARIABLES.Template = "../CustomTags/Portalv#ARGUMENTS.SessionData.intPortalVersion#/DisplayXMLModule.cfm"&gt;
          &lt;/cfif&gt;
          &lt;cfif VARIABLES.qryPaneModules.recordcount GT 0&gt;
              &lt;cfloop index="VARIABLES.modLoop" from="1" to="#ArrayLen(VARIABLES.aryModulesInPane)#"&gt;
                  &lt;cfparam name="VARIABLES.aryModulesInPane[VARIABLES.modLoop]" default="0"&gt;
                  &lt;cfset VARIABLES.objModuleInfo = ARGUMENTS.Modules.GetModuleInfo( VARIABLES.aryModulesInPane[VARIABLES.modLoop], ARGUMENTS.Modules.GetModules() ) /&gt;
                  &lt;cfif NOT IsNumeric(VARIABLES.objModuleInfo.intModuleID)&gt;
                      &lt;cfset VARIABLES.objModuleInfo.intModuleID = 0 &gt;
                  &lt;/cfif&gt;
                  &lt;cfmodule template="#VARIABLES.Template#"
                      ModuleID="#VARIABLES.objModuleInfo.intModuleID#"
                      ModuleName="#VARIABLES.objModuleInfo.strModuleName#"
                      SecurityLevel="#VARIABLES.objModuleInfo.intRoleTypeID#"
                      ModuleDSN="#VARIABLES.objModuleInfo.strModDBDSN#"
                      ModuleUserName="#VARIABLES.objModuleInfo.strModDBUserID#"
                      ModulePassword="#VARIABLES.objModuleInfo.strModDBPassword#"
                      AlternateFunctionID="#VARIABLES.objModuleInfo.intAlternateFunctionID#"
                      AlternateFunctionName="#VARIABLES.objModuleInfo.strAlternateFunctionName#"
                      InstructionFileID="#VARIABLES.objModuleInfo.intManualID#"
                      ModuleOps="#VARIABLES.objModuleInfo.blnModuleOps#"
                      ModuleSource="#VARIABLES.objModuleInfo.strModuleSource#"
                      ItemID="#VARIABLES.objModuleInfo.intModItemID#"
                      AutoLoginID="#VARIABLES.objModuleInfo.intAutoLoginCategoryID#"
                      IPS_objPortalSessionData="#ARGUMENTS.SessionData#"
                      ModuleList="#ARGUMENTS.ActionHandler.GetModuleList_SingleRecord(VARIABLES.objModuleInfo.intModuleID)#"
                      ModulesComponent="#ARGUMENTS..Modules#"
                      ControlHeaderIR = "#VARIABLES.objModuleInfo.blnControlHeaderIR#"
                      BorderIR = "#VARIABLES.objModuleInfo.blnBorderIR#"
                      IPS_strPortalRoot = "#VARIABLES.IPS_strPortalRoot#"
                      IPS_strPortalURL = "#VARIABLES.IPS_strPortalURL#"
                      Wrapper = "#Arguments.ActionHandler.getWrapper()#"
                      Definition = "#VARIABLES.objModuleInfo.strModuleDef#"
                      Category = "#VARIABLES.objModuleInfo.strModuleCat#"
                      aryModulesInPane = "#VARIABLES.aryModulesInPane#"
                      blnLockIR = "#VARIABLES.objModuleInfo.blnLockIR#"
                      strMessageTE = "#VARIABLES.objModuleInfo.strMessageTE#" &gt;
  
              &lt;/cfloop&gt;
          &lt;/cfif&gt;
      &lt;/cfif&gt;
  &lt;/cffunction&gt;
  </code></pre>