-
Notifications
You must be signed in to change notification settings - Fork 24.4k
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
[Android] - XMLHttpRequest ontimeout not fired #11666
Comments
+1 |
+1 same problem. but use axios |
@sergop321 @OMegaTiger Do you resolve this problem? |
I encounter the problem while using axios but after some investigation I tracked the issue to react native XMLHttpRequest. I wasn't able to resolve the issue, but I did find out that it is not always happening, https://github.com/facebook/react-native/blob/master/Examples/UIExplorer/js/XHRExampleOnTimeOut.js this example with timeout on in android works (it do not work if you change the domain) I am not sure what exactly the issue it might be with localhost, or port or it might be with the way you holding the request on the server. @Lxxyx if you are using localhost try to make the request to a different domain it might help. |
@sergop321 thank you, i'll try it. |
Seems to be axios bug. When i set timeout to 10ms, timeout works for every request but when i set timeout to 3000ms or longer, timeout do not work. |
@sergop321 I resolve this problem by use axios cancelToken but not timeout (I can not resolve react-native bugs). Here is the code: const TIMEOUT = 5000
function generateRequestCancelToken () {
let cancelRequest
return async (config: Axios.AxiosXHRConfigBase<any>) => {
config.cancelToken = new axios.CancelToken((cancelExecutor) => {
cancelRequest = cancelExecutor
})
setTimeout(() => {
if (cancelRequest instanceof Function) {
cancelRequest('ECONNABORTED')
}
}, TIMEOUT)
return config
}
}
const requestCancelToken = generateRequestCancelToken()
axiosInstance.interceptors.request.use(requestCancelToken, null) When timeout, it will throw an error: |
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. |
I solved this problem with a setTimeout method. If the readyState is not DONE, call the request.abort() function.
|
Summary: In the NetworkingModule, if the http request failed, we send a `didCompleteNetworkResponse` event with the error message, which is used on JS side to determine if the request was erroring. Currently we get the error message from `e.getMessage()`, however, not all exceptions have a message and it might therefore return null and thus resulting in no error on JS side. This change checks if the message is null and if so uses a default message. In android send a request using XMLHttpRequest with a timeout set to a server that has a delay larger than the timeout (so we force the timeout to happen). ``` const request = new XMLHttpRequest(); request.open('GET', "http://localhost:3000/", true); request.timeout = 1000; request.ontimeout = () => { console.log('ontimeout'); }; request.send(); ``` See the timeout callback being called correctly. Fixes facebook#11666 Closes facebook#13407 Differential Revision: D4963764 Pulled By: hramos fbshipit-source-id: 61ffcef9e0594fe9bface24fdb8bde1e6eec3990
Summary: Fixes facebook#11666 (Which was incorrectly closed) Pull Request resolved: facebook#22164 Differential Revision: D13057001 Pulled By: hramos fbshipit-source-id: bcd53e893bc7c114f866e54938166b74b8ae0299
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.
The server sleeps for 3 seconds to make sure the timeout occurs.
will print on Android
on Ios
Additional Information
The text was updated successfully, but these errors were encountered: