Skip to content

Commit 6df81c4

Browse files
committed
stream: throw error if stream has been destroyed on _final and _write
Fixes: #39030
1 parent 85d4cd3 commit 6df81c4

File tree

1 file changed

+14
-7
lines changed

1 file changed

+14
-7
lines changed

lib/internal/streams/writable.js

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -486,20 +486,24 @@ function afterWriteTick({ stream, state, count, cb }) {
486486
}
487487

488488
function afterWrite(stream, state, count, cb) {
489-
const needDrain = !state.ending && !stream.destroyed && state.length === 0 &&
489+
const needDrain = !state.ending && !state.destroyed && state.length === 0 &&
490490
state.needDrain;
491491
if (needDrain) {
492492
state.needDrain = false;
493493
stream.emit('drain');
494494
}
495495

496-
while (count-- > 0) {
497-
state.pendingcb--;
498-
cb();
499-
}
500-
501496
if (state.destroyed) {
497+
while (count-- > 0) {
498+
state.pendingcb--;
499+
cb(new ERR_STREAM_DESTROYED('write'));
500+
}
502501
errorBuffer(state);
502+
} else {
503+
while (count-- > 0) {
504+
state.pendingcb--;
505+
cb();
506+
}
503507
}
504508

505509
finishMaybe(stream, state);
@@ -665,12 +669,15 @@ function callFinal(stream, state) {
665669

666670
function onFinish(err) {
667671
if (called) {
668-
errorOrDestroy(stream, err ?? ERR_MULTIPLE_CALLBACK());
672+
errorOrDestroy(stream, err ?? new ERR_MULTIPLE_CALLBACK());
669673
return;
670674
}
671675
called = true;
672676

673677
state.pendingcb--;
678+
if (!err && state.destroyed) {
679+
err = new ERR_STREAM_DESTROYED('final')
680+
}
674681
if (err) {
675682
const onfinishCallbacks = state[kOnFinished].splice(0);
676683
for (let i = 0; i < onfinishCallbacks.length; i++) {

0 commit comments

Comments
 (0)