diff --git a/lib/internal/streams/writable.js b/lib/internal/streams/writable.js index 7c082c075642eb..defbbc1a3201f0 100644 --- a/lib/internal/streams/writable.js +++ b/lib/internal/streams/writable.js @@ -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); @@ -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++) {