-
Notifications
You must be signed in to change notification settings - Fork 4
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Prevent stackoverflow #23
Conversation
@@ -251,7 +251,13 @@ namespace promises.internal | |||
if storage = invalid then | |||
' unregister any observers on the promise to prevent multiple callbacks | |||
promises.internal.unobserveFieldScoped(promise, promises.internal.PromiseField.promiseState) | |||
promises.internal.observeFieldScoped(promise, promises.internal.PromiseField.promiseState, promises.internal.notifyListeners) | |||
promises.internal.observeFieldScoped(promise, promises.internal.PromiseField.promiseState, sub(event) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This was the important piece here. This observer fires synchronously, which would then trigger upstream promises to also trigger synchronously (since they were all already marked resolved).
By adding a nexttick, it allows each promise's observers to run nexttick, breaking the stack chain.
@@ -482,7 +488,7 @@ namespace promises.internal | |||
print "Crash during utils.delay:", e | |||
#end if | |||
end try | |||
m[delayId] = invalid | |||
m.delete(delayId) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Unrelated to the main issue. I noticed these timer entries were never getting properly cleaned up, as they were only being set to invalid. This should clean up the m
variable nicely now.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I had a similar issue with a rewrite I did of the Roku Unit Test Framework to make async tests work. I think it was like 30 or so levels deep that caused a stack overflow.
I remedied it basically the same way.
👍
Fixes an issue where deeply nested promise chain could cause a stackoverflow. This seems to be caused by the observers all firing synchronously, so when the lowest promise unravels, it synchronously triggers observers the entire way back up.
I added a test that proves we can now successfully unravel a promise chain 10,000 promises deep (but I suspect there is no limitation now in our implementation other than max memory usage).