Skip to content

Commit

Permalink
stream: throw error if stream has been destroyed on _final and _write
Browse files Browse the repository at this point in the history
Fixes: #39030
  • Loading branch information
JPedroAmorim committed Dec 14, 2021
1 parent 85d4cd3 commit 5e20d05
Showing 1 changed file with 12 additions and 5 deletions.
17 changes: 12 additions & 5 deletions lib/internal/streams/writable.js
Original file line number Diff line number Diff line change
Expand Up @@ -493,13 +493,17 @@ function afterWrite(stream, state, count, cb) {
stream.emit('drain');
}

while (count-- > 0) {
state.pendingcb--;
cb();
}

if (state.destroyed) {
while (count-- > 0) {
state.pendingcb--;
cb(new ERR_STREAM_DESTROYED('write'));
}
errorBuffer(state);
} else {
while (count-- > 0) {
state.pendingcb--;
cb();
}
}

finishMaybe(stream, state);
Expand Down Expand Up @@ -671,6 +675,9 @@ function callFinal(stream, state) {
called = true;

state.pendingcb--;
if (!err && state.destroyed) {
err = new ERR_STREAM_DESTROYED('final');
}
if (err) {
const onfinishCallbacks = state[kOnFinished].splice(0);
for (let i = 0; i < onfinishCallbacks.length; i++) {
Expand Down

0 comments on commit 5e20d05

Please sign in to comment.