Skip to content

Commit

Permalink
stream: fix multiple Writable.destroy() calls
Browse files Browse the repository at this point in the history
Calling Writable.destroy() multiple times in the same tick
could cause an assertion error.

Fixes: #38189

PR-URL: #38221
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
Reviewed-By: Nitzan Uziely <linkgoron@gmail.com>
Reviewed-By: Rich Trott <rtrott@gmail.com>
  • Loading branch information
ronag committed Apr 16, 2021
1 parent d3162da commit 369f239
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 6 deletions.
9 changes: 3 additions & 6 deletions lib/internal/streams/writable.js
Expand Up @@ -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,
Expand Down Expand Up @@ -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);
}

Expand Down
11 changes: 11 additions & 0 deletions test/parallel/test-stream-writable-destroy.js
Expand Up @@ -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();
}

0 comments on commit 369f239

Please sign in to comment.