axios: Requests with timeout still hand indefinitely

I have w project where I make tens of thousands of requests to a backend service, scraping some API. I don’t control the backend. To make the process fast, I post the requests in chunks, in parallel.

Sometimes, though, the backend just hangs indefinitely on a request. For that I’d like to have a timeout and just resend new request. Here is the code that I use for that purpose (I’m using the ES7 async/await functionality):

while(true) {
    try {
        let response = await axios.get(url, {timeout: timeout});
        return response.data;
    } catch (e) {
        if (!silent) {
            console.log(`Exception caught: ${JSON.stringify(e)}`);
        }
    }
}

The problem is, even with the timeout settings some of the requests above hang indefinitely - by average 1 per 3000.

To check if the problem is with my code, or with the library I’ve implemented similar functionality using superagent:

while(true) {
    try {
        let promise = new Promise((resolve, reject) => {
            superagent.get(url).timeout(timeout).end((error, response) => {
                if (error) {
                    reject(error);
                } else {
                    resolve(response);
                }
            })
        });
        let response = await promise;
        return response.body;
    } catch (e) {
        if (!silent) {
            console.log(`Exception caught: ${JSON.stringify(e)}`);
        }
    }
}

And the above code works as expected.

About this issue

  • Original URL
  • State: closed
  • Created 9 years ago
  • Comments: 16 (3 by maintainers)

Most upvoted comments

any update on this issue? I have the same problem in some of the android devices (React Native), Timeout doesn’t happen at all.

@mzabriskie This is still happening at least in React-Native

axios.post(`/create/`, Qs.stringify(payload),{timeout: 1000})
    .then(json => {}).catch(e => {}).

My request is sent, the result is there before timeout runs out and the .then() is not called if I add the timeout parameter.

@amithgc still having this issue. running on android too(emulator)

It’s still an issue in 2020.