Skip to content

Commit 5e20d05

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

File tree

1 file changed

+12
-5
lines changed

1 file changed

+12
-5
lines changed

lib/internal/streams/writable.js

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -493,13 +493,17 @@ function afterWrite(stream, state, count, cb) {
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);
@@ -671,6 +675,9 @@ function callFinal(stream, state) {
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)