axios: Response timeout does not work as expected
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?
About this issue
- Original URL
- State: closed
- Created 8 years ago
- Reactions: 17
- Comments: 20 (2 by maintainers)
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 aftertimeout
has elapsed.@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).
I’ve come to this solution.
On a related note, I wrote https://github.com/blockai/broken-http-server to help test those sort of things in my code. Could be helpful in axios tests.
@Rewieer @all, Even I was facing this issue. I used to stop the server where I send API requests and then I used to make a request. It used to take minutes for displaying the error message.
Then I found a solution provided by someone in the issues. I do not remember the issue number or title. (Please attach the issue here if someone recognizes the title of the issue from the code I am providing)
This is what I did: