axios: Axios doesn't address memory leaks?
Describe the bug
I used the latest version of Axios (v0.20.0-0) with interceptors, interceptors.response.use () after that, the sent request will be viewed through the chrome tool. The returned content will always be saved and will not be released. This problem should be very common and should have been solved earlier. Is there a problem with my configuration? I hope you can get a reply. Thank you very much
To Reproduce
Pseudo code
servers.interceptors.response.use();
Expected behavior
I send requests to intercept, but don’t leak memory
Environment
- Axios Version 0.20.0-0
- Browser Chrome
- Browser Version 84.0.4147.125
- Node.js Version 14.0.1
- Additional Library Versions React 16.7
Additional context/Screenshots
Add any other context about the problem here. If applicable, add screenshots to help explain.
About this issue
- Original URL
- State: closed
- Created 4 years ago
- Reactions: 11
- Comments: 22 (6 by maintainers)
I’m on version 0.21.1 and encountering this issue. The data from the responses isn’t being garbage collected.
We were using 0.19.2 with no issues. We bumped to 0.20.0 and an issue arose with a box running out of memory and crashing. We used interceptors and found the response was not being released, as you described. Slowly leaking and eventually dying. We reverted back to 0.19.2 and everything seems to have gone back to ok. I cannot speak for 0.18.1 myself as this was for a prototype and we never used 0.18.1 on it so there may have been bug fixes in between 0.19.2 and 0.18.1.
Otherwise if you’re still having issues on 0.19.2 we may have had different root issues. I have never really used axios, so you’re probably way ahead of me if it’s a config thing. I was just the unlucky person assigned to finding the issue 👍
Thanks I will update Axios
I’ve done some investigation and have narrowed down the problem some. In my case the FD/memory leak happens when
options.maxRedirects > 0
, in which case lib/adapters/http.js is using the follow-redirects library. Unfortunately in case of redirects, therequest.setTimeout()
function gets called on the original request only, and not on the redirect requests. This means the sockets hang around forever unless explicitly destroyed, as indicated in the NodeJS documentation,When I change the request URL so that a redirect does not happen, hence the native NodeJS http/https libraries are used, no leak whatsoever since there’s only a single request involved.
This is what I think the problem in. Tomorrow I’ll write a wrapper around the follow-redirects library to manually set a timeout in all the redirect requests, and see if that solves the issue with memory leaks when there are redirects involved.