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.

Response timeout does not work as expected

See original GitHub issue

The following code doesn’t work as expected (no timeout error is fired):

 client.get('/stop-writing-body-halfway', {
    timeout: 100,
 })

After a few minutes, a response is resolved despite Content-Length not having been reached and the server not explicitly closing the connection. Edit: never mind the connection is probably closed after 2 mins because of https://nodejs.org/api/http.html#http_server_timeout

My server never closes the connection and stops writing the body half way:

  const stopWritingBodyHalfway = (msg, res) => {
    res.setHeader('Content-Length', 500)
    const body = Buffer.alloc(250, 'a')
    res.write(body)
  }

Is the timeout only for the request, not for the response?

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Reactions:17
  • Comments:20 (2 by maintainers)

github_iconTop GitHub Comments

16reactions
keyworkscommented, Aug 9, 2019

In case it’s useful to anyone I’ve been patching the behaviour of timeout by using an interceptor. It uses a cancel token to cancel the request after timeout has elapsed.

const requestTimeoutInterceptor = config => {
  if (config.timeout === undefined || config.timeout === 0) {
    return config;
  }

  const source = axios.CancelToken.source();

  setTimeout(() => {
    source.cancel(`Cancelled request. Took longer than ${config.timeout}ms to get complete response.`);
  }, config.timeout);

  // If caller configures cancelToken, preserve cancelToken behaviour.
  if (config.cancelToken) {
    config.cancelToken.promise.then(cancel => {
      source.cancel(cancel.message);
    });
  }

  return { ...config, cancelToken: source.token };
};


axios.interceptors.request.use(requestTimeoutInterceptor);
5reactions
dtryoncommented, Oct 12, 2016

@olalonde Yes, exactly. At the moment, the current timeout is cleared before the response body has been completely received:

https://github.com/mzabriskie/axios/blob/master/lib/adapters/http.js#L116

The timeout gets cleared before the body stream is processed.

Instead the timeout could be be cleared later (when the body has been completely received).

Read more comments on GitHub >

github_iconTop Results From Across the Web

Why Connect Timeout and Response Timeout not working in ...
I notice that Connect Timeout and Response Timeout are not working as expected. In the HTTP Request, I've specified Connect Timeout to be...
Read more >
408 Request Timeout - HTTP - MDN Web Docs
The HyperText Transfer Protocol (HTTP) 408 Request Timeout response status code means that the server would like to shut down this unused ...
Read more >
expect -timeout vs. "set timeout" in expect: Different behavior
The problem is explained fully in the Stack Overflow post. Why doesn't this expect handle timeout or eof. In short, the -timeout flag...
Read more >
Troubleshoot your Application Load Balancers
Troubleshoot issues that you might encounter with your Application Load ... that the target page is not responding before the health check timeout...
Read more >
FIX: The Timeout parameter for the IcmpSendEcho function ...
File name File size Date Time Path Ipv4.lib 2,473,026 12‑Mar‑2012 17:49 Public\Common\Oak\Lib\Armv5\Checked Ipv4.lib 2,550,392 12‑Mar‑2012 17:48 Public\Common\Oak\Lib\Armv5\Debug Ipv4.lib 2,370,616 12‑Mar‑2012 17:48 Public\Common\Oak\Lib\Armv5\Retail
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