Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Javascript > Newbie Q: XMLHttpRequest.send() and race conditions?

Reply
Thread Tools

Newbie Q: XMLHttpRequest.send() and race conditions?

 
 
kj
Guest
Posts: n/a
 
      12-21-2006


Hi. I'm new to programming with XMLHttpRequest and I'm unclear
about how non-blocking requests are handled, and in particular
about the question of the potential for race conditions. Suppose
that I have a function like this one that gets called when, for
example, the user clicks some button X:

function handleButtonXClick() {
var request = makeRequest();
function handleResponse() {
if ( request.readyState == 4 && request.status == 200 ) {
// has its way with some Innocent Global Variables...
}
};
request.onreadystatechange = handleResponse;
var url = 'http://myurl.com/cgi/latestinfo';
request.open( 'GET', url ); // non-blocking request, asynchronous response
request.send();
}

My question is: if the user triggers two invocations of handleResponse
in rapid succession, could the second invocation begin before the
first one is finished? Or does the browser ensure that these two
invocations are executed sequentially?

Assuming that the browser allows both invocations to execute
concurrently, does JavaScript provide any mechanism for locking
shared resources, such as global variables?

(I know that I could design the UI to prevent concurrent requests,
e.g. by disabling button X until handleResponse finishes, but I
would like to avoid this if I can.)

Thanks!

kj

--
NOTE: In my address everything before the first period is backwards;
and the last period, and everything after it, should be discarded.
 
Reply With Quote
 
 
 
 
webEater
Guest
Posts: n/a
 
      12-21-2006

kj schrieb:

> Hi. I'm new to programming with XMLHttpRequest and I'm unclear
> about how non-blocking requests are handled, and in particular
> about the question of the potential for race conditions. Suppose
> that I have a function like this one that gets called when, for
> example, the user clicks some button X:
>
> function handleButtonXClick() {
> var request = makeRequest();
> function handleResponse() {
> if ( request.readyState == 4 && request.status == 200 ) {
> // has its way with some Innocent Global Variables...
> }
> };
> request.onreadystatechange = handleResponse;
> var url = 'http://myurl.com/cgi/latestinfo';
> request.open( 'GET', url ); // non-blocking request, asynchronous response
> request.send();
> }
>
> My question is: if the user triggers two invocations of handleResponse
> in rapid succession, could the second invocation begin before the
> first one is finished? Or does the browser ensure that these two
> invocations are executed sequentially?
>
> Assuming that the browser allows both invocations to execute
> concurrently, does JavaScript provide any mechanism for locking
> shared resources, such as global variables?
>
> (I know that I could design the UI to prevent concurrent requests,
> e.g. by disabling button X until handleResponse finishes, but I
> would like to avoid this if I can.)
>
> Thanks!
>
> kj
>
> --
> NOTE: In my address everything before the first period is backwards;
> and the last period, and everything after it, should be discarded.


What you do will not work, you must set the 3rd param for open method
in firefox, true means asynchronous, false means synchronous request,
so use true (the A in Ajax)

request.open( 'GET', url, true); // non-blocking request, asynchronous
response
request.send(null);

param in send method must be null.

Andi

 
Reply With Quote
 
 
 
 
kj
Guest
Posts: n/a
 
      12-21-2006
In <(E-Mail Removed) om> "webEater" <(E-Mail Removed)> writes:

>What you do will not work, you must set the 3rd param for open method
>in firefox, true means asynchronous, false means synchronous request,
>so use true (the A in Ajax)


>request.open( 'GET', url, true); // non-blocking request, asynchronous
>response
>request.send(null);


>param in send method must be null.


Thanks, but those details were not the point of my question. The
main question remains: can multiple invocations of the onreadystatechange
handler happen concurrently, or does the browser make sure that
they happen sequentially?

Thanks,

kj
--
NOTE: In my address everything before the first period is backwards;
and the last period, and everything after it, should be discarded.
 
Reply With Quote
 
Laurent Bugnion
Guest
Posts: n/a
 
      12-22-2006
Hi,

kj wrote:
> Hi. I'm new to programming with XMLHttpRequest and I'm unclear
> about how non-blocking requests are handled, and in particular
> about the question of the potential for race conditions. Suppose
> that I have a function like this one that gets called when, for
> example, the user clicks some button X:
>
> function handleButtonXClick() {
> var request = makeRequest();
> function handleResponse() {
> if ( request.readyState == 4 && request.status == 200 ) {
> // has its way with some Innocent Global Variables...
> }
> };
> request.onreadystatechange = handleResponse;
> var url = 'http://myurl.com/cgi/latestinfo';
> request.open( 'GET', url ); // non-blocking request, asynchronous response
> request.send();
> }
>
> My question is: if the user triggers two invocations of handleResponse
> in rapid succession, could the second invocation begin before the
> first one is finished? Or does the browser ensure that these two
> invocations are executed sequentially?


The invocation will be executed sequentially (JavaScript is single
threaded), but the browser doesn't check if a request is currently
active before sending a next one. That would be very inefficient, and
would lose all the benefits of asynchrnous requests.

If you need to make sure that a request ends before another is sent, you
can either use synchronous calls (but they are blocking, so not a good
idea), or use a "isRequestActive" variable, which prevents a new request
being sent as long as the response didn't arrive. Set the variable to
true before the "send" call, set it to false in "handleResponse" (also
in error cases).

Additionally, it might be a good idea to enclose your code in try/catch,
and set the varable to false in the catch (so that a possible error
doesn't prevent sending again.

One more thing: The current HTTP stack implementation prevents more than
2 connections being open concurrently from a give client to a given web
server. So you have a maximum of 2 concurrent XmlHttpRequests to a given
web server at any time (but that doesn't help you in your case, 2 being
too much anyway


> Assuming that the browser allows both invocations to execute
> concurrently, does JavaScript provide any mechanism for locking
> shared resources, such as global variables?


No, such mechanism (lock) doesn't exist in JavaScript, as JavaScript is
single threaded and doesn't need this. But requests sent asynchronously
are a special case, and need an implementation as described above.

> (I know that I could design the UI to prevent concurrent requests,
> e.g. by disabling button X until handleResponse finishes, but I
> would like to avoid this if I can.)


That would be a good practice anyway (but doesn't prevent you from
implementing a safeguard).

>
> Thanks!
>
> kj


HTH,
Laurent
--
Laurent Bugnion, GalaSoft
Software engineering: http://www.galasoft-LB.ch
PhotoAlbum: http://www.galasoft-LB.ch/pictures
Support children in Calcutta: http://www.calcutta-espoir.ch
 
Reply With Quote
 
webEater
Guest
Posts: n/a
 
      12-22-2006
> What you do will not work, you must set the 3rd param for open method
> in firefox, true means asynchronous, false means synchronous request,


I quote myself, I answered your question - this is what I wanted to
explain you, the third param makes the difference. Default is true.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to prevent from race conditions to share data between many process and thread in python mars Python 6 02-07-2007 08:52 AM
copyfile avoiding overwrites and race conditions manuelg@gmail.com Python 2 06-19-2006 06:29 PM
?? CONSISTENT CRIMES OF THE EVIL ANGLO-SAXON RACE AND RELIGION CHURCH MOLESTED CHILDS C Programming 0 05-07-2004 07:16 PM
DVD Verdict reviews: HOT WHEELS WORLD RACE, SOUTH PARK: THE COMPLETE THIRD SEASON, and more! DVD Verdict DVD Video 0 01-14-2004 10:08 AM
Mega Pixel race is like the Mhz Race Hugo Drax Digital Photography 7 01-12-2004 11:07 AM



Advertisments