Skip to content

Commit 369f239

Browse files
committed
stream: fix multiple Writable.destroy() calls
Calling Writable.destroy() multiple times in the same tick could cause an assertion error. Fixes: #38189 PR-URL: #38221 Reviewed-By: Matteo Collina <[email protected]> Reviewed-By: Luigi Pinca <[email protected]> Reviewed-By: Nitzan Uziely <[email protected]> Reviewed-By: Rich Trott <[email protected]>
1 parent d3162da commit 369f239

File tree

2 files changed

+14
-6
lines changed

2 files changed

+14
-6
lines changed

Diff for: lib/internal/streams/writable.js

+3-6
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,6 @@ const {
5757
getHighWaterMark,
5858
getDefaultHighWaterMark
5959
} = require('internal/streams/state');
60-
const assert = require('internal/assert');
6160
const {
6261
ERR_INVALID_ARG_TYPE,
6362
ERR_METHOD_NOT_IMPLEMENTED,
@@ -854,11 +853,9 @@ Writable.prototype.destroy = function(err, cb) {
854853
const state = this._writableState;
855854

856855
// Invoke pending callbacks.
857-
if (
858-
state.bufferedIndex < state.buffered.length ||
859-
state[kOnFinished].length
860-
) {
861-
assert(!state.destroyed);
856+
if (!state.destroyed &&
857+
(state.bufferedIndex < state.buffered.length ||
858+
state[kOnFinished].length)) {
862859
process.nextTick(errorBuffer, state);
863860
}
864861

Diff for: test/parallel/test-stream-writable-destroy.js

+11
Original file line numberDiff line numberDiff line change
@@ -460,3 +460,14 @@ const assert = require('assert');
460460
assert.strictEqual(write.destroyed, true);
461461
}));
462462
}
463+
464+
{
465+
// Destroy twice
466+
const write = new Writable({
467+
write(chunk, enc, cb) { cb(); }
468+
});
469+
470+
write.end(common.mustCall());
471+
write.destroy();
472+
write.destroy();
473+
}

0 commit comments

Comments
 (0)