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.

Axios will not timeout on idle sockets

See original GitHub issue

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.

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:4
  • Comments:9 (2 by maintainers)

github_iconTop GitHub Comments

3reactions
aarcangelicommented, May 26, 2019

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') });
0reactions
jesse23commented, 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
Read more comments on GitHub >

github_iconTop 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 >

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