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 6df81c4
Showing 1 changed file with 14 additions and 7 deletions.
21 changes: 14 additions & 7 deletions lib/internal/streams/writable.js
Original file line number Diff line number Diff line change
Expand Up @@ -486,20 +486,24 @@ function afterWriteTick({ stream, state, count, cb }) {
}

function afterWrite(stream, state, count, cb) {
const needDrain = !state.ending && !stream.destroyed && state.length === 0 &&
const needDrain = !state.ending && !state.destroyed && state.length === 0 &&
state.needDrain;
if (needDrain) {
state.needDrain = false;
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 @@ -665,12 +669,15 @@ function callFinal(stream, state) {

function onFinish(err) {
if (called) {
errorOrDestroy(stream, err ?? ERR_MULTIPLE_CALLBACK());
errorOrDestroy(stream, err ?? new ERR_MULTIPLE_CALLBACK());
return;
}
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 6df81c4

Please sign in to comment.