Skip to content

Commit 80158ec

Browse files
author
David Mark Clements
committed
stream: null push transform in async_iterator
when the readable side of a transform ends any for await loop on that transform stream should also complete. This fix prevents for await loop on a transform stream from hanging indefinitely. #28566
1 parent de10602 commit 80158ec

File tree

2 files changed

+24
-2
lines changed

2 files changed

+24
-2
lines changed

lib/internal/streams/async_iterator.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@ const createReadableStreamAsyncIterator = (stream) => {
155155
});
156156
iterator[kLastPromise] = null;
157157

158-
finished(stream, (err) => {
158+
finished(stream, { writable: false }, (err) => {
159159
if (err && err.code !== 'ERR_STREAM_PREMATURE_CLOSE') {
160160
const reject = iterator[kLastReject];
161161
// Reject if we are waiting for data in the Promise returned by next() and

test/parallel/test-stream-readable-async-iterators.js

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
'use strict';
22

33
const common = require('../common');
4-
const { Readable, PassThrough, pipeline } = require('stream');
4+
const { Readable, Transform, PassThrough, pipeline } = require('stream');
55
const assert = require('assert');
66

77
async function tests() {
@@ -396,6 +396,28 @@ async function tests() {
396396
}
397397
}
398398

399+
console.log('readable side of a transform stream pushes null');
400+
{
401+
const transform = new Transform({
402+
objectMode: true,
403+
transform(chunk, enc, cb) {
404+
cb(null, chunk);
405+
}
406+
});
407+
transform.push(0);
408+
transform.push(1);
409+
transform.push(null);
410+
const mustReach = common.mustCall();
411+
const iter = transform[Symbol.asyncIterator]();
412+
assert.strictEqual((await iter.next()).value, 0);
413+
414+
for await (const d of iter) {
415+
assert.strictEqual(d, 1);
416+
}
417+
418+
mustReach();
419+
}
420+
399421
{
400422
console.log('all next promises must be resolved on end');
401423
const r = new Readable({

0 commit comments

Comments
 (0)