From b3372a8b0e90583e5136893465392eee1786848d Mon Sep 17 00:00:00 2001 From: jakecastelli <38635403+jakecastelli@users.noreply.github.com> Date: Sat, 15 Jun 2024 17:00:11 +0930 Subject: [PATCH] stream: callback should be called when pendingcb is 0 Fixes: https://github.com/nodejs/node/issues/46170 PR-URL: https://github.com/nodejs/node/pull/53438 Reviewed-By: Robert Nagy Reviewed-By: Chemi Atlow Reviewed-By: James M Snell Reviewed-By: Matteo Collina --- lib/internal/streams/end-of-stream.js | 3 ++- test/parallel/test-stream-finished.js | 8 +++++++- 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/lib/internal/streams/end-of-stream.js b/lib/internal/streams/end-of-stream.js index dd8e4e5d8a0c7e..723292faf5bd3a 100644 --- a/lib/internal/streams/end-of-stream.js +++ b/lib/internal/streams/end-of-stream.js @@ -213,7 +213,8 @@ function eos(stream, options, callback) { } else if ( !readable && (!willEmitClose || isReadable(stream)) && - (writableFinished || isWritable(stream) === false) + (writableFinished || isWritable(stream) === false) && + (wState == null || wState.pendingcb === 0) ) { process.nextTick(onclosed); } else if ( diff --git a/test/parallel/test-stream-finished.js b/test/parallel/test-stream-finished.js index c7513805e7ac6f..6820ac18cf4b5c 100644 --- a/test/parallel/test-stream-finished.js +++ b/test/parallel/test-stream-finished.js @@ -669,9 +669,13 @@ testClosed((opts) => new Writable({ write() {}, ...opts })); } { + let isCalled = false; const stream = new Duplex({ write(chunk, enc, cb) { - setImmediate(cb); + setImmediate(() => { + isCalled = true; + cb(); + }); } }); @@ -679,5 +683,7 @@ testClosed((opts) => new Writable({ write() {}, ...opts })); finished(stream, { readable: false }, common.mustCall((err) => { assert(!err); + assert.strictEqual(isCalled, true); + assert.strictEqual(stream._writableState.pendingcb, 0); })); }