First of all, I just wanted to say I'm a bit embarrassed to see people getting rude over what is clearly a legitimate, well-isolated, reproducible, and well explained issue. Shitting on other people's work like that is just plain unprofessional. I think whether funfix or bluebird were used as a POC solution should have been irrelevant, and that ideally people should have been eager (or at least open) to learn about the existence of a potential freaking crash and mitigation techniques rather than bickering over the definition of a memory leak.
So I guess the important question is: can the promise spec be fixed?
To be fair, there's nothing to fix. Yes you can optimize it, but then there has to be made a good argument as to why this is needed.
As it stands the issue sounds a lot like "I filled an array with the number 0 until JavaScript crashed (out of memory). This is bad, and if only the array was run-length encoded (compressed) to support an arbitrary number of 0's, there would not be a crash.
For both the original issue and my toy array example, yes you can optimize them, but the question becomes: Are there enough legitimate cases of this in the wild to warrant making the standard more complex? I think the answer is no, and there's plenty of room to create custom implementations outside of the spec to work around this issue (like the OP did).
the question becomes: Are there enough legitimate cases of this in the wild to warrant making the standard more complex
Yeah, that's a great line of reasoning. Personally I would prefer that the solution be a native one, and I feel that if bluebird is able to get away with its optimization, it should theoretically be possible to amend the spec, or at least get vendors to agree to make the optimized implementation the de-facto standard. Admittedly, I haven't dug too deep into this yet.
From going down this rabbit hole before, I think the main issues are:
Assimilating an old Promise (or Thenable) into a new Promise without maintaining a reference to the old Promise is easy IF both Promises came from the same implementation. This is because the Promises need to be able to look into each other’s internal states in order to assimilate. Since one point of Promise/A+ was interop, it’s potentially spec-breaking to force an implementation detail like this.
There are potential GC issues when different segments of a recursive Promise chain are held by different variables.
The Promise/A+ folks have certain tests to guarantee resolution order in all kinds of edge cases, and implementing TCO seems to break that.
The ECMAScript use of the Promise/A+ spec complicates things further since now there’s another layer of approvals etc. that needs to happen for what, effectively, is an edge case with documented workarounds.
And my own spin on it:
There seems to be a major force in the JS community that wants to maintain Promises as a 1-for-1 asynchronous analog of sync code. This makes sense from the perspective of the async/await API. Just as infinite recursion will exhaust the stack, infinite Promise recursion will exhaust the heap. Perhaps, if TCO is ever shipped for sync code, it will also be shipped for Promises.
2
u/alexelcu Oct 11 '17
It depends on what your memory limit is and it doesn't crash immediately, first the GC freezes the world a few times, freeing whatever memory it can.