Skip to content

stream.Readable.from does not always close synchronous generators #32842

@vadzim

Description

@vadzim
  • Version: 12.16.2, 13.12.0
  • Platform: linux
  • Subsystem: arch

What steps will reproduce the bug?

If synchronous generator wrapped with stream.Readable.from emits rejected Promise then finally blocks of that generator is never called.
But if that generator is consumed with general for-of loop then finally blocks are called as expected.

stream.Readable.from(function*() {
  try {
    yield Promise.resolve(1)
    yield Promise.reject('error')
  } finally {
    console.log('fin')
  }
}())
.on('data',x=>console.log(x))
.on('error',e=>console.log(e))

It logs

1
error

but does not log fin.

How often does it reproduce? Is there a required condition?

Always.

What is the expected behavior?

stream.Readable.from should either not resolve Promises yielded from synchronous generators and should pass unresolved Promises to the returned stream or should properly close generator even if it yields rejected Promise.

Additional information

To fix this there should be called iterator.return if await value throws.

readable.destroy(err);

Metadata

Metadata

Assignees

No one assigned

    Labels

    streamIssues and PRs related to the stream subsystem.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions