Skip to content

Commit 0793398

Browse files
David Mark Clementstargos
authored andcommitted
stream: add 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. PR-URL: #28566 Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: Jeremiah Senkpiel <fishrock123@rocketmail.com> Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Rich Trott <rtrott@gmail.com>
1 parent 77bdbc5 commit 0793398

2 files changed

Lines changed: 26 additions & 2 deletions

File tree

‎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
constreject=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: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
'use strict';
22

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

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

399+
{
400+
console.log('readable side of a transform stream pushes null');
401+
consttransform=newTransform({
402+
objectMode: true,
403+
transform: (chunk,enc,cb)=>{cb(null,chunk);}
404+
});
405+
transform.push(0);
406+
transform.push(1);
407+
process.nextTick(()=>{
408+
transform.push(null);
409+
});
410+
411+
constmustReach=[common.mustCall(),common.mustCall()];
412+
413+
constiter=transform[Symbol.asyncIterator]();
414+
assert.strictEqual((awaititer.next()).value,0);
415+
416+
forawait(constdofiter){
417+
assert.strictEqual(d,1);
418+
mustReach[0]();
419+
}
420+
mustReach[1]();
421+
}
422+
399423
{
400424
console.log('all next promises must be resolved on end');
401425
constr=newReadable({

0 commit comments

Comments
 (0)