Repository navigation
Closed
Description
- Version: Node v4.4.4 / Node v6.1.0
- Platform: Linux 4.2.0-1-amd64 deps: update openssl to 1.0.1j #1 SMP Debian 4.2.6-3 (2015-12-06) x86_64 GNU/Linux
;(function loop () {
return Promise.resolve().then(loop)
})()The code above increasingly consumes memory until it crashes with:
<--- Last few GCs --->
16059 ms: Scavenge 1406.1 (1457.9) -> 1406.1 (1457.9) MB, 19.4 / 0 ms (+ 2.6 ms in 1 steps since last GC) [allocation failure] [incremental marking delaying mark-sweep].
18001 ms: Mark-sweep 1406.1 (1457.9) -> 1406.1 (1457.9) MB, 1941.5 / 0 ms (+ 3.7 ms in 2 steps since start of marking, biggest step 2.6 ms) [last resort gc].
19928 ms: Mark-sweep 1406.1 (1457.9) -> 1406.1 (1457.9) MB, 1927.5 / 0 ms [last resort gc].
<--- JS stacktrace --->
==== JS stack trace =========================================
Security context: 0x30f9e32b4629 <JS Object>
1: PromiseSet(aka PromiseSet) [native promise.js:~38] [pc=0x21bc625235b] (this=0x30f9e32041b9 <undefined>,n=0x23be73509901 <a Promise with map 0x2d3fc3316dc9>,q=0,r=0x30f9e32041b9 <undefined>,t=0x23be73509961 <JS Array[0]>,u=0x23be73509941 <JS Array[0]>)
2: PromiseInit(aka PromiseInit) [native promise.js:~53] [pc=0x21bc624d6e9] (this=0x30f9e32041b9 <undefined>,n=0x23be73509901 <a Promis...
FATAL ERROR: CALL_AND_RETRY_LAST Allocation failed - process out of memory
With Bluebird, the used memory never goes above 30MB and the program does not crash:
global.Promise = require('bluebird')
;(function loop () {
return Promise.resolve().then(loop)
})()Reacted by Dmitriy Pushkov and Jadian
Reacted by Brian Kim
Activity
mscdex commentedon May 10, 2016
/cc @nodejs/v8
Fishrock123 commentedon May 10, 2016
Is this because each promise keeps a reference to the next?
ChALkeR commentedon May 10, 2016
@Fishrock123
Doesn't look like that, looks like mark-sweep isn't called for whatever reason.Could be a duplicate of #6180.Perhaps it's even fixed by #6398 in the current master, but I can't check atm.Update: it's not, #6180 isn't fixed yet.jeisinger commentedon May 10, 2016
filed https://bugs.chromium.org/p/v8/issues/detail?id=5002
Firefox also runs out of memory, so maybe it's a spec thing
according to the crash, MS is executed.
ChALkeR commentedon May 10, 2016
@jeisinger
It's not a spec thing, adding a manualgcevery e.g. 1e7 iterations fixes it.Looks very similar to #6180 — scavege is triggered instead of mark-sweep, but the memory is reclaimed only by mark-sweep, not by scavenge.
Update: I made a mistake in the test, shame on me =).
julien-f commentedon May 10, 2016
@jeisinger If it's a spec thing it's an issue because I don't know a better way to do a loop with promises :/
@petkaantonov What's your opinion on this?
ChALkeR commentedon May 10, 2016
@jeisinger No idea why the mark-sweep at the end doesn't reclaim most of that, though.ChALkeR commentedon May 10, 2016
@Fishrock123 @jeisinger I made a mistake in the test, ignore my previous comments. Sorry for the inconvenience.
Fishrock123 commentedon May 10, 2016
Unbounded recursive loops are typically not safe in JavaScript at the current time, promise or not apparently.
ChALkeR commentedon May 10, 2016
Yes, it looks like all Promise objects are retained in the heap. There are twice more of them than the number of iterations, btw.
vkurchatkin commentedon May 10, 2016
makes sense, 2 new promises are created on each iteration
julien-f commentedon May 10, 2016
Yes but there are no other way to do async loops than using recursion (without tricks likes
setImmediate()to break the stack).ChALkeR commentedon May 10, 2016
Ah, yes, that is correct.
28 remaining items
danielo515 commentedon Jul 28, 2017
@bnoordhuis update to which version ? With v.7 I mean 7.10. If you mean that I should update to node v.8 that may be a problem
bnoordhuis commentedon Jul 28, 2017
@danielo515 either v6.x or v8.x, both are LTS release lines.
danielo515 commentedon Jul 28, 2017
Thanks @bnoordhuis , I'll give a try to v8.x
fenixphp commentedon Jan 26, 2018
Can anyone explain the result of this conversation? I'm using "Node v8.9.2" but there is still a leak for cyclic use Promise or async / await functions.
danielo515 commentedon Jan 27, 2018
@fenixphp do you mean that you experiment the leak just with promises or that you experiment it both with promises and async functions ?
Theoretically this only happens on promises because they keep a chain that grows wild, which is not the case of async functions.
fenixphp commentedon Jan 27, 2018
Thanks @danielo515, Experimentally, I came to the conclusion that promises begin to leak as the call chain looks like a tree. What can I replace the promises in a pair of async / await functions?
danielo515 commentedon Jan 28, 2018
You can use async await function calls inside an infinite loop without any problem. But if you use recursion you will end with the same stack size problem.
Poller: remore recursion and memory leak. The problem is well describ…
Sharcoux commentedon Nov 18, 2019
Reading this thread, I am getting a little worried and confused.
The following code will make me able to pile tasks wherever in my codebase, and make sure that each task is executed only after all the previous ones are done.
Thanks for your help
dr-js commentedon Mar 15, 2020
@Sharcoux your code do not have the tail-recursive pattern like the example, and if your outer code do not have the pattern either, and only reference the tail of the promise chain, the GC should work and no memory leak.
dr-js commentedon Mar 15, 2020
From what I tested, the pattern for promise memory leaking must include a tail-recursive setup, the promise creating function always recursively appear in itself's
promise.thencode.The recursive code cause the promise implementation need to keep extra reference/stack info so the GC can not claim those already-resolved promise from the chain.
If we build a long/infinite promise chain gradually without the recursive code pattern, and only keep reference to the tail of the chain, the GC can took those un-referenced promise from the chain, whether the promise is pending or not.
So I think chaining promise up is not always unsafe, it depends on the actual code.
And if the promise version of
while (true) { /* async code */ }is needed, better use aasyncfunction instead of the tail-recursive promise pattern.First part of test is 2 different OOM sample code and a fix attempt:
Then some common promise chain use pattern is tested:
The test code I used: (and an sample output from
v13.11.0 x64 win32)Fix memory from leaking
Fix memory leaking
feat(utils): add sequential async iterators
refactor: clear Sonar maintainability issues (S9382, S7760, S2301) (#752