r/javascript • • Oct 11 '17

JavaScript's Promise is Memory Unsafe

https://alexn.org/blog/2017/10/11/javascript-promise-memory-unsafe.html
70 Upvotes

77 comments sorted by

View all comments

11

u/1-800-BICYCLE Oct 11 '17 edited Oct 11 '17

For starters, return await loop should be return loop. No point in useless rewrapping.

Secondly, I suspect the reason why your Funfix implementation works and native doesn’t is because native promises (and possibly even setImmediate) use the microtask queue and not the event queue. The microtask queue fills and empties before the next turn of the event loop, so if you’re constantly filling the queue with promises to be resolved, chances are that it’s never getting out of that turn. If my theory is right, you’re basically creating an infinite loop (edit: or an infinitely linked list of promises) in the microtask queue.

That said, the solution here has always been to unchain the tail call with setTimeout (yes, setTimeout, not setImmediate or process.nextTick) . Note that your loop here never ends, so you’re effectively only using Promises for side effects and therefore don’t need to chain them at all.

5

u/alexelcu Oct 11 '17 edited Oct 11 '17

For starters, return await loop should be return loop. No point in useless rewrapping.

That is correct, but my primary test has been with the plain function using then, precisely to not make mistakes like this. It makes no difference though.

the solution here has always been to unchain the tail call with setTimeout (yes, setTimeout, not setImmediate or process.nextTick)

I know what you're referring to, but your intuition here is not correct. I updated the article to use setTimeout and now it crashes even faster, managing to only go through ~ 1,000,000 iterations before it chokes.

What ends up happening is that the code is creating a chain of promises waiting on each other for completion. So it creates f1, which waits on f2, which waits on f3, creating a long chain of resolve callbacks being registered for triggering once the last promise is complete and since that never happens, the heap eventually blows up.

What I did in Funfix to fix it, is to indicate to newly created futures (in flatMap) that they need to complete the source, doing a sort of chaining in reverse. See the implementation.

And so what this does is to effectively drop intermediate future references, leaving directly accessible by the GC only the future reference you're holding on to and the active future. And so it ends up using constant memory.

Note that your loop here never ends, so you’re effectively only using Promises for side effects and therefore don’t need to chain them at all.

It's just a simple example to illustrate a point.

You can imagine a stream of tasks processed one by one, where the results are folded into a final result that doesn't have to be void.

Yes, promises get used for triggering async side effects, but many times you also want to aggregate a result as well and having your process blow up only after ~1,000,000 items or so is a big surprise, at least to an FP developer like myself, given that these are asynchronous calls that shouldn't have such problems.

5

u/1-800-BICYCLE Oct 11 '17

Aw shucks, I thought that would fix it. For the last part, though, what I meant was (sorry, I had just woken up lol):

function loop(n) {
  return signal(n).then(i => {
     if (i % 1000 == 0) console.log(i)
     setTimeout(loop, 0, n + 1)
  })
}

Or maybe even

function loop(n) {
  return signal(n).then(i => {
    if (i % 1000 == 0) console.log(i)
    loop(n + 1)
  })
}

You can still sequence and aggregate the results without explicitly chaining the Promise object. I believe that libraries like bluebird do have this same flapMap optimization you’ve created, though (but I’ve started to prefer native for less dependencies).

5

u/alexelcu Oct 11 '17

I have not tried Bluebird, maybe it does it because the problem and the solution are pretty obvious, which is why I was surprised to see that the standard Promise doesn't do it.

5

u/1-800-BICYCLE Oct 11 '17

https://github.com/nodejs/node/issues/6673

You can read about the rationale there — seems to be an issue with either the Promise/A+ or ES6 spec.

3

u/alexelcu Oct 11 '17

Oh, that's useful, thanks.

1

u/SaabiMeister Oct 11 '17

I believe the memory consumption comes from the fact that they keep the functions on hold even if not needed for normal functioning because if an exception is thrown they want to build a stack trace.