From 23ba0889cedab2fb9129d0dffbd5ebf6cd30774d Mon Sep 17 00:00:00 2001 From: Robert Nagy Date: Sun, 15 Mar 2020 00:50:09 +0100 Subject: [PATCH] stream: don't emit 'finish' after 'error' Backport-PR-URL: https://github.com/nodejs/node/pull/32372 PR-URL: https://github.com/nodejs/node/pull/32275 Refs: https://github.com/nodejs/node/issues/28710 Reviewed-By: Luigi Pinca Reviewed-By: James M Snell Backport-PR-URL: https://github.com/nodejs/node/pull/32372 --- lib/_stream_writable.js | 2 ++ test/parallel/test-stream-writable-write-writev-finish.js | 1 + 2 files changed, 3 insertions(+) diff --git a/lib/_stream_writable.js b/lib/_stream_writable.js index c8dbeeaddde8c0..d2739f5788a606 100644 --- a/lib/_stream_writable.js +++ b/lib/_stream_writable.js @@ -647,6 +647,8 @@ function finishMaybe(stream, state) { if (need) { prefinish(stream, state); if (state.pendingcb === 0) { + if (state.errorEmitted) + return; state.finished = true; stream.emit('finish'); diff --git a/test/parallel/test-stream-writable-write-writev-finish.js b/test/parallel/test-stream-writable-write-writev-finish.js index aa43b1490c8600..9fce315f8b2e1a 100644 --- a/test/parallel/test-stream-writable-write-writev-finish.js +++ b/test/parallel/test-stream-writable-write-writev-finish.js @@ -132,6 +132,7 @@ const stream = require('stream'); process.nextTick(cb); }; w.on('error', common.mustCall()); + w.on('finish', common.mustNotCall()); w.on('prefinish', () => { w.write("shouldn't write in prefinish listener"); });