FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

stream: fix handling generators in Readable.from by vadzim · Pull Request #32844 · nodejs/node · GitHub

/ node Public

stream: fix handling generators in Readable.from - #32844

Closed
vadzim wants to merge 11 commits into
nodejs:masterfrom
vadzim:FixHandlingGeneratorsInReadableFrom
Closed

stream: fix handling generators in Readable.from#32844
vadzim wants to merge 11 commits into
nodejs:masterfrom
vadzim:FixHandlingGeneratorsInReadableFrom

Conversation

vadzim commented Apr 14, 2020
edited by ronag
Loading

Copy link
Copy Markdown
Contributor
Checklist
  • make -j4 test (UNIX), or vcbuild test (Windows) passes
  • tests and/or benchmarks are included
  • commit message follows commit guidelines

closes #32842

vadzim changed the title Fix handling generators in readable from Fix handling generators in stream.Readable.from Apr 14, 2020
ronag requested review from mcollina and ronag April 14, 2020 13:51
Comment thread lib/internal/streams/from.js Outdated

ronag left a comment
edited
Loading

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

Good spot. I'm not sure needToClose is required, just always call return() in _destroy(). Also, please add a test.

mcollina left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

Would you mind to add a unit test for this? Thanks

vadzim commented Apr 14, 2020
edited
Loading

Copy link
Copy Markdown
Contributor Author

@ronag @mcollina sure, I'll add a test as soon as it builds on my machine.

vadzim commented Apr 14, 2020

Copy link
Copy Markdown
Contributor Author

I'm not sure needToClose is required, just always call return() in _destroy()

for-of loop does this check. Why we shouldn't?

ronag commented Apr 14, 2020
edited
Loading

Copy link
Copy Markdown
Member

Here is a "desugared" pseudo version of for-of I got from Gus Caplan:

const iterator = B[Symbol.asyncIterator]();
while (true) {
  const next = await iterator.next();
  if (next.done) {
    break;
  }
  const A = next.value;

  // pseudocode follows, because dealing with `break` and `throw` is difficult to desugar.
  const result = BODY;
  if (result is a break or result is a throw) {
    if (iterator.return !== undefined) {
      let innerResult = iterator.return();
      if (innerResult is not a throw) {
        // if iterator.return() threw an exception, we don't want to await it.
        innerResult = await innerResult;
      }
      if (result is a throw) {
        // if BODY threw an exception, we want to throw that one instead of the possible
        // exception from iterator.return()
        throw result;
      }
      if (innerResult is a throw) {
        // if iterator.return() threw, either directly or because it returned a rejected promise,
        // now we re-throw that exception.
        throw innerResult;
      }
      break;
    }
  }
}

I think that's probably what we should aim for.

As you've noticed the throw + return() part is tricky.

ronag commented Apr 14, 2020

Copy link
Copy Markdown
Member

for-of loop does this check.

Where do you see this? Maybe?

vadzim commented Apr 14, 2020
edited
Loading

Copy link
Copy Markdown
Contributor Author

I'm sorry, it's quite difficult for me to understand that pseudocode.
The next is what I got from the node cli:

// next returns ordinary value, done = false:
for(const x of {
  [Symbol.iterator](){ return this },
  next(){ console.log('next'); return { value: 42, done: false } },
  return(){ console.log('return'); return { done: true } }
}) {
  console.log(x)
  break
}
// output:
//    next
//    42
//    return
//    undefined

// next returns done = true:
for(const x of {
  [Symbol.iterator](){ return this },
  next(){ console.log('next'); return { value: 42, done: true } },
  return(){ console.log('return'); return { done: true } }
}) {
  console.log(x)
  break
}
// output:
//    next
//    undefined

// next throws
for(const x of {
  [Symbol.iterator](){ return this },
  next(){ console.log('next'); throw 42 },
  return(){ console.log('return'); return { done: true } }
}) {
  console.log(x)
  break
}
// output:
//    next
//    Uncaught 42

It seems that return isn't called if next throws.

Comment thread lib/internal/streams/from.js Outdated

ronag commented Apr 14, 2020
edited
Loading

Copy link
Copy Markdown
Member

The next is what I got from the node cli:

What do you want to illustrate with this?

It seems that return isn't called if next throws.

Yes, that's what we are fixing in this PR?

vadzim commented Apr 14, 2020
edited
Loading

Copy link
Copy Markdown
Contributor Author

What do you want to illustrate with this?

I mean

It seems that return isn't called if next throws.

ronag commented Apr 14, 2020

Copy link
Copy Markdown
Member

Ah, I think I understand what you mean.

ronag commented Apr 14, 2020

Copy link
Copy Markdown
Member

What happens if you do the same thing with async iterators?

ronag commented Apr 14, 2020

Copy link
Copy Markdown
Member

It seems that return isn't called if next throws.

But then the problem with #32842 remains?

I think looking at the for await..of would be relevant.

vadzim commented Apr 14, 2020
edited
Loading

Copy link
Copy Markdown
Contributor Author

What happens if you do the same thing with async iterators?

Same thing happens if we use for-await-of with async iterator.
.return isn't called if .next throws or returns {done: true}.

