From 369f23950346706828953a1ab99f8cd07879e053 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 PR-URL: https://github.com/nodejs/node/pull/38221 Reviewed-By: Matteo Collina Reviewed-By: Luigi Pinca Reviewed-By: Nitzan Uziely Reviewed-By: Rich Trott --- lib/internal/streams/writable.js | 9 +++------ test/parallel/test-stream-writable-destroy.js | 11 +++++++++++ 2 files changed, 14 insertions(+), 6 deletions(-) diff --git a/lib/internal/streams/writable.js b/lib/internal/streams/writable.js index 2fe811236a27e8..d00e175ceb1c1b 100644 --- a/lib/internal/streams/writable.js +++ b/lib/internal/streams/writable.js @@ -57,7 +57,6 @@ const { getHighWaterMark, getDefaultHighWaterMark } = require('internal/streams/state'); -const assert = require('internal/assert'); const { ERR_INVALID_ARG_TYPE, ERR_METHOD_NOT_IMPLEMENTED, @@ -854,11 +853,9 @@ Writable.prototype.destroy = function(err, cb) { const state = this._writableState; // Invoke pending callbacks. - if ( - state.bufferedIndex < state.buffered.length || - state[kOnFinished].length - ) { - assert(!state.destroyed); + if (!state.destroyed && + (state.bufferedIndex < state.buffered.length || + state[kOnFinished].length)) { 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(); +}