Skip to content

stream: don't emit errors after destroy #29211

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 10 additions & 8 deletions lib/internal/streams/destroy.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,8 @@ function destroy(err, cb) {

if ((w && w.destroyed) || (r && r.destroyed)) {
if (cb) {
cb(err);
} else if (needError(this, err)) {
process.nextTick(emitErrorNT, this, err);
cb(err || null);
}

return this;
}

Expand All @@ -47,11 +44,11 @@ function destroy(err, cb) {
r.destroyed = true;
}

this._destroy(err || null, (err) => {
this._destroy(err || null, (er) => {
if (cb) {
process.nextTick(emitCloseNT, this);
cb(err);
} else if (needError(this, err)) {
cb(er || null);
}
if (err && needError(this, er || null)) {
process.nextTick(emitErrorAndCloseNT, this, err);
} else {
process.nextTick(emitCloseNT, this);
Expand Down Expand Up @@ -114,6 +111,11 @@ function errorOrDestroy(stream, err) {
const r = stream._readableState;
const w = stream._writableState;

// Don't emit errors if already destroyed.
if ((w && w.destroyed) || (r && r.destroyed)) {
return;
}

if ((r && r.autoDestroy) || (w && w.autoDestroy))
stream.destroy(err);
else if (needError(stream, err))
Expand Down
4 changes: 1 addition & 3 deletions test/parallel/test-stream-duplex-destroy.js
Original file line number Diff line number Diff line change
Expand Up @@ -144,9 +144,7 @@ const assert = require('assert');

duplex.on('finish', common.mustNotCall('no finish event'));
duplex.on('end', common.mustNotCall('no end event'));
duplex.on('error', common.mustCall((err) => {
assert.strictEqual(err, expected);
}));
duplex.on('error', common.mustNotCall());

duplex.destroy();
assert.strictEqual(duplex.destroyed, true);
Expand Down
31 changes: 28 additions & 3 deletions test/parallel/test-stream-readable-destroy.js
Original file line number Diff line number Diff line change
Expand Up @@ -130,9 +130,7 @@ const assert = require('assert');
});

read.on('end', common.mustNotCall('no end event'));
read.on('error', common.mustCall((err) => {
assert.strictEqual(err, expected);
}));
read.on('error', common.mustNotCall());

read.destroy();
assert.strictEqual(read.destroyed, true);
Expand Down Expand Up @@ -175,6 +173,7 @@ const assert = require('assert');
const expected = new Error('kaboom');

read.on('close', common.mustCall());
read.on('error', common.mustCall());
read.destroy(expected, common.mustCall(function(err) {
assert.strictEqual(err, expected);
}));
Expand All @@ -189,3 +188,29 @@ const assert = require('assert');
read.push('hi');
read.on('data', common.mustNotCall());
}

{
const read = new Readable({
read() {},
destroy: common.mustCall((err, cb) => {
cb(new Error('test'));
})
});

read.on('error', common.mustNotCall());
read.destroy();
}

{
const read = new Readable({
read() {},
destroy: common.mustCall((err, cb) => {
cb(new Error('test'))
})
});

read.on('error', common.mustCall(err => {
assert.strictEqual(err.message, 'destroyed');
}));
read.destroy(new Error('destroyed'));
}
4 changes: 1 addition & 3 deletions test/parallel/test-stream-transform-destroy.js
Original file line number Diff line number Diff line change
Expand Up @@ -135,9 +135,7 @@ const assert = require('assert');
transform.on('close', common.mustCall());
transform.on('finish', common.mustNotCall('no finish event'));
transform.on('end', common.mustNotCall('no end event'));
transform.on('error', common.mustCall((err) => {
assert.strictEqual(err, expected);
}));
transform.on('error', common.mustNotCall());

transform.destroy();
}
11 changes: 3 additions & 8 deletions test/parallel/test-stream-writable-destroy.js
Original file line number Diff line number Diff line change
Expand Up @@ -129,9 +129,7 @@ const assert = require('assert');

write.on('close', common.mustCall());
write.on('finish', common.mustNotCall('no finish event'));
write.on('error', common.mustCall((err) => {
assert.strictEqual(err, expected);
}));
write.on('error', common.mustNotCall());

write.destroy();
assert.strictEqual(write.destroyed, true);
Expand Down Expand Up @@ -163,10 +161,7 @@ const assert = require('assert');
});

writable.on('close', common.mustCall());
writable.on('error', common.expectsError({
type: Error,
message: 'kaboom 2'
}));
writable.on('error', common.mustNotCall());

writable.destroy();
assert.strictEqual(writable.destroyed, true);
Expand All @@ -175,7 +170,7 @@ const assert = require('assert');
// Test case where `writable.destroy()` is called again with an error before
// the `_destroy()` callback is called.
writable.destroy(new Error('kaboom 2'));
assert.strictEqual(writable._writableState.errorEmitted, true);
assert.strictEqual(writable._writableState.errorEmitted, false);
}

{
Expand Down