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

stream: improve Readable.from error handling #37158

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 15 additions & 18 deletions lib/internal/streams/from.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,6 @@ function from(Readable, iterable, opts) {
// being called before last iteration completion.
let reading = false;

// Flag for when iterator needs to be explicitly closed.
let needToClose = false;

readable._read = function() {
if (!reading) {
reading = true;
Expand All @@ -54,19 +51,23 @@ function from(Readable, iterable, opts) {
};

readable._destroy = function(error, cb) {
if (needToClose) {
needToClose = false;
PromisePrototypeThen(
close(),
() => process.nextTick(cb, error),
benjamingr marked this conversation as resolved.
Show resolved Hide resolved
(e) => process.nextTick(cb, error || e),
);
} else {
cb(error);
}
PromisePrototypeThen(
close(error),
() => process.nextTick(cb, error), // nextTick is here in case cb throws
(e) => process.nextTick(cb, e || error),
);
};

async function close() {
async function close(error) {
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 is tricky and I want feedback on the semantics, basically:

  • If we had an error ( .destroy(err) ) we call .throw - this aligns the behaviour of .from with other methods and means stuff like addAbortSignal automagically "works".
  • If after calling throw the iterator did not finish - we also call .return

const hadError = (error !== undefined) && (error !== null);
const hasThrow = typeof iterator.throw === 'function';
if (hadError && hasThrow) {
const { value, done } = await iterator.throw(error);
await value;
if (done) {
return;
}
}
if (typeof iterator.return === 'function') {
const { value } = await iterator.return();
await value;
Expand All @@ -75,13 +76,9 @@ function from(Readable, iterable, opts) {

async function next() {
try {
needToClose = false;
const { value, done } = await iterator.next();
needToClose = !done;
if (done) {
readable.push(null);
} else if (readable.destroyed) {
await close();
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 check is no longer necessary without needToClose since _destroy is called and if the stream is destroyed the iterator is closed.

} else {
const res = await value;
if (res === null) {
Expand Down
23 changes: 22 additions & 1 deletion test/parallel/test-readable-from.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ const { mustCall } = require('../common');
const { once } = require('events');
const { Readable } = require('stream');
const { strictEqual, throws } = require('assert');
const common = require('../common');

{
throws(() => {
Expand Down Expand Up @@ -187,6 +188,25 @@ async function endWithError() {
}
}

async function destroyingStreamWithErrorThrowsInGenerator() {
const validateError = common.mustCall((e) => {
strictEqual(e, 'Boum');
});
async function* generate() {
try {
yield 1;
yield 2;
yield 3;
throw new Error();
} catch (e) {
validateError(e);
}
}
const stream = Readable.from(generate());
stream.read();
stream.once('error', common.mustCall());
stream.destroy('Boum');
}

Promise.all([
toReadableBasicSupport(),
Expand All @@ -198,5 +218,6 @@ Promise.all([
toReadableOnDataNonObject(),
destroysTheStreamWhenThrowing(),
asTransformStream(),
endWithError()
endWithError(),
destroyingStreamWithErrorThrowsInGenerator(),
]).then(mustCall());