From 5e20d05c8034f93cbb02c92bd7c27d8a793d22d2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=C3=A3o=20Pedro=20de=20Amorim?= Date: Mon, 13 Dec 2021 22:47:30 -0300 Subject: [PATCH] stream: throw error if stream has been destroyed on _final and _write Fixes: https://github.com/nodejs/node/issues/39030 --- lib/internal/streams/writable.js | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) 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++) {