-
-
Notifications
You must be signed in to change notification settings - Fork 10.8k
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
CancelToken causes memory leak if re-used for multiple requests #3001
Comments
Hi, I think #1118 is possibly the wrong issue? Other than that I think we should have a look at this. Thanks |
You are right Jason - Sorry it was #1181 |
Hello, according to your method seems to be useless, is my writing wrong, hope to get a reply, thank you very much // request
config.cancelToken = new cancelToken(cancel => {
source[request] = cancel;
})
// resonse
interceptors.response.use(response => {
source[request] = null
}) |
?? I am not sure what you mean by "seems to be useless"? If I understand your code you are cancelling the token immediately after a single request. However, for a file upload of 2000+ requests (I am talking multi-GB files here) I used the following algorithm. Obtain cancel token This has the advantage of not having to allocate cancel tokens on every request. However, this causes a memory leak. Instead to avoid this every single request has to cancel the token and then allocate a new token. No interceptors are involved. |
Thank you very much for your reply. I may not express it clearly. I saw you in your comments saying: Remove the cancellation token for every request and create a new cancellation token. Now the problem is, the pseudo code that I understand to remove the cancellation token for each request is as follows source[request] = null; But after I set it this way, there are still memory leaks. You can write a pseudo code to explain how Help remove the cancellation token for every request and create a new cancellation token? |
This issue also causes leak of the entire memory used for request & response when assigning a default instance.defaults.cancelToken = new axios.CancelToken((canceler) => { /* anything */ });
// Now every request sent through `instance` will leak memory because of the unique token (unless they override it) It seems @DigitalBrainJS's #3305 nicely fixes the issue. Is there any chance to get it accepted & released? 🙏 |
Obtaining a cancel token (as per the Axios documentation on github) and re-using it across many requests results in a major memory leak as the payload for each request is not freed.
I am transferring large files - from 3 GB to 100GB using a chunked protocol and 20MB requests. The memory fills and remains until the cancel token reference is removed. Using the same code but not setting the cancel token in the config resolved the issue.
Creating a cancel token for each and every single request individually and removing the reference to the cancel token achieves the expected behaviour and frees the request payload memory.
Simple use of chrome dev tools and memory profile shows the cancel token as holding the memory, also reflect in process stats using top.
This is not using the global axios but an instance.
Previously reported in #1118 but it is unclear if this was properly resolved.
Axios 0.19.2
OS Linux
Browsers: Firefox 76.0, Opera 68.0.3618.63, Chromium 81.0.4044.129
The text was updated successfully, but these errors were encountered: