diff --git a/lib/internal/streams/end-of-stream.js b/lib/internal/streams/end-of-stream.js index eda08b5fc60b93..31b8dfca58cbe3 100644 --- a/lib/internal/streams/end-of-stream.js +++ b/lib/internal/streams/end-of-stream.js @@ -214,7 +214,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); })); }