question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[Android] - XMLHttpRequest ontimeout not fired

See original GitHub issue

Timeouted XMLHttpRequest request do not call the timeout callback on android. The request is being aborted (the onreadystatechange or onerror never get called after) while in IOS the timeout event getting called as expected.

const request =  new XMLHttpRequest();

request.open('GET', "http://localhost:3333/", true);
request.timeout = 1000;
request.onabort = () => { console.log('abort', request); };
request.onerror = () => { console.log('error', request); };
request.onload = () => { console.log('load', request); };
request.onloadend = () => { console.log('loadend', request); };
request.onreadystatechange = () => { console.log('onreadystatechange', request); };
request.ontimeout = () => { console.log('ontimeout', request); };
request.send();

The server sleeps for 3 seconds to make sure the timeout occurs.

will print on Android

onreadystatechange XMLHttpRequest {UNSENT: 0, OPENED: 1, HEADERS_RECEIVED: 2, LOADING: 3, DONE: 4…}
load XMLHttpRequest {UNSENT: 0, OPENED: 1, HEADERS_RECEIVED: 2, LOADING: 3, DONE: 4…}
loadend XMLHttpRequest {UNSENT: 0, OPENED: 1, HEADERS_RECEIVED: 2, LOADING: 3, DONE: 4…}

on Ios

onreadystatechange XMLHttpRequest {UNSENT: 0, OPENED: 1, HEADERS_RECEIVED: 2, LOADING: 3, DONE: 4…}
ontimeout XMLHttpRequest {UNSENT: 0, OPENED: 1, HEADERS_RECEIVED: 2, LOADING: 3, DONE: 4…}
loadend XMLHttpRequest {UNSENT: 0, OPENED: 1, HEADERS_RECEIVED: 2, LOADING: 3, DONE: 4…}

Additional Information

  • React Native version: 0.39.2
  • Platform: Android
  • Operating System: MacOS

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Reactions:1
  • Comments:9 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
vmattcommented, May 2, 2017

I solved this problem with a setTimeout method. If the readyState is not DONE, call the request.abort() function.

setTimeout(() => {
    if (request.readyState !== XMLHttpRequest.DONE) {
        request.abort();
        callback("timeouterror");
    }
}, parseFloat(this.state.timeout));
0reactions
sergop321commented, Feb 12, 2017

Thanks @Lxxyx This is a specific workaround using axios but it probably should have a fix in the android code. this bug will persistent for different libraries as well.

@Lxxyx In the gist provided you always overriding the cancelToken, that mean you won’t be able to cancel requests that are not timeouted (Which is probably one of the reasons you are using axios instead of fetch in the first place), you can solve this by creating a wrapper around every request that will generate the cancel token and pass it (Not using the axios interceptors) or by passing cancelExecuter inside the config object and before generating a new cancel token, check if one already exsists.

Read more comments on GitHub >

github_iconTop Results From Across the Web

XMLHttpRequest.timeout - Web APIs - MDN Web Docs
When a timeout happens, a timeout event is fired. Note: You may not use a timeout for synchronous requests with an owning window....
Read more >
Timeout XMLHttpRequest - Stack Overflow
I want it to display text as "Timed Out". var bustcachevar = 1 //bust potential caching of external pages after initial request? (1=yes,...
Read more >
XMLHttpRequest timeout ignored - Fuse Community
Just noticed that XMLHttpRequest is ignoring the timeout property on Fuse, I'm running version 1.0.1 (build 13566) on Windows 10 here.
Read more >
XMLHttpRequest.timeout - Web APIs
The XMLHttpRequest.timeout property is an unsigned long representing the number of milliseconds a request can take before automatically being terminated.
Read more >
XMLHttpRequest timeout - Chrome Platform Status
Exposing the XHR timeout property and sending corresponding events ... Status: Specification currently under development in a Working Group ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found