From 531d495aa75b0b6af3c08113d93f2927bcb91593 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"); });