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

2

u/Gundersen Oct 11 '17

I didn't get from the article why the promise spec and implementations leak memory. What is it exactly in the spec that requires references to be kept around? I'm guessing it's just a single reference from the new promise to the previous one (or the other way around) that is causing this issue? But for what reason is that reference being kept?

3

u/alexelcu Oct 11 '17

So the problem is that it creates a callback chain. Picture this ...

const p = new Promise(...).then(r1 => {
  new Promise(...).then(r2 => {
    new Promise(...).then(r3 => {
      //...
    })
  })
})

If you'll try imagining how an implementation looks like, you'll come up with something like an array on which you .push() those then listeners, which are going to be executed when the promise will eventually complete.

The issue with the chaining above is that:

  1. the first promise (the result of the first then) will not complete until the second promised then does, so a callback for completing the first promise is kept in the second promise

  2. the second promise (the result of the second then) will not complete until the third promise does, so a callback for completing this second promise is kept in the third promise

  3. ...

The problem is that in this chain of 3 promises, the second promise is no longer relevant once it produced r2 so you can simply discard it and if you don't, then it leaks.

Therefore a well known solution to this is to reverse the chain. You make the second promise forward its completion state to the first. And the third promise to forward its state to the second and (by compressing the chain) to the first, thus discarding the second.

The problem with the Spec is that the Spec has to guarantee an ordering of operations in certain cases. And by implementing this solution, then ordering changes — not necessarily in a bad way, but the issue is you can no longer rely on the spec alone to tell you what result you'll get, because it ends up being implementation dependent.

So in other words, either all implementations are going to do this, or we can just leave this as semi-unspecified, in which case everybody is wrong, given that some implementations will crash your process whereas others will not.

2

u/Gundersen Oct 11 '17

You should add this part to the article, so it's clearer what the problem is.

Can you also show how the timing of things would be different with an alternative implementation?

I'm guessing a solution/work around is to not return the new promise in each step, since you don't really care about the result. It looks like you only really care about the error?

1

u/alexelcu Oct 11 '17

In my example yes, but I kept it simple to easily reproduce the problem.

Many times however you do care about building a state, folding over the results ... e.g. if you have chunks of data read asynchronously from a file, you might want to gradually analyze it and produce some final result eventually.

Yes, a solution would be to not use return, but many times you need to return, since you need that value. But if you complicate the sample shown, then the origin of the leak gets confusing, so I guess it's a trade-off I haven't hit.

1

u/Gundersen Oct 11 '17

The only real use case for something like this (where it would be an actual problem) is an infinite chain of events, like an async message queue. I've used similar code for that before, without knowing of this memory leak. Letting it run forever in a node app would cause issues.

But now that we (soon) have async iterables it seems less like a problem. They fit much better in those situations, like reading a file and processing chunks of it. I guess promises aren't really designed to be used like this, and while it is a problem now, it's less likely to be one when we have alternatives.

1

u/alexelcu Oct 11 '17

I've included a sample here, refresh to see it: https://alexn.org/blog/2017/10/11/javascript-promise-memory-unsafe.html#concrete-use-cases

What I'm describing is basically an async iterator and I think that even for iterators, it's useful to have a non-leaky implementation because you can then easily describe lazy stream transformations.