svschouw-bb · GitHub

Since the XMLHttpRequest event handler rewrite in HtmlUnit 2.44.0, there is a race condition between xhr.onreadystatechange and xhr.abort().

Suppose I have the following javascript:

var xhr = new XMLHttpRequest();
xhr.onreadystatechange = function() {
  if (xhr.readyState == XMLHttpRequest.DONE) {
    if (xhr.status == 200) {
      // handle success
      // ...
    }
  }
}
xhr.open('GET', '/someurl');
xhr.send();

And very quickly afterwards:

xhr.onreadystatechange = function() {};
xhr.abort();

The original onreadystatechange function is already queued, but is blocked on the page lock by the second javascript snippet. When it finally executes, the xhr.abort() method has removed the webResponse_ causing XMLHttpRequest.getStatus() to log the error XMLHttpRequest.status was retrieved without a response available (readyState: 4)

I'm not sure how normal browsers handle this exactly. Our JavaScript code can handle this case because xhr.status == 200 just returns false. I was mostly tripped by the error logging. From my side I would be ok with something like if (aborted) return 0 in XMLHttpRequest.getStatus(), but this might be hiding the bug instead of fixing it.

For now we can just ignore the error.

Read the original on github.com ↗