This comment has been minimized.

Comment thread lib/internal/streams/from.js Outdated

vadzim commented Apr 14, 2020

Copy link
Copy Markdown
Contributor Author

Giving this more thought. I think the absolute simplest and most correct implementation is to just simply use a for..of.

We still need to deal with buffer in readable stream.
After readable.push returns false we should wait for the next read call.
We just need _destroy implementation.

ronag left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

LGTM once tests are added, good job!

ronag added the stream Issues and PRs related to the stream subsystem. label Apr 14, 2020

vadzim commented Apr 14, 2020
edited
Loading

Copy link
Copy Markdown
Contributor Author

I'll write the tests, though it can take some time cause I'm gonna cover all these mentioned edge cases.

ronag commented Apr 14, 2020

Copy link
Copy Markdown
Member

Also, please take a look at the commit guidelines.

You should use stream: as subsystem, and Fixes: before the url for the issue it fixed.

vadzim commented Apr 14, 2020

Copy link
Copy Markdown
Contributor Author

Sure, I'll squash my draft commits into one when the job is done.

Comment thread lib/internal/streams/from.js Outdated

vadzim commented Apr 14, 2020
edited
Loading

Copy link
Copy Markdown
Contributor Author

I've added tests and it's allowed me to catch one more case.
But I'm afraid those tests are too complex to support and now I like your proposal to use for-of cycle. I'm gonna to prepare another PR to compare.

vadzim commented Apr 14, 2020

Copy link
Copy Markdown
Contributor Author

Actually the code does not look simpler) and the tests should remain in any case.
But you can choose either #32855

vadzim force-pushed the FixHandlingGeneratorsInReadableFrom branch from 426b1d6 to 6bddcf7 Compare April 14, 2020 22:29
vadzim requested a review from ronag April 15, 2020 13:46
Comment thread lib/internal/streams/from.js Outdated

ronag left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

LGTM

Copy link
Copy Markdown
Collaborator

vadzim requested a review from mcollina April 16, 2020 07:55
vadzim changed the title Fix handling generators in stream.Readable.from stream: fix handling generators in Readable.from Apr 17, 2020

mcollina left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

lgtm

ronag added the author ready PRs that have at least one approval, no outstanding review comments, and a CI started. label Apr 17, 2020

Copy link
Copy Markdown
Collaborator

vadzim commented Apr 17, 2020

Copy link
Copy Markdown
Contributor Author

@ronag @mcollina
Thank you for the great review. It's really valueble for my experience.

Btw, actually the code didn't contain any iterator.return() call, so it didn't close the iterator when not of its values were consumed. E.g. when the stream is destroyed.

I guess this bugfix could be applied to node@12. Does it make sense?

Copy link
Copy Markdown
Member

Absolutely, in due time. It will be included in the closest release of 12.x after 2 weeks of being in 13.x.

ronag pushed a commit that referenced this pull request Apr 18, 2020
Call iterator.return() if not all of its values are consumed.

Fixes: #32842

PR-URL: #32844
Reviewed-By: Robert Nagy <ronagy@icloud.com>
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: Zeyu Yang <himself65@outlook.com>

ronag commented Apr 18, 2020

Copy link
Copy Markdown
Member

Landed in 8a3fa32

ronag closed this Apr 18, 2020
BethGriggs pushed a commit that referenced this pull request Apr 27, 2020
Call iterator.return() if not all of its values are consumed.

Fixes: #32842

PR-URL: #32844
Reviewed-By: Robert Nagy <ronagy@icloud.com>
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: Zeyu Yang <himself65@outlook.com>
BethGriggs mentioned this pull request Apr 27, 2020
BridgeAR pushed a commit that referenced this pull request Apr 28, 2020
Call iterator.return() if not all of its values are consumed.

Fixes: #32842

PR-URL: #32844
Reviewed-By: Robert Nagy <ronagy@icloud.com>
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: Zeyu Yang <himself65@outlook.com>
BridgeAR mentioned this pull request Apr 28, 2020
targos pushed a commit that referenced this pull request May 7, 2020
Call iterator.return() if not all of its values are consumed.

Fixes: #32842

PR-URL: #32844
Reviewed-By: Robert Nagy <ronagy@icloud.com>
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: Zeyu Yang <himself65@outlook.com>
targos pushed a commit that referenced this pull request May 13, 2020
Call iterator.return() if not all of its values are consumed.

Fixes: #32842

PR-URL: #32844
Reviewed-By: Robert Nagy <ronagy@icloud.com>
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: Zeyu Yang <himself65@outlook.com>

Copy link
Copy Markdown
Member

Hi, I don't understand why needToClose is here? If someone destroys the stream we need to always close the generator don't we?

Copy link
Copy Markdown
Member

That is - if someone creates a stream, calls .read but .next is never called on the generator - needToClose is false and blocks don't run (since await iterator.next()` is async and waits a microtick)

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

Labels

author ready PRs that have at least one approval, no outstanding review comments, and a CI started. stream Issues and PRs related to the stream subsystem.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

stream.Readable.from does not always close synchronous generators

6 participants


Back | FazBrowse Home | New Git URL