Skip to content

Commit b3372a8

Browse files
jakecastellitargos
authored andcommitted
stream: callback should be called when pendingcb is 0
Fixes: #46170 PR-URL: #53438 Reviewed-By: Robert Nagy <ronagy@icloud.com> Reviewed-By: Chemi Atlow <chemi@atlow.co.il> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
1 parent 4b47f89 commit b3372a8

File tree

2 files changed

+9
-2
lines changed

2 files changed

+9
-2
lines changed

lib/internal/streams/end-of-stream.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -213,7 +213,8 @@ function eos(stream, options, callback) {
213213
} else if (
214214
!readable &&
215215
(!willEmitClose || isReadable(stream)) &&
216-
(writableFinished || isWritable(stream) === false)
216+
(writableFinished || isWritable(stream) === false) &&
217+
(wState == null || wState.pendingcb === 0)
217218
) {
218219
process.nextTick(onclosed);
219220
} else if (

test/parallel/test-stream-finished.js

+7-1
Original file line numberDiff line numberDiff line change
@@ -669,15 +669,21 @@ testClosed((opts) => new Writable({ write() {}, ...opts }));
669669
}
670670

671671
{
672+
let isCalled = false;
672673
const stream = new Duplex({
673674
write(chunk, enc, cb) {
674-
setImmediate(cb);
675+
setImmediate(() => {
676+
isCalled = true;
677+
cb();
678+
});
675679
}
676680
});
677681

678682
stream.end('foo');
679683

680684
finished(stream, { readable: false }, common.mustCall((err) => {
681685
assert(!err);
686+
assert.strictEqual(isCalled, true);
687+
assert.strictEqual(stream._writableState.pendingcb, 0);
682688
}));
683689
}

0 commit comments

Comments
 (0)