From f9172635571eca1257eef608d982db5ec154e7ef Mon Sep 17 00:00:00 2001 From: Debadree Chatterjee Date: Thu, 27 Apr 2023 13:14:39 +0530 Subject: [PATCH] stream: prevent pipeline hang with generator functions Fixes: https://github.com/nodejs/node/issues/47708 PR-URL: https://github.com/nodejs/node/pull/47712 Reviewed-By: Robert Nagy Reviewed-By: Antoine du Hamel Reviewed-By: Feng Yu --- lib/internal/streams/pipeline.js | 3 +-- test/parallel/test-stream-pipeline.js | 18 ++++++++++++++++++ 2 files changed, 19 insertions(+), 2 deletions(-) diff --git a/lib/internal/streams/pipeline.js b/lib/internal/streams/pipeline.js index 95737d95e48e41..062bdc192d1310 100644 --- a/lib/internal/streams/pipeline.js +++ b/lib/internal/streams/pipeline.js @@ -138,10 +138,9 @@ async function pumpToNode(iterable, writable, finish, { end }) { if (end) { writable.end(); + await wait(); } - await wait(); - finish(); } catch (err) { finish(error !== err ? aggregateTwoErrors(error, err) : err); diff --git a/test/parallel/test-stream-pipeline.js b/test/parallel/test-stream-pipeline.js index d37ca275f1dddf..e9f6a2fdf711d3 100644 --- a/test/parallel/test-stream-pipeline.js +++ b/test/parallel/test-stream-pipeline.js @@ -1616,3 +1616,21 @@ const tsp = require('timers/promises'); dup.push(null); dup.read(); } + +{ + let res = ''; + const writable = new Writable({ + write(chunk, enc, cb) { + res += chunk; + cb(); + } + }); + pipelinep(async function*() { + yield 'hello'; + await Promise.resolve(); + yield 'world'; + }, writable, { end: false }).then(common.mustCall(() => { + assert.strictEqual(res, 'helloworld'); + assert.strictEqual(writable.closed, false); + })); +}