From 2609204b1f3c46ffcff70766423f08a79f0b7e5c Mon Sep 17 00:00:00 2001 From: Robert Nagy Date: Tue, 13 Apr 2021 00:54:24 +0200 Subject: [PATCH] stream: fix multiple Writable.destroy() calls. Calling Writable.destroy() multiple times in the same tick could cause an assertion error. Fixes: https://github.com/nodejs/node/issues/38189 --- lib/internal/streams/writable.js | 4 ++-- test/parallel/test-stream-writable-destroy.js | 11 +++++++++++ 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/lib/internal/streams/writable.js b/lib/internal/streams/writable.js index 97fe1d28ef1f3a..45de5157ec517b 100644 --- a/lib/internal/streams/writable.js +++ b/lib/internal/streams/writable.js @@ -845,10 +845,10 @@ Writable.prototype.destroy = function(err, cb) { // Invoke pending callbacks. if ( + !state.destroyed && ( state.bufferedIndex < state.buffered.length || state[kOnFinished].length - ) { - assert(!state.destroyed); + )) { process.nextTick(errorBuffer, state); } diff --git a/test/parallel/test-stream-writable-destroy.js b/test/parallel/test-stream-writable-destroy.js index 7cd800b0938138..2e6e1f975a2be1 100644 --- a/test/parallel/test-stream-writable-destroy.js +++ b/test/parallel/test-stream-writable-destroy.js @@ -460,3 +460,14 @@ const assert = require('assert'); assert.strictEqual(write.destroyed, true); })); } + +{ + // Destroy twice + const write = new Writable({ + write(chunk, enc, cb) { cb(); } + }); + + write.end(common.mustCall()); + write.destroy(); + write.destroy(); +}