stream: fix nested compose error propagation

PR-URL: https://github.com/nodejs/node/pull/62556
Fixes: https://github.com/nodejs/node/issues/60083
Reviewed-By: Robert Nagy <ronagy@icloud.com>
Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
This commit is contained in:
Matteo Collina authored and GitHub committed 2026-04-17 01:46:10 +00:00
1 parent bee1087258
commit 9a7e7b889b
3 files changed
+48 -9

No files matched your search

+11 -1
View File
@@ -239,7 +239,17 @@ function fromAsyncGen(fn) {
_resolve({ __proto__: null, done: true, cb });
},
destroy(err, cb) {
ac.abort();
ac.abort(err);
// If the source async iterator is waiting for the next write/final
// signal, unblock it so the readable side can observe the abort and
// finish destroying.
if (resolve !== null) {
const _resolve = resolve;
resolve = null;
_resolve({ __proto__: null, done: true, cb() {} });
}
cb(err);
},
};
+19 -8
View File
@@ -8,9 +8,12 @@ const {
const { Buffer } = require('buffer');
const {
ERR_INVALID_ARG_TYPE,
ERR_STREAM_NULL_VALUES,
} = require('internal/errors').codes;
aggregateTwoErrors,
codes: {
ERR_INVALID_ARG_TYPE,
ERR_STREAM_NULL_VALUES,
},
} = require('internal/errors');
function from(Readable, iterable, opts) {
let iterator;
@@ -43,6 +46,7 @@ function from(Readable, iterable, opts) {
// TODO(ronag): What options should be allowed?
...opts,
});
const originalDestroy = readable._destroy;
// Flag to protect against _read
// being called before last iteration completion.
@@ -64,11 +68,18 @@ function from(Readable, iterable, opts) {
};
readable._destroy = function(error, cb) {
PromisePrototypeThen(
close(error),
() => process.nextTick(cb, error), // nextTick is here in case cb throws
(e) => process.nextTick(cb, e || error),
);
originalDestroy.call(this, error, (destroyError) => {
const combinedError = destroyError || error;
PromisePrototypeThen(
close(combinedError),
// nextTick is here in case cb throws
() => process.nextTick(cb, combinedError),
(closeError) => process.nextTick(
cb,
aggregateTwoErrors(combinedError, closeError),
),
);
});
};
async function close(error) {
@@ -116,6 +116,24 @@ const assert = require('assert');
).then(common.mustCall());
}
{
// Errors from nested `.compose()` calls should propagate instead of hanging.
const stream = Readable.from(['hello'])
.compose(async function *(source) { // eslint-disable-line require-yield
for await (const chunk of source) {
throw new Error(`boom: ${chunk}`);
}
})
.compose(async function *(source) {
yield* source;
});
assert.rejects(
stream.toArray(),
/boom: hello/,
).then(common.mustCall());
}
{
// AbortSignal
const ac = new AbortController();