axios / axios

Promise based HTTP client for the browser and node.js
https://axios-http.com
MIT License
105.39k stars 10.88k forks source link

axios timeout s not respected for chunked requests #5886

Open n0th1ng-else opened 1 year ago

n0th1ng-else commented 1 year ago

Describe the bug

Hello dear team. hope you are doing well.

I am about to report a bug, although it probably works as expected, then I would ask you to clarify this behavior in the documentation.

regarding the axios timeout - it works fine for regular requests. But it is not respected for chunked requests. Imagine the server is streaming the response chunks for 30 sec and I would expect setting 10 sec timeout will abort execution. Instead, it keeps hanging and waiting on the last chunk, so the request can be finished.

It is quite non-intuitive and confusing and I wonder if it was designed like that, or you consider it as a bug and we want to fix it. I have attached the github repository with a reproduction example. https://github.com/n0th1ng-else/axios-streaming-timeout

could you please take a look?

image

To Reproduce

https://github.com/n0th1ng-else/axios-streaming-timeout

Code snippet

No response

Expected behavior

The request is aborted after 10 sec due to timeout, even tho it is half way through

Axios Version

1.5.0

Adapter Version

No response

Browser

NodeJS express + axios

Browser Version

No response

Node.js Version

16.16.1

OS

MacOS Venture 13.4.1

Additional Library Versions

No response

Additional context/Screenshots

No response

SagarKapoorin commented 1 year ago

Hi there, I'm relatively new to this project, so please correct me if I'm mistaken. i have read your commits (file )and come with idea that:- Axios may not forcefully terminate the request while it's still expecting more data to come.So,i think in that case timeout function will not work for you rather We have to make a separate timeout function in addition to axios's timeout function.By implementing this custom timeout mechanism, you should have better control over requests with chunked responses and ensure that the timeout is respected

n0th1ng-else commented 1 year ago

Yes exactly, once the first chunk is received, Axios drops the timeout and considers the request answered in time. This I think raises the debate on at what moment we consider the request timed out - at the first byte received or the last byte received.

Initially yes, I was also thinking of the custom timeout function and wrapping the axios request into Promise.race([]) with that timeout function. But I found a workaround for this issue (the issue in my case, as it generally looks like designed this way), the solution is to use AbortSignal. It cancels the request regardless, see the example:

    const AXIOS_TIMEOUT = 5000;

   axios.request({
        method: "POST",
        url,
        timeout: AXIOS_TIMEOUT,
        signal: AbortSignal.timeout(AXIOS_TIMEOUT), // Ensures the request will be cancelled
    })

I wrote a detailed text about the technique in the article

I would still love to clarify this behavior in the documentation