Skip to content

Race condition between XMLHttpRequest.onreadystatechange and .abort() #365

Description

@svschouw-bb

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.

No activity

Activity on this issue will appear here.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions