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

Axios 0.15.2 doesn't reject promise if request is cancelled in Chrome #537

Closed
michaeluskov opened this issue Nov 16, 2016 · 6 comments

Comments

@michaeluskov
Copy link

commented Nov 16, 2016

I have axios@0.15.2

If this HTML is run, some requests will be cancelled by Chrome:

<html>
<head>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/axios/0.15.2/axios.js"></script>
</head>
    <script>
    var promises = [];
    window.onload = function() {
        for (var i = 0; i < 4; i++) {
            var promise = axios.get('some url which causes ERR_CONNECTION_CLOSED');
            promises.push(promise);
        }
    }
   </script>
</html>

Cancelled

But promises which turned into cancelled request won't be fulfilled:

notfulfilled

@mzabriskie

This comment has been minimized.

Copy link
Member

commented Dec 8, 2016

Can you share the code that is generating the cancelled requests?

@fvicente

This comment has been minimized.

Copy link

commented Feb 6, 2017

Hi @mzabriskie
I have exactly the same problem and I can reproduce it consistently. My app has a long pull ajax, if I put my laptop to sleep, when it comes back the ajax es cancelled by chrome but never completed/rejected the promise.

code is like this:

  const timeStamp = (new Date()).getTime();
  return axios.get(`${API_LISTEN_URL}?subject=POS${posId}&syncId=${syncId}&_ts=${timeStamp}`, {
    headers: defaultHeaders,
    responseType: 'document'
  })
  .then(response => {
    // we need to retrieve the new synchronization ID - this needs to be sent on the next request
    return { 'ok': true, 'syncId': response.headers["x-sync-id"], 'xml': response.data }
  }, error => {
    const message = "Error while listening for an event"
    console.log(message)
    return { 'ok': false }
  })

You have to put some sleep on your server side in order to reproduce the problem. Then when the ajax is pending, close your laptop monitor, after a few seconds lift and you will see that the request is cancelled, but the promise not resolved.
Note: I'm using chrome Version 55.0.2883.95 (64-bit) on a MacBook computer, MacOS X "El Capitán" version 10.11.6 (15G1217)

fvicente pushed a commit to fvicente/axios that referenced this issue Feb 7, 2017

fvicente
https://github.com/axios/issues/537
Fixes unhandled browser request cancellation
@fvicente

This comment has been minimized.

Copy link

commented Feb 7, 2017

Pull request created to fix this issue: #689

@mattridley

This comment has been minimized.

Copy link
Contributor

commented Mar 5, 2018

I'm encountering the same issue - given @fvicente is no longer working on a PR to resolve this does anyone have an objection if I pick the issue up and run with it?

mattridley pushed a commit to mattridley/axios that referenced this issue Mar 5, 2018

mattridley
@fvicente

This comment has been minimized.

Copy link

commented Mar 6, 2018

Hey @mattridley
I'm not saying is the best solution, but you can always monkey-patch the library, by putting in your code something like this:

  try {
    // monkey patch for "onAbort" not being handled by axios correctly, this is to avoid an
    // unresolved promise on the long-pull ajax if the computer goes to sleep
    // https://github.com/axios/axios/pull/689
    const oldSend = XMLHttpRequest.prototype.send
    XMLHttpRequest.prototype.send = function () {
      this.onabort = this.onerror
      oldSend.apply(this, arguments)
    }
  } catch (e) {
    console.error(e)
  }

nickuraltsev added a commit that referenced this issue Mar 7, 2018

Merge pull request #1399 from mattridley/master
Fixing #537 Rejecting promise if request is cancelled by the browser
@nickuraltsev

This comment has been minimized.

Copy link
Member

commented Mar 7, 2018

Fixed via #1399

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
5 participants
You can’t perform that action at this time.