Menu

#360 asynchronous XMLHttpRequest handler runs in separate thread

1.8
closed
None
5
2012-10-21
2006-06-19
Kris Zyp
No

When an asynchronous XMLHttpRequest call is made the
http connection is handled asynchonously and in modern
browsers, the http connection processing may be
handled by a separate thread, however the JavaScript
response callback handler that is registed for the XHR
should not run in a separate thread. Modern browsers
maintain a strict single threaded model for
JavaScript. This is critical as JavaScript functions
are generally not threadsafe (and it is not needed)
because they are assured of always running being run
within a single thread. If the response handler for
an XMLHttpRequest is handled in a separate thread,
there can possibly be two threads of JavaScript
running simulataneously and this introduces numerous
possible problems with JavaScript functions that are
not threadsafe having unexpected behavior. HTMLUnit
seems to execute the handler in a separate thread and
therefore these JavaScript threading problems can
arise. The correct behavior for an XMLHttpRequest is
that when a response callback is appropriate, HTMLUnit
should wait for a rest point in the execution of the
single JavaScript thread, and then issue the callback
handler in that single thread when it becomes
available to execute the handler. The actual
underlying http connection can be handled in a
separate thread, but no JavaScript execution should be
in a separate thread.

This single thread behavior should also be observed
for the setTimeout function. The callback on the
setTimeout should wait for a break in the main
JavaScript thread and then execute the callback
function in that thread.

Discussion

  • Kris Zyp

    Kris Zyp - 2006-06-19

    Logged In: YES
    user_id=1542119

    The attached file demonstrates a AJAX request that can be
    preempted improperly.

     
  • Marc Guillemot

    Marc Guillemot - 2006-06-20

    Logged In: YES
    user_id=402164

    No file currently attached. Have you forgotten to check this
    stupid small checkbox?

     
  • Kris Zyp

    Kris Zyp - 2006-06-22

    This makes an AJAX request. If the AJAX request takes a litte time to return and you can see the described behavior

     
  • Marc Guillemot

    Marc Guillemot - 2006-12-28

    Logged In: YES
    user_id=402164
    Originator: NO

    Now fixed in SVN (would surely be cleaner to use a JavaScript execution Thread per HtmlPage, but in a first time current hack allows the problem exposed here to be fixed)

     

Log in to post a comment.