Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ensure callback not called when request aborted #153

Merged

Conversation

stuartsan
Copy link
Contributor

When the xhr is aborted programmatically, the callback should not be called, but it still is. This patch prevents that.

Related to #93, #104

Copy link
Owner

@naugtur naugtur left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Am I right that if you abort asynchronously you will get the callback again?

@stuartsan
Copy link
Contributor Author

@naugtur, do you mean, something like this?

test("aborting XHR prevents callback from being called", { timeout: 500 }, function(assert) {
    var req = xhr({ uri: "/mock/200ok" }, function(err, response) {
        assert.fail('this callback should not be called');
    });

    setTimeout(function() {
      req.abort();
      assert.end()
    }, 0)
})

This test still passes. I think this would depend entirely upon how long the server response takes and when you call req.abort(). If you abort the request before it has completed (maybe with an error, maybe not), this approach should lead to callback prevention.

I believe the core issue was that the onreadystatechange handler is called before the onabort handler in the case of an abort, so this patch flips the order of these, so the onabort handler goes first and gets a chance to say "no do not call the callback".

Does this answer your q?

@naugtur
Copy link
Owner

naugtur commented Jan 20, 2017

Yes, that's it. Would you mind adding that as a test too?

@stuartsan
Copy link
Contributor Author

Yep no problem, added.

@stuartsan
Copy link
Contributor Author

Hmmm, I'm not sure why that failed in CI, I'm able to see it pass consistently locally. It might have to do with the mock server response time being faster in CI than on my machine. I'll have to look into this some more.

@stuartsan
Copy link
Contributor Author

I wonder if there is a way to delay the mock server's response by a fixed amount, say 50ms. Otherwise I'm not sure this second test, the async abort, is very helpful, because it might sometimes pass and sometimes fail, depending on the environment.

@stuartsan
Copy link
Contributor Author

I had not looked at the mock server...so I swapped in the "timeout" endpoint to simulate a more realistic server response time rather than immediate, like the 200 ok endpoint. This works!

@naugtur naugtur merged commit 9f60a89 into naugtur:master Feb 3, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants