-
Notifications
You must be signed in to change notification settings - Fork 407
Fix ZoneAwarePromise.all to resolve at the correct time #1150
Conversation
For ZoneAwarePromise.all, as [implemented](https://github.com/angular/zone.js/blob/7201d44451f6c67eac2b86eca3cbfafde14035d6/lib/common/promise.ts), the `count` variable serves three purposes: 1. Count the number of values passed-in 2. Specify the index of a resolved value in `resolvedValues` 3. Track when all the promises have been resolved. In the event that `value.then` is immediately called, `count` will be decremented at the incorrect time resulting in a potentially negative value or reaching 0 when not all values have actually been resolved. The fix is to be more meticulous about using the correct indices at the correct time and to not overload the count and number of resolved promises. This updated version is more explicit about the purpose of the `unresolvedCount` and `valueIndex` variables. `unresolvedCount` needs to start at 2 to prevent `resolve` from being called too soon in the event that `value.then` calls the callback immediately. The scoping of the index for use in `resolvedValues` is made constant to guarantee the correct index is used. This buggy behavior specifically manifested as an issue in the Monaco editor but most likely occurs elsewhere as well in cases where promises may immediately call into `.then`. Related issue: microsoft/monaco-editor#790 (comment)
cc @rkirov |
Maybe mention this fixes #1077 |
The continuous-integration/travis-ci/pr failure appears to be a disconnect. I didn't see any failing tests. I don't know how to re-run those tests. |
I remembered some other guy get the same issue before, I think this is the bug of |
Igor says that @mhevery is the right person to review this |
Also at a higher level, why do we need to reimplement these? Why can't we have our monkeypatches reuse the builtins? |
should we add some test cases about this? I can do that in another PR. |
@evmar, yeah, I think if we only need to keep the |
@mhevery, this appears ready to go. Not sure what the next step is for this. |
For ZoneAwarePromise.all, as implemented, the
count
variable serves three purposes:resolvedValues
In the event that
value.then
is immediately called,count
will be decremented at the incorrect time resulting in a potentially negative value or reaching 0 when not all values have actually been resolved.The fix is to be more meticulous about using the correct indices at the correct time and to not overload the count and number of resolved promises. This updated version is more explicit about the purpose of the
unresolvedCount
andvalueIndex
variables.unresolvedCount
needs to start at 2 to preventresolve
from being called too soon in the event thatvalue.then
calls the callback immediately. The scoping of the index for use inresolvedValues
is made constant to guarantee the correct index is used.This buggy behavior specifically manifested as an issue in the Monaco editor but most likely occurs elsewhere as well in cases where promises may immediately call into
.then
.Related issue:
microsoft/monaco-editor#790 (comment)
This fixes #1077