Properly handle multiple iterators for Events#38
Merged
Marsup merged 1 commit intohapijs:masterfrom Aug 21, 2025
Merged
Conversation
Marsup
reviewed
Aug 20, 2025
|
|
||
| #pending = null; | ||
| #queue = []; | ||
| #iterators = new Set(); |
Contributor
There was a problem hiding this comment.
Wouldn't it be safer to also use a WeakSet here just in case it's iterated through weird means?
Contributor
Author
There was a problem hiding this comment.
It might be safer, but not possible since you can't iterate a WeakSet.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Due to using shared state, the current implementation is broken if multiple iterators are used on the Events class.
This is fixed in this PR by:
iterator()only returns newly emitted events, and not all emitted events during the Events instance lifetime.Additionally, this fixes it so trying to iterate further on a completed iterator returns "done", instead of a never-ending Promise.
The updated semantics are required. Otherwise the Events instance will need to hang on to all emitted events, just in case someone calls
iterator()on it again. Given that I am not aware of any existing usage of the Events feature, I cannot know if this will cause problems and should be considered a breaking change. It all depends on how earlyiterator()is called. An alternative fix, that might cause less breakage, is to add an assert that only allowiterator()to be called once.Note that with the moved state, care must be taken not to cause a leak due to two-way references. I have added logic to handle this automatically when using
for-ofthrough the implicitdestroy()call, as well as several tests.