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

guard against race condition #9338

Closed
wants to merge 1 commit into from
Closed

Conversation

Rich-Harris
Copy link
Member

this prevents a race condition in #9332 — if someone were to call iterator.next() twice, it would return the same promise. In practice this isn't an issue because we're using for await, but it could potentially bite us in future

@dummdidumm
Copy link
Member

This assumes that when there's nothing left in the array that we are done, but the producer could still push things before being done. I think we need to push defer() into the array in both push and next if needed, and an index keeps track of the current value that needs to be fullfilled, which is used next to know which existing defer to fulfil and whether or not to push a new defer() into the array; and done needs to resolve all pending promises.

@Rich-Harris
Copy link
Member Author

Tried implementing that, got quite unwieldy, and when I tried to add tests I realised that unless you manually call iterator[Symbol.asyncIterator]() yourself (which... don't) you can't call next() repeatedly without waiting for the relevant promises to resolve anyway (i.e. you can only consume an async iterator via for await), so this race condition probably doesn't actually matter. So I vote for closing this PR

@Rich-Harris
Copy link
Member Author

Though in the course of reading through MDN I realised we're using terminology incorrectly — the thing we currently call iterator is actually an iterable; the thing returned from iterable[Symbol.asyncIterator]() is the iterator. Brilliant work, whoever named all this stuff

@dummdidumm
Copy link
Member

👍 closing this then

@dummdidumm dummdidumm closed this Mar 6, 2023
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.

2 participants