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

Confusing timeout property #5896

Open
ueokande opened this issue Sep 15, 2023 · 2 comments
Open

Confusing timeout property #5896

ueokande opened this issue Sep 15, 2023 · 2 comments

Comments

@ueokande
Copy link

ueokande commented Sep 15, 2023

Section/Content To Improve

The timeout property in Request Config

Suggested Improvement

The description of the timeout property says:

timeout specifies the number of milliseconds before the request times out. If the request takes longer than timeout, the request will be aborted.

I believe it is incorrect in Node.js. Axios calls request.setTimeout() here and it invokes socket.setTimeout() internally. The API document explains as the following:

Sets the socket to timeout after timeout milliseconds of inactivity on the socket.
https://nodejs.org/api/net.html#socketsettimeouttimeout-callback

This means the sockets emit a timeout event when it doesn't send or receive data for a certain time. The request can exceed the specified timeout in the case of a huge request payload or slow network.

This is the reproduction code. The server send payload slowly, and the takes about 10 seconds which is longer than timeout.

import http from 'http';
import Axios from 'axios';
import timer from 'timers/promises';

(async () => {
  const server = http.createServer((req, res) => {
    (async () => {
      for (let i = 0; i < 100; i++) {
        res.write('.');
        await timer.setTimeout(100);
      }
      res.end();
    })();
  });

  await new Promise((resolve) => { server.listen(3000, resolve) });

  const startAt = Date.now();
  const axios = await Axios.get('http://127.0.0.1:3000', { "timeout": 5000 });
  const endAt = Date.now();
  console.log(`Received ${axios.data.length} bytes in ${endAt - startAt} ms`);

  server.close();
})();

Output:

$ node index.mjs
Received 100 bytes in 10035 ms

Relevant File(s)

No response

@ueokande
Copy link
Author

ueokande commented Sep 30, 2023

I described GET requests can exceed the timeout. On the other hand, I found POST requests with a big payload throws a timeout error when the request spends over the timeout. This behavior depends on follow-redirects which axios uses internally and it is different from the standard socket.setTimeout(). This difference between GET and POST is unexpected one and I found some issues on the follow-redirects repository.

I think this issue is not only document problem. It's better if you could fix axios (or follow-redirects).

@Vansh1419
Copy link

Could u assign this issue to me?

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

2 participants