3

I am trying lock block on page test.cfm and below is code written on page.

<cfscript>
writeOutput("Before lock at #now()#");
lock name="threadlock" timeout="3" type="exclusive"
{
    writeOutput("<br/>started at #now()#");
    thread action="sleep"  duration="10000";
    writeOutput("<br/>ended at #now()#");
}
writeOutput("<br/>After lock at #now()#");
</cfscript>

assuming my url for page is http://localhost.local/test.cfm and running it on browser in two different tabs. I was expecting one of the url will throw timeout error after 3 second since another url lock it atleast for 10 seconds due to thread sleep. Surprisingly I do not get any timeout error rather second page call run after 10 seconds as first call finish execution.

But I am appending some url parameter (e.g. http://localhost.local/test.cfm?q=1) will throw error. Also I am calling same url in different browser then one of the call will throw timeout issue.

Is lock based on session and url?

Update Here is output for two different cases:

Case 1:

TAB1 Url: http://localhost.local/test/test.cfm

 Before lock at {ts '2013-10-18 09:21:35'} 
 started at {ts '2013-10-18 09:21:35'} 
 ended at {ts '2013-10-18 09:21:45'} 
 After lock at {ts '2013-10-18 09:21:45'}

TAB2 Url: http://localhost.local/test/test.cfm

Before lock at {ts '2013-10-18 09:21:45'} 
started at {ts '2013-10-18 09:21:45'}
ended at {ts '2013-10-18 09:21:55'} 
After lock at {ts '2013-10-18 09:21:55'}

Case 2:

TAB1 Url: http://localhost.local/test/test.cfm

 Before lock at {ts '2013-10-18 09:27:18'} 
 started at {ts '2013-10-18 09:27:18'} 
 ended at {ts '2013-10-18 09:27:28'} 
 After lock at {ts '2013-10-18 09:27:28'}

TAB2 Url: http://localhost.local/test/test.cfm? (Added ? at the end)

Before lock at {ts '2013-10-18 09:27:20'} 
A timeout occurred while attempting to lock threadlock.
The error occurred in C:/inetpub/wwwroot/test/test.cfm: line 13
11 : 
12 : <cfoutput>Before lock at #now()#</cfoutput>
13 : <cflock name="threadlock" timeout="3" type="exclusive">
14 :    <cfoutput><br/>started at #now()#</cfoutput>    
15 :    <cfthread action="sleep"  duration="10000"/> 
...

Result for case 2 as expected.

For case 1, strange thing I just noticed is tab 2 output "Before lock at {ts '2013-10-18 09:21:45'} indicates that whole request start after 10 seconds (means after the complete execution of first tab) when I have fired it in second URL just after 2 seconds of first tabs.

4

1 回答 1

2

I have seen this behavior in Chrome before. If one tab is executing a request to a given URL the other tabs will wait for the first tab to finish and then immediately return.

Here is a proof of concept which I am able to replicate in my Chrome browser consistently.

foo.cfm

<cfoutput>#now()#</cfoutput>
<cfscript>
    sleep(5000);
</cfscript>
<cfoutput>#now()#</cfoutput>

Open two tabs in your browser to foo.cfm.

Test1: Do this next sequence as fast you can without errors. Click tab1, select the URL bar, hit the enter key. Click tab2, select the URL bar, hit the enter key.

When doing that you'll notice that the time stamps in the first page are 5 seconds apart, and you'll notice the timestamp on the second page did not even begin until the first had finished.

Test2: Do this next sequence as fast you can without errors. Click tab1, hit ctrl+f5, click tab2, hit ctrl+f5.

Now, notice that tab two will finish nearly right after tab1 and the timestamps indicate that processing took place equal to the duration it took you to switch tabs.

The same behavior occurs if you use f5 as well. So basically it's a micro-optimization of the Chrome browser (and likely others) to not make additional http requests for resources which other tabs are already working on. My supposition, which I have not tested yet, is if the resource were to return proper browser caching HTTP headers, then the second request would use the first requests result.

于 2013-10-18T21:27:13.530 回答