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 will not timeout on idle sockets #2143

Closed
justinmeiners opened this issue May 10, 2019 · 9 comments
Closed

Axios will not timeout on idle sockets #2143

justinmeiners opened this issue May 10, 2019 · 9 comments

Comments

@justinmeiners
Copy link

Describe the bug

On nodejs the timeout mechanism does not work correctly.
It is only concerned with the time to receive the first byte.
If the server sends a few bytes across the socket, but leaves it open
the request will never timeout.

This only happens with node, not in the browser.

To Reproduce

  1. Create a web server which writes a few bytes and then stops :
const http = require('http');

const hostname = '127.0.0.1';
const port = 3000;

const server = http.createServer((req, res) => {
    res.statusCode = 200;
    res.setHeader('Content-Type', 'text/plain');

    res.write('h');
//    res.end();
});

server.listen(port, hostname, () => {
    console.log(`Server running at http://${hostname}:${port}/`);
});
  1. Make a get request to the server with axios, with a timeout setting.
const axios = require('axios');

axios.get('http://localhost:3000', { timeout: 1000 })
.then(() => console.log('hello there'));
  1. The request will never timeout.

Expected behavior
If a socket stops writing for long period of time, the request should timeout.

nodejs get requests can detect this. You need to use their timeout
event instead of just using a timer.

https://nodejs.org/api/http.html#http_event_timeout

Environment:

  • Axios Version [e.g. 0.18.0]
  • OS: Linux (Ubuntu 18)

Looking at the code, I believe this is independent of environment.

@justinmeiners justinmeiners changed the title Axios will not timeout on an idle sockets in node Axios will not timeout on idle sockets in node May 10, 2019
@justinmeiners justinmeiners changed the title Axios will not timeout on idle sockets in node Axios will not timeout on idle sockets May 10, 2019
@aarcangeli
Copy link

I agree, an infinite fetching time can be problematic, especially if you expect a small response.

My workaround is to use a cancel token.

const CancelToken = axios.CancelToken;
const source = CancelToken.source();
let timeout = setTimeout(() => source.cancel('Timeout'), 5000);

axios.get('http://localhost:3000', { timeout: 1000, cancelToken: source.token })
    .then(() => { clearTimeout(timeout); console.log('hello there') });

@justinmeiners
Copy link
Author

Yes, I workaround it by just using the http module in node. Since it has support for proper timeout, I imagine adding it to axios would be easy. I feel this is especially important because it can have drastic consequences for the client.

@ryouaki
Copy link
Contributor

ryouaki commented Jul 3, 2019

maybe it looks like #1752

@Alanscut
Copy link
Collaborator

this issue has been fixed in v0.19.0, please try it.

@justinmeiners
Copy link
Author

justinmeiners commented Dec 11, 2019

@Alanscut I am no longer working on the project using the library. If it is fixed I will close the issue.
Do you have a link to the commit or release notes?

@joquijada
Copy link

joquijada commented Mar 14, 2021

I agree, an infinite fetching time can be problematic, especially if you expect a small response.

My workaround is to use a cancel token.

const CancelToken = axios.CancelToken;
const source = CancelToken.source();
let timeout = setTimeout(() => source.cancel('Timeout'), 5000);

axios.get('http://localhost:3000', { timeout: 1000, cancelToken: source.token })
    .then(() => { clearTimeout(timeout); console.log('hello there') });

This workaround worked for me, I'm on Axios 0.21.1. This has wreaked havoc in my production app. We were on Axios 0.19.x without any issues. Only after we upgraded to Axios 0.21.1 did troubles begin. It took me 3 days to isolate where the file descriptor and memory leak were coming from.

@justinmeiners
Copy link
Author

@joquijada your fix requires the entire request to occur within that time. I believe the nice thing about node is it considers the time between bytes.

@jasonsaayman
Copy link
Member

This has been fixed with #3694

@jesse23
Copy link

jesse23 commented Jan 19, 2022

+2 cents to this issue:

Test env: macOS Monterey
Node version: v16.13.1

0.18.0 -> not working for test case at the top even with workaround.
0.21.4 -> works fine even without workaround

NullSoldier added a commit to iron-fish/ironfish that referenced this issue Aug 5, 2022
Axios does not time out idle connections, this means if you're downloading
for a long time and the TCP connection gets lost you'll be stuck
downloading the snapshot and it won't fail or kill the command.

This re-writes downloading to use axios cancelation source to kill the
request after a specified idle timeout. It also fixes some issues
plumbing the errors through the process.

See axios/axios#2143 for more information
NullSoldier added a commit to iron-fish/ironfish that referenced this issue Aug 5, 2022
Axios does not time out idle connections, this means if you're downloading
for a long time and the TCP connection gets lost you'll be stuck
downloading the snapshot and it won't fail or kill the command.

This re-writes downloading to use axios cancelation source to kill the
request after a specified idle timeout. It also fixes some issues
plumbing the errors through the process.

See axios/axios#2143 for more information
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

No branches or pull requests

8 participants