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

0

u/[deleted] Oct 11 '17

[deleted]

-18

u/alexelcu Oct 11 '17

As I mentioned in the article, an implementation that doesn't leak is possible.

So obviously the assertion about creating an "infinitely long promise chain ... means nothing can be GCed" is false.

To see it in action, do this:

npm install funfix --save

And then:

import { Future } from "funfix"

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

loop(0).recover(console.error)

This code is doing exactly the same thing, but it doesn't leak.

This is sort of a Stockholm Syndrome btw :-)

0

u/[deleted] Oct 11 '17

[deleted]

-9

u/alexelcu Oct 11 '17

All loops in functional programming are expressed as tail recursions, or as folds that are implemented with tail recursions, because otherwise there's no way to express them. And classic loops are inadequate for expressing algorithms involving asynchronous calls, being error prone.

This is not a narrow, edge case, having started with lambda calculus in 1930. It's only an edge case when your tool of choice doesn't support it.

Hence my Stockholm Syndrome joke is really only a half joke ;-)

0

u/[deleted] Oct 11 '17

[deleted]

3

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

It is an edge case to keep around a 2,000,000 promise long chain

The issue is that the implementation should not keep around 2,000,000 promises, being the problem we are talking about. Please re-read what I wrote, because I don't want to keep repeating the same words.

As for why this is an issue, I'm also a little out of breath explaining why, tried to do that in a comment here.

If this is a bug in the implementation ... you should just report it to the v8 team

This isn't an issue with V8, since Firefox has the same behavior, but an issue with the standard.

If it's an issue in the spec you should point out the flaw in the spec and start a discussion about how to fix it.

I was under the impression that I'm doing just that and I'm showing you the solution as well.

you write an article attacking what's become a fundamental part of JS for the purpose of promoting your own library

Well, those of us that are building libraries, we like talking about it. I'm sorry for contributing stuff, I really am, it's a reward-less activity.

It's toxic and my issue is that it does nothing to help anyone except you.

You're attacking me for no reason. My critique was for a piece of technology, yours is personal.

0

u/[deleted] Oct 11 '17

[deleted]

11

u/alexelcu Oct 11 '17

So I should just be an angry man that complains about what other people do, but that does nothing about it?

Personally I prefer to show and tell, to build and deliver alternatives and to talk about it. That's not a "scumbag thing to do" in my culture, even if I were to do this for money, which I don't, this being on my own free time and at personal sacrifice.

What about calling people scumbags? Is that appropriate for this or any other community?

We have nothing more to talk about. Have a great life.

1

u/pier25 Oct 23 '17

It is an edge case to keep around a 2,000,000 promise long chain

I agree. Very few people will stumble upon that problem, if ever.

Whether the standard is properly designed to handle such an edge case is another story.

6

u/[deleted] Oct 11 '17

funfix

Ah, so this whole contrived example is to push the funfix package you wrote.

Future isn't a promise. So of course you can write it however you want. You can leave spec behind so you can show you fixed this "terrible" Promise bug by not using Promises.

1

u/alexelcu Oct 11 '17

No, the Funfix implementation is there to show that it can be done, otherwise there would be people here saying that it can't be ... oh wait.

Well, I've been called a scumbag for doing self promotion. I guess that if you work on stuff and talk about it, you must be one.

5

u/[deleted] Oct 11 '17

I guess that if you work on stuff and talk about it, you must be one.

No. That's not the case. It's how you're coming across in all of this.

You came up with a rather contrived, academic example to demonstrate a very specific problem. Instead of addressing how the specific implementation causes problems, you attempted to generalize it beyond the scope of the issue - including using a clickbait title like "Promises are Memory Unsafe".

No, Promises are NOT memory unsafe and that has been shown time and time again in this thread. In fact, your example isn't even memory unsafe - it's just a fucking infinite loop that causes the program to run out of memory. The exception is thrown because JS can't safely handle memory after it's out of usable memory space.


People are upset with you because they expected you to actually demonstrate how Promises as a whole suffer from a serious memory problem. You didn't and people called you out. You somehow decided that the best approach was to double down on your flawed argument and attack people for criticizing your approach.

-1

u/alexelcu Oct 11 '17

I replied in kind to all technical arguments and added extra explanations where possible, given the limitations of a forum and the time available.

Your argument has a logical inconsistency ... either my linked Funfix is proof that the leak is real and that a fix is possible, or it is not. I also added the following links to that article, for completeness sake:

Now if you have actual technical arguments or opinions, that you can express politely, then we can have a dialog.

1

u/pier25 Oct 23 '17

So obviously the assertion about creating an "infinitely long promise chain ... means nothing can be GCed" is false.

How is that? If you keep a reference to something then it won't be GCed...

This code is doing exactly the same thing, but it doesn't leak.

Well, then it's not doing exactly the same thing. Does it?

1

u/alexelcu Oct 24 '17

There is nothing in the Promise API that says references must be kept in such chains — it's quite interesting how people jump to conclusions on how the implementation should behave, when the API itself is free of such constraints.

In fact intermediate references can be dropped, implementations are known, TFA now includes mentions of such implementations, which are widely deployed and verifiable. TFA also includes acknowledgements from the authors of the Promise spec and others.

And I think I've been pretty clear on that. If not, then let me know how the explanation in TFA can be improved.