Skip to content
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

Merged
merged 2 commits into from
Oct 18, 2024
Merged

Prevent stackoverflow #23

merged 2 commits into from
Oct 18, 2024

Conversation

TwitchBronBron
Copy link
Member

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).

@@ -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)
Copy link
Member Author

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)
Copy link
Member Author

@TwitchBronBron TwitchBronBron Oct 17, 2024

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.

Copy link

@markwpearce markwpearce left a 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.
👍

@TwitchBronBron TwitchBronBron merged commit 0c4804e into master Oct 18, 2024
2 checks passed
@TwitchBronBron TwitchBronBron deleted the prevent-stackoverflow branch October 18, 2024 14:49
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants