Axios will not timeout on idle sockets
See original GitHub issueDescribe 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
- 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}/`);
});
- 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'));
- 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.
Issue Analytics
- State:
- Created 4 years ago
- Reactions:4
- Comments:9 (2 by maintainers)
Top Results From Across the Web
Timeout feature in the axios library is not working
Let say you've requested the URL through axios and server is taking long time to respond, in this case the axios timeout will...
Read more >A Complete Guide to Timeouts in Node.js
Node.js exposes a server.timeout property that determines the amount of inactivity on a connection socket before it is assumed to have timed out...
Read more >NodeJS with Keep-Alives and Connection Reuse -
This post will be an overview of how to implement connection reuse on Azure with Node.js. By default, Node.js doesn't reuse connections ......
Read more >Handling timeout in Axios
The default timeout is set to 0 which indicates no timeout. With that default value, any remote end can keep us waiting for...
Read more >Improving API Performance with HTTP Keepalive
Reuse established connections, Easy to do with HTTP Keepalive ... Connection idle until another request comes in... opt [first request] ...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
I agree, an infinite fetching time can be problematic, especially if you expect a small response.
My workaround is to use a cancel token.
+2 cents to this issue: