From 6ae4e4a86814f6dac68e0f603ba26fc41cc9939a 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 2fe811236a27e8..693ef2ee5d25ff 100644 --- a/lib/internal/streams/writable.js +++ b/lib/internal/streams/writable.js @@ -855,10 +855,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(); +}